Setting.dart 12 KB

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