changeUserInfo.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 'dart:convert';
  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 new 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(
  66. color: Colors.white,
  67. fontSize: 18,
  68. fontWeight: FontWeight.w500),
  69. fieldCallBack: (content) {
  70. setState(() {
  71. changeText = content;
  72. });
  73. },
  74. counterStyle:
  75. TextStyle(color: BG_SUB_COLOR, fontSize: 0),
  76. ),
  77. ),
  78. Container(
  79. margin: EdgeInsets.only(top: 30),
  80. width: double.infinity,
  81. height: 48,
  82. child: FlatButton(
  83. textColor: Colors.white,
  84. color: PRIMARY_COLOR,
  85. highlightColor: Color(0xFF763434),
  86. child: Text(
  87. "确定修改",
  88. style: TextStyle(
  89. fontSize: 16, fontWeight: FontWeight.w500),
  90. ),
  91. onPressed: () async {
  92. if (changeText.isEmpty) {
  93. Toast.show(context, '修改内容不能为空', 1500, 'info');
  94. return;
  95. }
  96. Toast.show(context, '加载中', -1, 'loading');
  97. final Result res = await HttpManager.post(
  98. 'userInfo/update',
  99. data: {
  100. "id": userInfo.id,
  101. changekey: changeText
  102. });
  103. Toast.hide();
  104. if (res.success) {
  105. Result res2 = await HttpManager.get(
  106. "userInfo/getUserInfo");
  107. if (res2.success) {
  108. StoreProvider.of<AppState>(context)
  109. .dispatch({
  110. "action": Actions.updateUser,
  111. "user": res2.data
  112. });
  113. Toast.show(
  114. context, '修改成功', 1000, 'success');
  115. Future.delayed(Duration(milliseconds: 1000),
  116. () {
  117. Navigator.of(context).pop();
  118. });
  119. } else {}
  120. } else {
  121. Toast.show(context, res.error, 1500, 'info');
  122. }
  123. }),
  124. ),
  125. ],
  126. ),
  127. )),
  128. onWillPop: () {
  129. Toast.hide();
  130. Navigator.pop(context, false);
  131. return Future.value(false);
  132. });
  133. });
  134. }
  135. }