profile_page.dart主界面使用EventBus登录成功以后跳转的逻辑中使用_getUerInfo来获取到用户的相关信息
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutterapp2/common/event_bus.dart'; import 'package:flutterapp2/constants/Constants.dart'; import 'package:flutterapp2/utils/data_utils.dart'; import 'package:flutterapp2/utils/net_utils.dart'; import 'login_web_page.dart'; /** * 主界面My */ class ProfilePage extends StatefulWidget { @override _ProfilePageState createState() => _ProfilePageState(); } class _ProfilePageState extends State<ProfilePage> { List menuTitles = [ '我的消息', '阅读记录', '我的博客', '我的问答', '我的活动', '我的团队', '邀请好友', ]; List menuIcons = [ Icons.message, Icons.print, Icons.error, Icons.phone, Icons.send, Icons.people, Icons.person, ]; String userAvatar; String userName; @override void initState() { // TODO: implement initState super.initState(); //尝试显示用户信息 _showUerInfo(); eventBus.on<LoginEvent>().listen((event) { //TODO //获取用户信息并显示 _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} 2020-10-09 00:46:36.478 24825-24927/com.example.flutterapp2 I/flutter: Debug profile page LoginEvent 2020-10-09 00:46:36.518 24825-24927/com.example.flutterapp2 I/flutter: Debug accessToken: 21b8d7d0-6bef-469f-ba64-033d47387d50 2020-10-09 00:46:36.518 24825-24927/com.example.flutterapp2 I/flutter: Debug NetUtils : https://www.oschina.net/action/openapi/user?access_token=21b8d7d0-6bef-469f-ba64-033d47387d50&dataType=json 2020-10-09 00:46:36.690 24825-24927/com.example.flutterapp2 I/flutter: Debug LoginWebPage onUrlChanged: https://www.dongnaoedu.com/?code=4GAc05&state=# 2020-10-09 00:46:36.691 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.915 24825-24927/com.example.flutterapp2 I/flutter: Debug data: {"gender":"male","name":"Augfun","location":"广东 深圳","id":4571926,"avatar":"https://static.oschina.net/uploads/user/2285/4571926_50.jpg?t=1593452705000","email":"123456@outlook.com","url":"https://my.oschina.net/u/4571926"} 2020-10-09 00:46:37.071 24825-24927/com.example.flutterapp2 I/flutter: Debug this is login_web_page: {"error_description":"Invalid authorization code: 4GAc05","error":"400"}
