my_info_page.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_osc/model/api.dart';
  3. import 'package:flutter_osc/model/constants.dart';
  4. import 'package:flutter_osc/events/LoginEvent.dart';
  5. import 'package:flutter_osc/model/user_info.dart';
  6. import 'package:flutter_osc/pages/CommonWebPage.dart';
  7. import 'package:flutter_osc/pages/login_page.dart';
  8. import 'package:flutter_osc/util/DataUtils.dart';
  9. import 'package:flutter_osc/util/NetUtils.dart';
  10. import 'dart:convert';
  11. import 'package:shared_preferences/shared_preferences.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:
  79. Image.asset(path, 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) as String?;
  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 =
  109. await Navigator.of(context).push(MaterialPageRoute(builder: (context) {
  110. return LoginPage();
  111. }));
  112. if (result != null && result == "refresh") {
  113. // 刷新用户信息
  114. getUserInfo();
  115. // 通知动弹页面刷新
  116. Constants.eventBus.fire(LoginEvent());
  117. }
  118. }
  119. _showUserInfoDetail() {}
  120. renderRow(i) {
  121. if (i == 0) {
  122. var avatarContainer = Container(
  123. color: const Color(0xff63ca6c),
  124. height: 200.0,
  125. child: Center(
  126. child: Column(
  127. mainAxisAlignment: MainAxisAlignment.center,
  128. children: [
  129. userAvatar == null
  130. ? Image.asset(
  131. "images/ic_avatar_default.png",
  132. width: 60.0,
  133. )
  134. : Container(
  135. width: 60.0,
  136. height: 60.0,
  137. decoration: BoxDecoration(
  138. shape: BoxShape.circle,
  139. color: Colors.transparent,
  140. image: DecorationImage(
  141. image: NetworkImage(userAvatar), fit: BoxFit.cover),
  142. border: Border.all(
  143. color: Colors.white,
  144. width: 2.0,
  145. ),
  146. ),
  147. ),
  148. Text(
  149. userName == null ? "点击头像登录" : userName,
  150. style: TextStyle(color: Colors.white, fontSize: 16.0),
  151. ),
  152. ],
  153. ),
  154. ),
  155. );
  156. return GestureDetector(
  157. onTap: () {
  158. DataUtils.isLogin().then((isLogin) {
  159. if (isLogin) {
  160. // 已登录,显示用户详细信息
  161. _showUserInfoDetail();
  162. } else {
  163. // 未登录,跳转到登录页面
  164. _login();
  165. }
  166. });
  167. },
  168. child: avatarContainer,
  169. );
  170. }
  171. --i;
  172. if (i.isOdd) {
  173. return Divider(
  174. height: 1.0,
  175. );
  176. }
  177. i = i ~/ 2;
  178. String title = titles[i];
  179. var listItemContent = Padding(
  180. padding: const EdgeInsets.fromLTRB(10.0, 15.0, 10.0, 15.0),
  181. child: Row(
  182. children: [
  183. icons[i],
  184. Expanded(
  185. child: Text(
  186. title,
  187. style: titleTextStyle,
  188. )),
  189. rightArrowIcon
  190. ],
  191. ),
  192. );
  193. return InkWell(
  194. child: listItemContent,
  195. onTap: () {
  196. _handleListItemClick(title);
  197. // Navigator
  198. // .of(context)
  199. // .push(MaterialPageRoute(builder: (context) => CommonWebPage(title: "Test", url: "https://my.oschina.net/u/815261/blog")));
  200. },
  201. );
  202. }
  203. _showLoginDialog() {
  204. showDialog(
  205. context: context,
  206. builder: (BuildContext ctx) {
  207. return AlertDialog(
  208. title: Text('提示'),
  209. content: Text('没有登录,现在去登录吗?'),
  210. actions: <Widget>[
  211. TextButton(
  212. child: Text(
  213. '取消',
  214. style: TextStyle(color: Colors.red),
  215. ),
  216. onPressed: () {
  217. Navigator.of(context).pop();
  218. },
  219. ),
  220. TextButton(
  221. child: Text(
  222. '确定',
  223. style: TextStyle(color: Colors.blue),
  224. ),
  225. onPressed: () {
  226. Navigator.of(context).pop();
  227. _login();
  228. },
  229. )
  230. ],
  231. );
  232. });
  233. }
  234. _handleListItemClick(String title) {
  235. DataUtils.isLogin().then((isLogin) {
  236. if (!isLogin) {
  237. // 未登录
  238. _showLoginDialog();
  239. } else {
  240. DataUtils.getUserInfo().then((info) {
  241. Navigator.of(context).push(MaterialPageRoute(
  242. builder: (context) => CommonWebPage(
  243. title: "我的博客",
  244. url: "https://my.oschina.net/u/${info!.id}/blog")));
  245. });
  246. }
  247. });
  248. }
  249. }