splash_page.dart
4.58 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import 'dart:async';
import 'package:Parlando/home/models/setting_entity.dart';
import 'package:Parlando/net/dio_utils.dart';
import 'package:Parlando/net/http_api.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:Parlando/extension/shared/size_fit.dart';
import 'package:Parlando/login/login_router.dart';
import 'package:Parlando/res/constant.dart';
import 'package:Parlando/routers/fluro_navigator.dart';
import 'package:Parlando/routers/routers.dart';
import 'package:Parlando/util/device_utils.dart';
import 'package:Parlando/util/image_utils.dart';
import 'package:Parlando/util/theme_utils.dart';
import 'package:Parlando/widgets/fractionally_aligned_sized_box.dart';
import 'package:Parlando/widgets/load_image.dart';
import 'package:jpush_flutter/jpush_flutter.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();
}
});
}
///创建 JPush
JPush jpush = JPush();
///配置应用 Key
jpush.setup(
appKey: "a3396b5bd1a399cfe5be96ed",
channel: "default",
production: false,
/// 设置是否打印 debug 日志
debug: true,
);
jpush.applyPushAuthority(
const NotificationSettingsIOS(sound: true, alert: true, badge: true),
);
}
@override
void dispose() {
_subscription?.cancel();
super.dispose();
}
void _initGuide() {
setState(() {
_status = 1;
});
}
void _initSplash() {
// 加载配置数据
DioUtils.instance.asyncRequestNetwork<SettingEntity>(
Method.get,
HttpApi.setting,
params: [],
onSuccess: (data) {},
onError: (code, msg) {},
);
_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.navBarPage, 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();
}
},
),
);
}
}