UserChange.dart 14 KB

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