setting.dart 14 KB

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