Setting.dart 12 KB

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