setting.dart 13 KB

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