MyInfoPage.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_osc/constants/Constants.dart';
  3. import 'package:flutter_osc/events/LoginEvent.dart';
  4. import '../pages/CommonWebPage.dart';
  5. import '../pages/LoginPage.dart';
  6. import 'dart:convert';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. import '../api/Api.dart';
  9. import '../util/NetUtils.dart';
  10. import '../util/DataUtils.dart';
  11. import '../model/UserInfo.dart';
  12. class MyInfoPage extends StatefulWidget {
  13. @override
  14. State<StatefulWidget> createState() {
  15. return MyInfoPageState();
  16. }
  17. }
  18. class MyInfoPageState extends State<MyInfoPage> {
  19. static const double IMAGE_ICON_WIDTH = 30.0;
  20. static const double ARROW_ICON_WIDTH = 16.0;
  21. var titles = ["我的消息", "阅读记录", "我的博客", "我的问答", "我的活动", "我的团队", "邀请好友"];
  22. var imagePaths = [
  23. "images/ic_my_message.png",
  24. "images/ic_my_blog.png",
  25. "images/ic_my_blog.png",
  26. "images/ic_my_question.png",
  27. "images/ic_discover_pos.png",
  28. "images/ic_my_team.png",
  29. "images/ic_my_recommend.png"
  30. ];
  31. var icons = [];
  32. var userAvatar;
  33. var userName;
  34. var titleTextStyle = TextStyle(fontSize: 16.0);
  35. var rightArrowIcon = Image.asset(
  36. 'images/ic_arrow_right.png',
  37. width: ARROW_ICON_WIDTH,
  38. height: ARROW_ICON_WIDTH,
  39. );
  40. MyInfoPageState() {
  41. for (int i = 0; i < imagePaths.length; i++) {
  42. icons.add(getIconImage(imagePaths[i]));
  43. }
  44. }
  45. @override
  46. void initState() {
  47. super.initState();
  48. _showUserInfo();
  49. Constants.eventBus.on().listen((event) {
  50. // 收到退出登录的消息,刷新个人信息显示
  51. _showUserInfo();
  52. });
  53. Constants.eventBus.on().listen((event) {
  54. // 收到登录的消息,重新获取个人信息
  55. getUserInfo();
  56. });
  57. }
  58. _showUserInfo() {
  59. DataUtils.getUserInfo().then((UserInfo userInfo) {
  60. if (userInfo != null) {
  61. print(userInfo.name);
  62. print(userInfo.avatar);
  63. setState(() {
  64. userAvatar = userInfo.avatar;
  65. userName = userInfo.name;
  66. });
  67. } else {
  68. setState(() {
  69. userAvatar = null;
  70. userName = null;
  71. });
  72. }
  73. });
  74. }
  75. Widget getIconImage(path) {
  76. return Padding(
  77. padding: const EdgeInsets.fromLTRB(0.0, 0.0, 10.0, 0.0),
  78. child: Image.asset(path,
  79. width: IMAGE_ICON_WIDTH, height: IMAGE_ICON_WIDTH),
  80. );
  81. }
  82. @override
  83. Widget build(BuildContext context) {
  84. var listView = ListView.builder(
  85. itemCount: titles.length * 2,
  86. itemBuilder: (context, i) => renderRow(i),
  87. );
  88. return listView;
  89. }
  90. // 获取用户信息
  91. getUserInfo() async {
  92. SharedPreferences sp = await SharedPreferences.getInstance();
  93. String accessToken = sp.get(DataUtils.SP_AC_TOKEN);
  94. Map<String, String> params = Map();
  95. params['access_token'] = accessToken;
  96. NetUtils.get(Api.USER_INFO, params: params).then((data) {
  97. if (data != null) {
  98. var map = json.decode(data);
  99. setState(() {
  100. userAvatar = map['avatar'];
  101. userName = map['name'];
  102. });
  103. DataUtils.saveUserInfo(map);
  104. }
  105. });
  106. }
  107. _login() async {
  108. final result = await Navigator
  109. .of(context)
  110. .push(MaterialPageRoute(builder: (context) {
  111. return LoginPage();
  112. }));
  113. if (result != null && result == "refresh") {
  114. // 刷新用户信息
  115. getUserInfo();
  116. // 通知动弹页面刷新
  117. Constants.eventBus.fire(LoginEvent());
  118. }
  119. }
  120. _showUserInfoDetail() {}
  121. renderRow(i) {
  122. if (i == 0) {
  123. var avatarContainer = Container(
  124. color: const Color(0xff63ca6c),
  125. height: 200.0,
  126. child: Center(
  127. child: Column(
  128. mainAxisAlignment: MainAxisAlignment.center,
  129. children: [
  130. userAvatar == null
  131. ? Image.asset(
  132. "images/ic_avatar_default.png",
  133. width: 60.0,
  134. )
  135. : Container(
  136. width: 60.0,
  137. height: 60.0,
  138. decoration: BoxDecoration(
  139. shape: BoxShape.circle,
  140. color: Colors.transparent,
  141. image: DecorationImage(
  142. image: NetworkImage(userAvatar),
  143. fit: BoxFit.cover),
  144. border: Border.all(
  145. color: Colors.white,
  146. width: 2.0,
  147. ),
  148. ),
  149. ),
  150. Text(
  151. userName == null ? "点击头像登录" : userName,
  152. style: TextStyle(color: Colors.white, fontSize: 16.0),
  153. ),
  154. ],
  155. ),
  156. ),
  157. );
  158. return GestureDetector(
  159. onTap: () {
  160. DataUtils.isLogin().then((isLogin) {
  161. if (isLogin) {
  162. // 已登录,显示用户详细信息
  163. _showUserInfoDetail();
  164. } else {
  165. // 未登录,跳转到登录页面
  166. _login();
  167. }
  168. });
  169. },
  170. child: avatarContainer,
  171. );
  172. }
  173. --i;
  174. if (i.isOdd) {
  175. return Divider(
  176. height: 1.0,
  177. );
  178. }
  179. i = i ~/ 2;
  180. String title = titles[i];
  181. var listItemContent = Padding(
  182. padding: const EdgeInsets.fromLTRB(10.0, 15.0, 10.0, 15.0),
  183. child: Row(
  184. children: [
  185. icons[i],
  186. Expanded(
  187. child: Text(
  188. title,
  189. style: titleTextStyle,
  190. )),
  191. rightArrowIcon
  192. ],
  193. ),
  194. );
  195. return InkWell(
  196. child: listItemContent,
  197. onTap: () {
  198. _handleListItemClick(title);
  199. // Navigator
  200. // .of(context)
  201. // .push(MaterialPageRoute(builder: (context) => CommonWebPage(title: "Test", url: "https://my.oschina.net/u/815261/blog")));
  202. },
  203. );
  204. }
  205. _showLoginDialog() {
  206. showDialog(
  207. context: context,
  208. builder: (BuildContext ctx) {
  209. return AlertDialog(
  210. title: Text('提示'),
  211. content: Text('没有登录,现在去登录吗?'),
  212. actions: <Widget>[
  213. FlatButton(
  214. child: Text(
  215. '取消',
  216. style: TextStyle(color: Colors.red),
  217. ),
  218. onPressed: () {
  219. Navigator.of(context).pop();
  220. },
  221. ),
  222. FlatButton(
  223. child: Text(
  224. '确定',
  225. style: TextStyle(color: Colors.blue),
  226. ),
  227. onPressed: () {
  228. Navigator.of(context).pop();
  229. _login();
  230. },
  231. )
  232. ],
  233. );
  234. });
  235. }
  236. _handleListItemClick(String title) {
  237. DataUtils.isLogin().then((isLogin) {
  238. if (!isLogin) {
  239. // 未登录
  240. _showLoginDialog();
  241. } else {
  242. DataUtils.getUserInfo().then((info) {
  243. Navigator.of(context).push(
  244. MaterialPageRoute(
  245. builder: (context) => CommonWebPage(
  246. title: "我的博客",
  247. url: "https://my.oschina.net/u/${info.id}/blog"
  248. )
  249. ));
  250. });
  251. }
  252. });
  253. }
  254. }