ChangeUserInfo.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'package:flutter/material.dart';
  2. import '../styles/colors.dart';
  3. import 'dart:ui';
  4. import '../styles/totast.dart';
  5. import '../widget/ITextInput.dart';
  6. import 'package:redux/redux.dart';
  7. import 'package:flutter_redux/flutter_redux.dart';
  8. import '../redux/AppState.dart';
  9. import '../model/UserInfo.dart';
  10. import '../net/HttpManager.dart';
  11. import '../net/Result.dart';
  12. import '../redux/UserRedux.dart';
  13. class ChangeUserInfo extends StatefulWidget {
  14. ChangeUserInfo({Key key, this.title, this.val}) : super(key: key);
  15. final String title; // 用来储存传递过来的值
  16. final String val; // 用来储存传递过来的值
  17. @override
  18. ChangeUserInfoState createState() => ChangeUserInfoState();
  19. }
  20. class ChangeUserInfoState extends State<ChangeUserInfo> {
  21. Map userInfo = {};
  22. String changeText = '';
  23. String changekey = 'nickname';
  24. @override
  25. void initState() {
  26. super.initState();
  27. switch (widget.title) {
  28. case '昵称':
  29. changekey = 'nickname';
  30. break;
  31. case '手机号':
  32. changekey = 'phone';
  33. break;
  34. }
  35. changeText = widget.val;
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return StoreConnector<AppState, UserInfo>(
  40. converter: (Store store) => store.state.userInfo,
  41. builder: (context, userInfo) {
  42. return WillPopScope(
  43. child: Scaffold(
  44. appBar: AppBar(
  45. // backgroundColor: PRIMARY_COLOR,
  46. title: Text('修改' + widget.title),
  47. centerTitle: true,
  48. elevation: 0,
  49. ),
  50. body: Container(
  51. padding: EdgeInsets.symmetric(vertical: 30, horizontal: 15),
  52. color: BG_COLOR,
  53. child: Column(
  54. children: <Widget>[
  55. Container(
  56. child: ITextField(
  57. autofocus: true,
  58. inputText: changeText,
  59. maxLength: 15,
  60. hintText: '输入用户' + widget.title,
  61. hintStyle: TextStyle(
  62. fontSize: 16,
  63. color: Color(0xFF727785),
  64. ),
  65. textStyle: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w500),
  66. fieldCallBack: (content) {
  67. setState(() {
  68. changeText = content;
  69. });
  70. },
  71. counterStyle: TextStyle(color: BG_SUB_COLOR, fontSize: 0),
  72. ),
  73. ),
  74. Container(
  75. margin: EdgeInsets.only(top: 30),
  76. width: double.infinity,
  77. height: 48,
  78. child: FlatButton(
  79. textColor: Colors.white,
  80. color: PRIMARY_COLOR,
  81. highlightColor: Color(0xFF763434),
  82. child: Text(
  83. '确定修改',
  84. style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
  85. ),
  86. onPressed: () async {
  87. if (changeText.isEmpty) {
  88. Toast.show(context, '修改内容不能为空', 1500, 'info');
  89. return;
  90. }
  91. Toast.show(context, '加载中', -1, 'loading');
  92. final Result res = await HttpManager.post('userInfo/update', data: {'id': userInfo.id, changekey: changeText});
  93. Toast.hide();
  94. if (res.success) {
  95. Result res2 = await HttpManager.get('userInfo/getUserInfo');
  96. if (res2.success) {
  97. StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(UserInfo.fromJson(res2.data)));
  98. Toast.show(context, '修改成功', 1000, 'success');
  99. Future.delayed(Duration(milliseconds: 1000), () {
  100. Navigator.of(context).pop();
  101. });
  102. } else {}
  103. } else {
  104. Toast.show(context, res.error, 1500, 'info');
  105. }
  106. },
  107. ),
  108. ),
  109. ],
  110. ),
  111. )),
  112. onWillPop: () {
  113. Toast.hide();
  114. Navigator.pop(context, false);
  115. return Future.value(false);
  116. });
  117. });
  118. }
  119. }