setting.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 Setting extends StatefulWidget {
  26. @override
  27. SettingState createState() => SettingState();
  28. }
  29. class SettingState extends State<Setting> {
  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("SettingsPage"),
  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. _sectionDivier(),
  118. _section([
  119. _cell(
  120. '是否接收消息',
  121. Switch(
  122. value: userInfo.noticeFlag,
  123. onChanged: (val) {
  124. updateUserInfo(userInfo, val ? 'Y' : 'N', 'noticeFlag');
  125. }),
  126. showBorder: false)
  127. ]),
  128. _sectionDivier(),
  129. _section([
  130. _cell('检查更新', ' '),
  131. _cell('版本号', version, showBorder: false),
  132. ]),
  133. ],
  134. ),
  135. ),
  136. ),
  137. ),
  138. Positioned(
  139. bottom: 10,
  140. right: 15,
  141. left: 15,
  142. height: 48,
  143. child: FlatButton(
  144. textColor: Colors.white,
  145. color: PRIMARY_COLOR,
  146. highlightColor: Color(0xFF763434),
  147. child: Text(
  148. "退出登录",
  149. style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700),
  150. ),
  151. onPressed: () async {
  152. Toast.show(context, '退出成功', 1500, 'success');
  153. Navigator.push(context, new MaterialPageRoute(builder: (context) => new LoginFirst()));
  154. final prefs = await SharedPreferences.getInstance();
  155. prefs.remove('token');
  156. JPush jpush = JPush();
  157. jpush.deleteAlias();
  158. // Future.delayed(const Duration(seconds: 1), () {
  159. // StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(null));
  160. // });
  161. }),
  162. )
  163. ],
  164. ),
  165. ),
  166. onWillPop: () {
  167. Toast.hide();
  168. Navigator.pop(context);
  169. return Future.value(false);
  170. });
  171. });
  172. }
  173. void updateUserInfo(userInfo, value, key) async {
  174. if (Toast.preToast == null) {
  175. Toast.show(context, '加载中', -1, 'loading');
  176. }
  177. final Result res = await HttpManager.post('userInfo/update', data: {"id": userInfo.id, key: value});
  178. Toast.hide();
  179. if (res.success) {
  180. Toast.show(context, '修改成功', 1500, 'success');
  181. getUserInfo();
  182. } else {
  183. Toast.show(context, res.error, 1500, 'info');
  184. }
  185. }
  186. void getUserInfo() async {
  187. Result res = await HttpManager.get("userInfo/getUserInfo");
  188. if (res.success) {
  189. StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(UserInfo.fromJson(res.data)));
  190. } else {}
  191. }
  192. _chooseSex(BuildContext context) async {
  193. String sex = await showCupertinoModalPopup(
  194. context: context,
  195. builder: (BuildContext context) {
  196. return CupertinoActionSheet(
  197. title: Text('选择性别'),
  198. actions: <Widget>[
  199. CupertinoActionSheetAction(
  200. child: Text('男'),
  201. onPressed: () {
  202. Navigator.pop(context, '男');
  203. },
  204. ),
  205. CupertinoActionSheetAction(
  206. child: Text('女'),
  207. onPressed: () {
  208. Navigator.pop(context, '女');
  209. },
  210. ),
  211. CupertinoDialogAction(
  212. child: const Text('取消'),
  213. isDestructiveAction: true,
  214. onPressed: () {
  215. Navigator.pop(context);
  216. },
  217. )
  218. ],
  219. );
  220. });
  221. if (sex != null) {
  222. updateUserInfo(StoreProvider.of<AppState>(context).state.userInfo, sex, 'sex');
  223. }
  224. }
  225. _chooseBirthday(BuildContext context) async {
  226. UserInfo userInfo = StoreProvider.of<AppState>(context).state.userInfo;
  227. DateTime date = userInfo.birthday > 0 ? DateTime.fromMillisecondsSinceEpoch(userInfo.birthday) : DateTime.now();
  228. DateTime res = await showCupertinoModalPopup<DateTime>(
  229. context: context,
  230. builder: (BuildContext context) {
  231. return Container(
  232. height: 216,
  233. color: CupertinoColors.white,
  234. child: Column(
  235. children: <Widget>[
  236. SizedBox(
  237. height: 44,
  238. child: Stack(
  239. children: <Widget>[
  240. Positioned(
  241. left: 0,
  242. top: 0,
  243. bottom: 0,
  244. child: CupertinoButton(
  245. child: Text(
  246. '取消',
  247. style: TextStyle(color: PRIMARY_COLOR),
  248. ),
  249. onPressed: () {
  250. Navigator.of(context).pop();
  251. },
  252. ),
  253. ),
  254. Positioned(
  255. right: 0,
  256. top: 0,
  257. bottom: 0,
  258. child: CupertinoButton(
  259. child: Text(
  260. '确定',
  261. style: TextStyle(color: PRIMARY_COLOR),
  262. ),
  263. onPressed: () {
  264. Navigator.of(context).pop(date);
  265. },
  266. ),
  267. )
  268. ],
  269. ),
  270. ),
  271. Expanded(
  272. flex: 1,
  273. child: DefaultTextStyle(
  274. style: const TextStyle(
  275. color: CupertinoColors.black,
  276. fontSize: 22.0,
  277. ),
  278. child: GestureDetector(
  279. // Blocks taps from propagating to the modal sheet and popping.
  280. onTap: () {},
  281. child: SafeArea(
  282. top: false,
  283. child: CupertinoDatePicker(
  284. mode: CupertinoDatePickerMode.date,
  285. initialDateTime: date,
  286. onDateTimeChanged: (DateTime newDateTime) {
  287. date = newDateTime;
  288. },
  289. ),
  290. ),
  291. ),
  292. ),
  293. )
  294. ],
  295. ),
  296. );
  297. },
  298. );
  299. if (res != null) {
  300. updateUserInfo(userInfo, res.millisecondsSinceEpoch, 'birthday');
  301. }
  302. }
  303. _chooseImage(BuildContext context) async {
  304. UserInfo userInfo = StoreProvider.of<AppState>(context).state.userInfo;
  305. var image = await FilePicker.getFilePath(type: FileType.IMAGE);
  306. Toast.show(context, '加载中', -1, 'loading');
  307. File _image = File(image);
  308. _image.readAsBytes().then((bytes) async {
  309. String encoded1 = 'data:image/jpeg;base64,' + base64Encode(bytes);
  310. FormData formData = new FormData.from({"base64": encoded1});
  311. Result res = await HttpManager.post('assets/uploadImg', data: formData);
  312. if (res.success) {
  313. updateUserInfo(userInfo, res.data, 'icon');
  314. }
  315. });
  316. }
  317. Widget _sectionDivier() {
  318. return SizedBox(height: 10);
  319. }
  320. Widget _section(List<Widget> children) {
  321. return Container(
  322. color: CELL_COLOR,
  323. child: Column(
  324. children: children,
  325. ),
  326. );
  327. }
  328. Widget _cell(String title, dynamic child, {String placeholder, void Function() onTap, bool showBorder = true}) {
  329. Widget secondChild;
  330. if (child == null || (child is String && child.length == 0)) {
  331. secondChild = Text(
  332. placeholder ?? '',
  333. style: TextStyle(color: PLACEHOLDER_COLOR, fontSize: 13),
  334. );
  335. } else if (child is String) {
  336. secondChild = Text(
  337. child ?? '',
  338. style: TextStyle(color: Colors.white, fontSize: 15),
  339. );
  340. } else {
  341. secondChild = child;
  342. }
  343. return Container(
  344. height: 60,
  345. padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
  346. child: GestureDetector(
  347. onTap: onTap,
  348. child: Container(
  349. decoration: BoxDecoration(
  350. border: Border(
  351. bottom: BorderSide(
  352. color: Color(0x2E000000),
  353. width: showBorder ? 1 : 0,
  354. ),
  355. ),
  356. ),
  357. child: Row(
  358. mainAxisAlignment: MainAxisAlignment.center,
  359. children: <Widget>[
  360. Expanded(
  361. child: Text(
  362. title ?? '',
  363. style: TextStyle(color: Colors.white, fontSize: 14),
  364. ),
  365. ),
  366. secondChild,
  367. Padding(
  368. padding: EdgeInsets.only(left: 2),
  369. child: Image.asset(
  370. "images/icon_inter.png",
  371. width: 24,
  372. height: 24,
  373. ),
  374. )
  375. ],
  376. ),
  377. ),
  378. ),
  379. );
  380. }
  381. }