Flutter33
//获取用户信息并显示
_getUerInfo();
});
eventBus.on<LogoutEvent>().listen((event) {
//TODO
});
}
_getUerInfo() {
DataUtils.getAccessToken().then((accessToken){
if (accessToken == null || accessToken.length == 0) {
return;
}
Map<String, dynamic> params = Map<String, dynamic>();
params['access_token'] = accessToken;
params['dataType'] = 'json';
print('Debug accessToken: $accessToken');
NetUtils.get(AppUrls.OPENAPI_USER, params).then((data) {
//{"gender":"male","name":"Damon2019","location":"湖南 长沙","id":2006874,"avatar":"https://oscimg.oschina.net/oscnet/up-21zvuaor7bbvi8h2a4g93iv9vve2wrnz.jpg!/both/50x50?t=1554975223000","email":"3262663349@qq.com","url":"https://my.oschina.net/damon007"}
//data: {"gender":"male","name":"Augfun","location":"广东 深圳","id":4571926,"avatar":"https://static.oschina.net/uploads/user/2285/4571926_50.jpg?t=1593452705000","email":"1234556@outlook.com","url":"https://my.oschina.net/u/4571926"}
print('Debug data: $data');
// Map<String, dynamic> map = json.decode(data);
// if (mounted) {
// setState(() {
// userAvatar = map['avatar'];
// userName = map['name'];
// });
// }
// DataUtils.saveUserInfo(map);
});
});
}
_showUerInfo() {
}
@override
Widget build(BuildContext context) {
return ListView.separated(
itemBuilder: (context, index) {
//My 界面的头部
if(index == 0){
//头像用 Container 装起来
return _buildHeader();
}
index -= 1;
return ListTile(
leading: Icon(menuIcons[index]),
title: Text(menuTitles[index]),
trailing: Icon(Icons.arrow_forward_ios),//尾巴
onTap: () {
_login();
},
);
},
//分割线
separatorBuilder: (context, index) {
return Divider();
},
itemCount: menuTitles.length + 1
);
}
_login() async {
final result = await Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => LoginWebPage()));
if (result != null && result == 'refresh') {
print('Debug profile page LoginEvent');
//登录成功
eventBus.fire(LoginEvent());
}
}
Container _buildHeader() {
return Container(
height: 150.0,
color: Color(AppColors.APP_THEME),
//头像的布局填充
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
child: Container(
width: 60.0,
height: 60.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Color(0xffffffff),
width: 2.0,
),
image: DecorationImage(
//加载网路图片
image: AssetImage('assets/images/ic_avatar_default.png'),
fit: BoxFit.cover,
),
),
),
onTap: () {
//执行登录
_login();
},
),
SizedBox(
),
Text(
'点击头像登录',
style: TextStyle(color: Color(0xffffffff)),
),
],
),
),
);
}
}
相关数据打印如下:
2020-10-09 00:46:28.516 24825-24927/com.example.flutterapp2 I/flutter: Debug LoginWebPage onUrlChanged: https://www.oschina.net/action/oauth2/authorize?response_type=code&client_id=6ZmjMJ4ZCW7YRhQ4sm42&redirect_uri=https://www.dongnaoedu.com/
2020-10-09 00:46:35.885 24825-24927/com.example.flutterapp2 I/flutter: Debug LoginWebPage onUrlChanged: https://www.dongnaoedu.com/?code=4GAc05&state=#
2020-10-09 00:46:35.893 24825-24927/com.example.flutterapp2 I/flutter: Debug NetUtils : https://www.oschina.net/action/openapi/token?client_id=6ZmjMJ4ZCW7YRhQ4sm42&client_secret=sXWCxyV1KegoF2gethYuBZhI8WQI9fjk&grant_type=authorization_code&redirect_uri=https://www.dongnaoedu.com/&code=4GAc05&dataType=json
2020-10-09 00:46:36.465 24825-24927/com.example.flutterapp2 I/flutter: Debug this is login_web_page: {"access_token":"21b8d7d0-6bef-469f-ba64-033d47387d50","refresh_token":"45545f98-d72c-4b3e-b6bd-f2f037f0a661","uid":4571926,"token_type":"bearer","expires_in":600473}
评论