UserChange.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import 'package:flutter/material.dart';
  2. import 'package:file_picker/file_picker.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import 'package:dio/dio.dart';
  6. import 'package:intl/intl.dart';
  7. import 'package:jpush_flutter/jpush_flutter.dart';
  8. import 'package:package_info/package_info.dart';
  9. import 'package:shared_preferences/shared_preferences.dart';
  10. import 'package:flutter_bugly/flutter_bugly.dart';
  11. import './loginFirst.dart';
  12. import '../styles/colors.dart';
  13. import 'dart:io';
  14. import 'dart:async';
  15. import 'dart:ui';
  16. import '../styles/totast.dart';
  17. import 'ChangeUserInfo.dart';
  18. import 'package:redux/redux.dart';
  19. import 'package:flutter_redux/flutter_redux.dart';
  20. import '../redux/AppState.dart';
  21. import '../model/UserInfo.dart';
  22. import '../net/HttpManager.dart';
  23. import '../net/Result.dart';
  24. import '../redux/UserRedux.dart';
  25. import 'package:image_cropper/image_cropper.dart';
  26. import '../widget/CheckUpgrade.dart';
  27. class UserChange extends StatefulWidget {
  28. @override
  29. UserChangeState createState() => UserChangeState();
  30. }
  31. class UserChangeState extends State<UserChange> {
  32. String version = '';
  33. bool receiveMsg = false;
  34. final GlobalKey<UpgradeDialogState> _dialogKey = GlobalKey();
  35. @override
  36. void initState() {
  37. super.initState();
  38. PackageInfo.fromPlatform().then((packageInfo) {
  39. setState(() {
  40. version = '当前版本:' + packageInfo.version;
  41. });
  42. });
  43. }
  44. @override
  45. Widget build(BuildContext context) {
  46. ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
  47. return StoreConnector<AppState, UserInfo>(
  48. converter: (Store store) => store.state.userInfo,
  49. builder: (context, userInfo) {
  50. return WillPopScope(
  51. key: Key('UserChangesPage'),
  52. child: Scaffold(
  53. appBar: AppBar(
  54. title: Text('个人设置'),
  55. centerTitle: true,
  56. elevation: 0,
  57. ),
  58. body: Stack(
  59. children: <Widget>[
  60. RefreshIndicator(
  61. color: PRIMARY_COLOR,
  62. backgroundColor: Colors.white,
  63. displacement: 10,
  64. onRefresh: () async {
  65. await Future.delayed(const Duration(seconds: 1));
  66. getUserInfo();
  67. },
  68. child: SizedBox.expand(
  69. child: SingleChildScrollView(
  70. physics: AlwaysScrollableScrollPhysics(),
  71. child: Column(
  72. children: <Widget>[
  73. _sectionDivier(),
  74. _section([
  75. _cell(
  76. '头像',
  77. ClipOval(
  78. child: Image.network(userInfo.icon,
  79. width: 36, height: 36),
  80. ),
  81. onTap: () {
  82. _chooseImage();
  83. },
  84. ),
  85. _cell(
  86. '昵称',
  87. userInfo.nickname,
  88. placeholder: '请填写昵称',
  89. onTap: () {
  90. Navigator.push(
  91. context,
  92. CupertinoPageRoute(
  93. builder: (context) =>
  94. ChangeUserInfo(
  95. title: '昵称',
  96. val: userInfo.nickname)));
  97. },
  98. ),
  99. _cell(
  100. '性别',
  101. userInfo.sex,
  102. placeholder: '请添加性别信息',
  103. onTap: () {
  104. _chooseSex(context);
  105. },
  106. ),
  107. _cell(
  108. '生日',
  109. DateFormat('yyyy-MM-dd').format(
  110. DateTime.fromMillisecondsSinceEpoch(
  111. userInfo.birthday),
  112. ),
  113. placeholder: '选择生日',
  114. onTap: () {
  115. _chooseBirthday(context);
  116. },
  117. ),
  118. _cell('手机', userInfo.phone, onTap: () {
  119. Navigator.push(
  120. context,
  121. CupertinoPageRoute(
  122. builder: (context) => ChangeUserInfo(
  123. title: '手机号',
  124. val: userInfo.phone)));
  125. }),
  126. _cell('检查更新', version, showBorder: false,
  127. onTap: () async {
  128. Toast.show(context, '检查更新中', -1, 'loading');
  129. final upgradeInfo =
  130. await FlutterBugly.getUpgradeInfo();
  131. Toast.hide();
  132. if (upgradeInfo != null) {
  133. showUpgradeDialog(
  134. context, upgradeInfo, _dialogKey);
  135. } else {
  136. Toast.show(context, '已是最新版本', 1500, 'info');
  137. }
  138. })
  139. ]),
  140. ],
  141. ),
  142. ),
  143. ),
  144. ),
  145. Positioned(
  146. bottom: 10,
  147. right: 15,
  148. left: 15,
  149. height: 48,
  150. child: FlatButton(
  151. textColor: Colors.white,
  152. color: PRIMARY_COLOR,
  153. highlightColor: Color(0xFF763434),
  154. child: Text(
  155. '退出登录',
  156. style: TextStyle(
  157. fontSize: 16, fontWeight: FontWeight.normal),
  158. ),
  159. onPressed: () async {
  160. Toast.show(context, '退出成功', 1500, 'success');
  161. Navigator.push(
  162. context,
  163. MaterialPageRoute(
  164. builder: (context) => LoginFirst()));
  165. final prefs = await SharedPreferences.getInstance();
  166. prefs.remove('token');
  167. JPush jpush = JPush();
  168. jpush.deleteAlias();
  169. // Future.delayed(const Duration(seconds: 1), () {
  170. // StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(null));
  171. // });
  172. }),
  173. )
  174. ],
  175. ),
  176. ),
  177. onWillPop: () {
  178. Toast.hide();
  179. Navigator.pop(context);
  180. return Future.value(false);
  181. });
  182. });
  183. }
  184. Future<void> updateUserInfo(userInfo, value, key) async {
  185. if (Toast.preToast == null) {
  186. Toast.show(context, '加载中', -1, 'loading');
  187. }
  188. final Result res = await HttpManager.post('userInfo/update',
  189. data: {'id': userInfo.id, key: value});
  190. Toast.hide();
  191. if (res.success) {
  192. Toast.show(context, '修改成功', 1500, 'success');
  193. getUserInfo();
  194. } else {
  195. Toast.show(context, res.error, 1500, 'info');
  196. }
  197. }
  198. Future<void> getUserInfo() async {
  199. Result res = await HttpManager.get('userInfo/getUserInfo');
  200. if (res.success) {
  201. StoreProvider.of<AppState>(context)
  202. .dispatch(UpdateUserAction(UserInfo.fromJson(res.data)));
  203. } else {}
  204. }
  205. Future<void> _chooseSex(BuildContext context) async {
  206. String sex = await showCupertinoModalPopup(
  207. context: context,
  208. builder: (BuildContext context) {
  209. return CupertinoActionSheet(
  210. title: Text('选择性别'),
  211. actions: <Widget>[
  212. CupertinoActionSheetAction(
  213. child: Text('男'),
  214. onPressed: () {
  215. Navigator.pop(context, '男');
  216. },
  217. ),
  218. CupertinoActionSheetAction(
  219. child: Text('女'),
  220. onPressed: () {
  221. Navigator.pop(context, '女');
  222. },
  223. )
  224. ],
  225. cancelButton: CupertinoDialogAction(
  226. child: const Text('取消'),
  227. onPressed: () {
  228. Navigator.pop(context);
  229. },
  230. ),
  231. );
  232. });
  233. if (sex != null) {
  234. updateUserInfo(
  235. StoreProvider.of<AppState>(context).state.userInfo, sex, 'sex');
  236. }
  237. }
  238. Future<void> _chooseBirthday(BuildContext context) async {
  239. UserInfo userInfo = StoreProvider.of<AppState>(context).state.userInfo;
  240. DateTime date = userInfo.birthday > 0
  241. ? DateTime.fromMillisecondsSinceEpoch(userInfo.birthday)
  242. : DateTime.now();
  243. DateTime res = await showCupertinoModalPopup<DateTime>(
  244. context: context,
  245. builder: (BuildContext context) {
  246. return Container(
  247. height: 216,
  248. color: CupertinoColors.white,
  249. child: Column(
  250. children: <Widget>[
  251. SizedBox(
  252. height: 44,
  253. child: Stack(
  254. children: <Widget>[
  255. Positioned(
  256. left: 0,
  257. top: 0,
  258. bottom: 0,
  259. child: CupertinoButton(
  260. padding: EdgeInsets.fromLTRB(15, 0, 0, 0),
  261. child: Text(
  262. '取消',
  263. style: TextStyle(color: PRIMARY_COLOR),
  264. ),
  265. onPressed: () {
  266. Navigator.of(context).pop();
  267. },
  268. ),
  269. ),
  270. Positioned(
  271. right: 0,
  272. top: 0,
  273. bottom: 0,
  274. child: CupertinoButton(
  275. padding: EdgeInsets.fromLTRB(0, 0, 15, 0),
  276. child: Text(
  277. '确定',
  278. style: TextStyle(color: PRIMARY_COLOR),
  279. ),
  280. onPressed: () {
  281. Navigator.of(context).pop(date);
  282. },
  283. ),
  284. )
  285. ],
  286. ),
  287. ),
  288. Expanded(
  289. flex: 1,
  290. child: DefaultTextStyle(
  291. style: const TextStyle(
  292. color: CupertinoColors.black,
  293. fontSize: 22.0,
  294. ),
  295. child: GestureDetector(
  296. // Blocks taps from propagating to the modal sheet and popping.
  297. onTap: () {},
  298. child: SafeArea(
  299. top: false,
  300. child: CupertinoDatePicker(
  301. mode: CupertinoDatePickerMode.date,
  302. initialDateTime: date,
  303. onDateTimeChanged: (DateTime newDateTime) {
  304. date = newDateTime;
  305. },
  306. ),
  307. ),
  308. ),
  309. ),
  310. )
  311. ],
  312. ),
  313. );
  314. },
  315. );
  316. if (res != null) {
  317. updateUserInfo(userInfo, res.millisecondsSinceEpoch, 'birthday');
  318. }
  319. }
  320. Future<void> _chooseImage() async {
  321. String path = await FilePicker.getFilePath(type: FileType.IMAGE);
  322. if (path == null) {
  323. return;
  324. }
  325. File croppedFile = await ImageCropper.cropImage(
  326. sourcePath: path,
  327. ratioX: 1.0,
  328. ratioY: 1.0,
  329. maxWidth: 512,
  330. maxHeight: 512,
  331. toolbarColor: PRIMARY_COLOR,
  332. );
  333. if (croppedFile == null) {
  334. return;
  335. }
  336. Toast.show(context, '加载中', -1, 'loading');
  337. Result res = await HttpManager.post('assets/uploadFile', data: {
  338. 'file': UploadFileInfo(croppedFile, croppedFile.path),
  339. });
  340. Toast.hide();
  341. if (res.success) {
  342. updateUserInfo(StoreProvider.of<AppState>(context).state.userInfo,
  343. res.data[0], 'icon');
  344. }
  345. }
  346. Widget _sectionDivier() {
  347. return SizedBox(height: 10);
  348. }
  349. Widget _section(List<Widget> children) {
  350. return Container(
  351. color: SUB_COLOR,
  352. child: Column(
  353. children: children,
  354. ),
  355. );
  356. }
  357. Widget _cell(String title, dynamic child,
  358. {String placeholder, void Function() onTap, bool showBorder = true}) {
  359. Widget secondChild;
  360. if (child == null || (child is String && child.isEmpty)) {
  361. secondChild = Text(
  362. placeholder ?? '',
  363. style: TextStyle(color: PLACEHOLDER_COLOR, fontSize: 13),
  364. );
  365. } else if (child is String) {
  366. secondChild = Text(
  367. child ?? '',
  368. style: TextStyle(color: Colors.white, fontSize: 15),
  369. );
  370. } else {
  371. secondChild = child;
  372. }
  373. return GestureDetector(
  374. onTap: onTap,
  375. child: Container(
  376. height: 60,
  377. padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
  378. child: Container(
  379. decoration: BoxDecoration(
  380. border: Border(
  381. bottom: BorderSide(
  382. color: Color(0x2E000000),
  383. width: showBorder ? 1 : 0,
  384. ),
  385. ),
  386. ),
  387. child: Row(
  388. mainAxisAlignment: MainAxisAlignment.center,
  389. children: <Widget>[
  390. Expanded(
  391. child: Text(
  392. title ?? '',
  393. style: TextStyle(color: Colors.white, fontSize: 14),
  394. ),
  395. ),
  396. secondChild,
  397. Padding(
  398. padding: EdgeInsets.only(left: 2),
  399. child: Image.asset(
  400. 'images/icon_inter.png',
  401. width: 24,
  402. height: 24,
  403. ),
  404. )
  405. ],
  406. ),
  407. ),
  408. ),
  409. );
  410. }
  411. }