logo头像
Snippet 博客主题

Flutter app 5.添加启动页

Flutter app 5.添加启动页

1.创建启动页

创建启动页
pages/SplashPage.dart

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
class SplashPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new _SplashPageState();
}
}

class _SplashPageState extends State<SplashPage> {


@override
Widget build(BuildContext context) {
return new Container(
color: Colors.white,
child: new Center(
child: new Image.asset("images/snap_icon_launcher.png", width: 60.0) ),
);
}


@override
Future initState() {
super.initState();
initData();
}

Future getUserInfo() async {
Map map = {};
String result = await NetUtils.apiRequest(Api.getUserInfo, map);
Map<String, dynamic> resultMap = json.decode(result);

print(resultMap);

if( null != resultMap ){

if( 200 == resultMap['messageCode'] || "ok" == resultMap['message']){
var data = resultMap['data'];
if ( null != data ){
num id = data['id'];
var account = data['account'];
var nickName = data['nickName'];
var profile = data['profile'];

var sengledWebsite = data['sengledWebsite'];
var privacyPolicyStatus = data['privacyPolicyStatus'];
var upgradeAppType = data['upgradeAppType'];

User userInfo = new User(
id:id,
name:nickName,
avatar:profile,
account:account);

new UserHelper().setUser(userInfo) ;
DataUtils.saveUserInfo(userInfo);

Navigator.of(context).pushReplacementNamed('/HomePage');
}
} else {
Navigator.of(context).pushReplacementNamed('/LoginPage');
}

}

}

Future initData() async {
String session = await new DataHelper().getSession();
if (null != session){
getUserInfo();
}else {
Navigator.of(context).pushReplacementNamed('/LoginPage');
}
}

}

主页面 设置路由
main.dart

1
2
3
4
5
6
7
8
9
10
11
12
void main() {
runApp(
new MaterialApp(
title: 'Snap',
home: new SplashPage(), // 闪屏页,
routes: <String, WidgetBuilder>{ // 路由
'/LoginPage': (BuildContext context) => new LoginPage(),
'/HomePage': (BuildContext context) => new MainPage(),
},
),
);
}

问题

  1. 同步 异步问题
    1
    2
    3
    String  session =   new DataHelper().getSession();

    没有 await 第一次 session 一直 获取不到
支付宝打赏 微信打赏

打赏