UserChange.dart 13 KB

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