setting.dart 15 KB

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