splash_page.dart
3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:one_poem/extension/shared/size_fit.dart';
import 'package:one_poem/login/login_router.dart';
import 'package:one_poem/res/constant.dart';
import 'package:one_poem/routers/fluro_navigator.dart';
import 'package:one_poem/routers/routers.dart';
import 'package:one_poem/util/device_utils.dart';
import 'package:one_poem/util/image_utils.dart';
import 'package:one_poem/util/theme_utils.dart';
import 'package:one_poem/widgets/fractionally_aligned_sized_box.dart';
import 'package:one_poem/widgets/load_image.dart';
import 'package:quick_actions/quick_actions.dart';
import 'package:rxdart/rxdart.dart';
import 'package:sp_util/sp_util.dart';
class SplashPage extends StatefulWidget {
const SplashPage({Key? key}) : super(key: key);
@override
_SplashPageState createState() => _SplashPageState();
}
class _SplashPageState extends State<SplashPage> {
int _status = 0;
final List<String> _guideList = ['app_start_1', 'app_start_2', 'app_start_3'];
StreamSubscription? _subscription;
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addPostFrameCallback((_) async {
/// 两种初始化方案,另一种见 main.dart
/// 两种方法各有优劣
await SpUtil.getInstance();
await Device.initDeviceInfo();
if (SpUtil.getBool(Constant.keyGuide, defValue: true)!) {
/// 预先缓存图片,避免直接使用时因为首次加载造成闪动
void _precacheImage(String image) {
precacheImage(
ImageUtils.getAssetImage(image, format: ImageFormat.png),
context);
}
_guideList.forEach(_precacheImage);
}
_initSplash();
});
if (Device.isAndroid) {
const QuickActions quickActions = QuickActions();
quickActions.initialize((String shortcutType) async {
if (shortcutType == 'demo') {
_subscription?.cancel();
}
});
}
}
@override
void dispose() {
_subscription?.cancel();
super.dispose();
}
void _initGuide() {
setState(() {
_status = 1;
});
}
void _initSplash() {
_subscription =
Stream.value(1).delay(const Duration(milliseconds: 1500)).listen((_) {
if (SpUtil.getBool(Constant.keyGuide, defValue: true)! ||
Constant.isDriverTest) {
SpUtil.putBool(Constant.keyGuide, false);
_initGuide();
} else {
_goLogin();
}
});
}
void _goLogin() {
if (SpUtil.containsKey(Constant.userToken)!) {
NavigatorUtils.push(context, Routes.home, clearStack: true);
} else {
NavigatorUtils.push(context, LoginRouter.loginPage, replace: true);
}
}
@override
Widget build(BuildContext context) {
HYSizeFit.initialize(context);
return Material(
color: context.backgroundColor,
child: _status == 0
? const FractionallyAlignedSizedBox(
heightFactor: 0.3,
widthFactor: 0.33,
leftFactor: 0.33,
bottomFactor: 0,
child: LoadAssetImage('logo'))
: Swiper(
key: const Key('swiper'),
itemCount: _guideList.length,
loop: false,
itemBuilder: (_, index) {
return LoadAssetImage(
_guideList[index],
key: Key(_guideList[index]),
fit: BoxFit.cover,
width: double.infinity,
height: double.infinity,
format: ImageFormat.png,
);
},
onTap: (index) {
if (index == _guideList.length - 1) {
_goLogin();
}
},
));
}
}