UserChange.dart 14 KB

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