Setting.dart 12 KB

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