Setting.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import 'package:flutter/cupertino.dart' as prefix0;
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import 'package:jpush_flutter/jpush_flutter.dart';
  6. import 'package:package_info/package_info.dart';
  7. import 'package:shared_preferences/shared_preferences.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. class Setting extends StatefulWidget {
  23. @override
  24. SettingState createState() => SettingState();
  25. }
  26. class SettingState extends State<Setting> {
  27. String version = '';
  28. bool receiveMsg = false;
  29. @override
  30. void initState() {
  31. super.initState();
  32. PackageInfo.fromPlatform().then((packageInfo) {
  33. setState(() {
  34. version = packageInfo.version;
  35. });
  36. });
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
  41. return StoreConnector<AppState, UserInfo>(
  42. converter: (Store store) => store.state.userInfo,
  43. builder: (context, userInfo) {
  44. return WillPopScope(
  45. key: Key('SettingsPage'),
  46. child: Scaffold(
  47. backgroundColor: Color(0xFF2E3049),
  48. appBar: AppBar(
  49. // backgroundColor: PRIMARY_COLOR,
  50. title: Text('系统设置'),
  51. centerTitle: true,
  52. elevation: 0,
  53. ),
  54. body: 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. Container(
  94. padding: EdgeInsets.fromLTRB(15, 60, 15, 30),
  95. child: LinearButton(
  96. btntext: '退出登录',
  97. onTapHomeMenu: () async {
  98. Toast.show(context, '退出成功', 1500, 'success');
  99. Navigator.push(context, MaterialPageRoute(builder: (context) => LoginFirst()));
  100. final prefs = await SharedPreferences.getInstance();
  101. prefs.remove('token');
  102. JPush jpush = JPush();
  103. jpush.deleteAlias();
  104. // Future.delayed(const Duration(seconds: 1), () {
  105. // StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(null));
  106. // });
  107. },
  108. ),
  109. ),
  110. ],
  111. ),
  112. ),
  113. ),
  114. ),
  115. floatingActionButton: Container(
  116. height: 100,
  117. padding: EdgeInsets.symmetric(vertical: 30),
  118. child: Column(
  119. children: <Widget>[Text('Copyright©2019',style:TextStyle(color: Colors.white30,fontSize: 12)), Text('盛世明越(海南)科技有限公',style:TextStyle(color: Colors.white30,fontSize: 12))],
  120. ),
  121. ),
  122. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  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 Container(
  263. height: 10,
  264. color: Color(0xFF24263A),
  265. );
  266. }
  267. Widget _section(List<Widget> children) {
  268. return Container(
  269. color: Color(0xFF2E3049),
  270. child: Column(
  271. children: children,
  272. ),
  273. );
  274. }
  275. Widget _cell(String title, dynamic child, {String placeholder, void Function() onTap, bool showBorder = true}) {
  276. Widget secondChild;
  277. if (child == null || (child is String && child.isEmpty)) {
  278. secondChild = Text(
  279. placeholder ?? '',
  280. style: TextStyle(color: PLACEHOLDER_COLOR, fontSize: 13),
  281. );
  282. } else if (child is String) {
  283. secondChild = Text(
  284. child ?? '',
  285. style: TextStyle(color: Colors.white, fontSize: 15),
  286. );
  287. } else {
  288. secondChild = child;
  289. }
  290. return Container(
  291. height: 60,
  292. padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
  293. child: GestureDetector(
  294. onTap: onTap,
  295. child: Container(
  296. decoration: BoxDecoration(
  297. border: Border(
  298. bottom: BorderSide(
  299. color: Color(0xFF24263A),
  300. width: showBorder ? 1 : 0,
  301. ),
  302. ),
  303. ),
  304. child: Row(
  305. mainAxisAlignment: MainAxisAlignment.center,
  306. children: <Widget>[
  307. Expanded(
  308. child: Text(
  309. title ?? '',
  310. style: TextStyle(color: Colors.white, fontSize: 14),
  311. ),
  312. ),
  313. secondChild,
  314. Padding(
  315. padding: EdgeInsets.only(left: 2),
  316. child: Image.asset(
  317. 'images/icon_inter.png',
  318. width: 24,
  319. height: 24,
  320. ),
  321. )
  322. ],
  323. ),
  324. ),
  325. ),
  326. );
  327. }
  328. }