Flutter37,互联网行业“中年”危机
作者:互联网
‘我的团队’,
‘邀请好友’,
];
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().listen((event) {
//获取用户信息并显示
_getUerInfo();
});
eventBus.on().listen((event) {
//登出需要刷新用户信息
_showUerInfo();
});
}
_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() {
//userName信息和头像信息更新
DataUtils.getUserInfo().then((user) {
if (mounted) {
setState(() {
if (user != null) {
userAvatar = user.avatar;
userName = user.name;
} else {
userAvatar = null;
userName = null;
}
});
}
});
}
@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: () {
DataUtils.isLogin().then((isLogin){
if(isLogin) {
switch (index) {
case 0:
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => MyMessagePage()));
break;
}
} else {
_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(Logi
nEvent());
}
}
Container _buildHeader() {
return Container(
height: 150.0,
color: Color(AppColors.APP_THEME),
//头像的布局填充
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
//先判断网路图片是否为空
child: userAvatar != null
? Container(
width: 60.0,
height: 60.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Color(0xffffffff),
width: 2.0,
),
image: DecorationImage(
//加载网路图片
image: NetworkImage(userAvatar),
fit: BoxFit.cover,
),
),
)
-
//加载网路图片
- Image.asset(‘assets/images/ic_avatar_default.png’,
width: 60.0,
height: 60.0,
),
onTap: () {
//执行登录
DataUtils.isLogin().then((isLogin){
if(isLogin){
//点击以后跳转到详情
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ProfileDetailPage()));
}else{
//执行登录
_login();
}
});
},
),
SizedBox(
),
Text(
userName ??= ‘点击头像登录’,
style: TextStyle(color: Color(0xffffffff)),
),
],
),
),
);
}
}
profile_detail_page.dart
import ‘dart:convert’;
import ‘package:flutter/cupertino.dart’;
import ‘package:flutter/material.dart’;
import ‘package:flutterapp2/constants/Constants.dart’;
import ‘package:flutterapp2/models/user_info.dart’;
import ‘package:flutterapp2/utils/data_utils.dart’;
import ‘package:flutterapp2/utils/net_utils.dart’;
/**
- 用户详情界面
*/
class ProfileDetailPage extends StatefulWidget {
@override
_ProfileDetailPageState createState() => _ProfileDetailPageState();
}
class _ProfileDetailPageState extends State {
UserInfo _userInfo;
//获取用户信息
_getDetailInfo() {
DataUtils.getAccessToken().then((accessToken) {
//Debug accessToken: 21b8d7d0-6bef-469f-ba64-033d47387d50
print(‘Debug accessToken: $accessToken’);
//拼装请求
Map<String, dynamic> params = Map<String, dynamic>();
params[‘dataType’] = ‘json’;
params[‘access_token’] = accessToken;
NetUtils.get(AppUrls.MY_INFORMATION, params).then((data) {
// {“gender”:1,“joinTime”:“2014-09-03 10:01:47”,“city”:“长沙”,“fansCount”:0,“portrait”:“https://www.oschina.net/img/portrait.gif”,“expertise”:[“手机软件开发”,“软件开发管理”],“platforms”:[“Android”,“C/C++”],“uid”:2006874,“lastLoginTime”:“2019-04-09 22:33:03”,“province”:“湖南”,“name”:“Damon2019”,“followersCount”:0,“favoriteCount”:0,“notice”:{“referCount”:0,“replyCount”:0,“msgCount”:0,“fansCount”:0}}
//Debug MY_INFORMATION: {“gender”:1,“joinTime”:“2020-06-30 01:45:04”,“city”:“深圳”,“fansCount”:0,“portrait”:“https://static.oschina.net/uploads/user/2285/4571926_50.jpg?t=1593452705000”,“expertise”:[],“platforms”:[],“uid”:4571926,“lastLoginTime”:“2020-10-09 01:43:08”,“province”:“广东”,“name”:“Augfun”,“followersCount”:0,“favoriteCount”:0,“notice”:{“referCount”:0,“replyCount”:0,“msgCount”:1,“fansCount”:0}
print(‘Debug MY_INFORMATION: $data’);
if (data != null && data.isNotEmpty) {
Map<String, dynamic> map = json.decode(data);
UserInfo userInfo = UserInfo();
userInfo.uid = map[‘uid’];
userInfo.name = map[‘name’];
userInfo.gender = map[‘gender’];
userInfo.province = map[‘province’];
userInfo.city = map[‘city’];
userInfo.platforms = map[‘platforms’];
userInfo.expertise = map[‘expertise’];
userInfo.joinTime = map[‘joinTime’];
userInfo.lastLoginTime = map[‘lastLoginTime’];
userInfo.portrait = map[‘portrait’];
userInfo.fansCount = map[‘fansCount’];
userInfo.favoriteCount = map[‘favoriteCount’];
userInfo.followersCount = map[‘followersCount’];
userInfo.notice = map[‘notice’];
//刷新用户信息
setState(() {
_userInfo = userInfo;
});
}
});
});
}
@override
void initState() {
super.initState();
_getDetailInfo();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0.0,
title: Text(
‘我的资料’,
style: TextStyle(
color: Color(AppColors.APPBAR),
),
),
iconTheme: IconThemeData(color: Color(AppColors.APPBAR)),
),
body: buildSingleChildScrollView(),
);
}
Widget buildSingleChildScrollView() {
return SingleChildScrollView(
child: _userInfo == null
? Center(
child: CupertinoActivityIndicator(),
-
)
- Column(
children: [
InkWell(
onTap: () {
//TODO
},
child: Container(
margin: const EdgeInsets.only(left: 20.0),
padding: const EdgeInsets.only(
top: 10.0, bottom: 10.0, right: 20.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
‘头像’,
style: TextStyle(fontSize: 20.0),
),
Container(
width: 60.0,
height: 60.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.white,
width: 2.0,
),
image: DecorationImage(
image: NetworkImage(_userInfo.portrait),
fit: BoxFit.cover,
),
),
)
],
),
),
),
Divider(),
InkWell(
onTap: () {
//TODO
},
child: Container(
margin: const EdgeInsets.only(left: 20.0),
padding: const EdgeInsets.only(
top: 10.0, bottom: 10.0, right: 20.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
‘昵称’,
style: TextStyle(fontSize: 20.0),
0,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.white,
width: 2.0,
),
image: DecorationImage(
image: NetworkImage(_userInfo.portrait),
fit: BoxFit.cover,
),
),
)
],
),
),
),
Divider(),
InkWell(
onTap: () {
//TODO
},
child: Container(
margin: const EdgeInsets.only(left: 20.0),
padding: const EdgeInsets.only(
top: 10.0, bottom: 10.0, right: 20.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
‘昵称’,
style: TextStyle(fontSize: 20.0),
标签:map,accessToken,Flutter37,中年,互联网,userInfo,context,child,data 来源: https://blog.csdn.net/m0_66264533/article/details/122599191