You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
7.3KB

  1. import 'package:farm_tpf/data/repository/user_repository.dart';
  2. import 'package:farm_tpf/models/account.dart';
  3. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/widget_toast.dart';
  5. import 'package:farm_tpf/presentation/screens/profile/sc_change_password.dart';
  6. import 'package:farm_tpf/utils/validators.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:fluttertoast/fluttertoast.dart';
  9. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  10. import 'bloc_get_account.dart';
  11. class UpdateProfileScreen extends StatefulWidget {
  12. static Route route() {
  13. return MaterialPageRoute<void>(builder: (_) => UpdateProfileScreen());
  14. }
  15. @override
  16. _UpdateProfileScreenState createState() => _UpdateProfileScreenState();
  17. }
  18. class _UpdateProfileScreenState extends State<UpdateProfileScreen> {
  19. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  20. final _repository = UserRepository();
  21. GlobalKey<FormState> _formKey = GlobalKey();
  22. bool _autoValidate = false;
  23. FlutterToast flutterToast;
  24. Account _account = Account();
  25. TextEditingController _userNameController = TextEditingController();
  26. TextEditingController _fullNameController = TextEditingController();
  27. TextEditingController _emailController = TextEditingController();
  28. TextEditingController _addressController = TextEditingController();
  29. @override
  30. void initState() {
  31. super.initState();
  32. flutterToast = FlutterToast(context);
  33. getAccountBloc.getAccount((data) {
  34. _account = data;
  35. _userNameController.text = _account.login;
  36. _fullNameController.text = _account.fullName.toString();
  37. _emailController.text = _account.email.toString();
  38. _addressController.text = _account.address;
  39. }, (err) {
  40. flutterToast.showToast(child: WidgetToast(message: "Lỗi tải dữ liệu"));
  41. });
  42. }
  43. _validateInputs() async {
  44. if (_formKey.currentState.validate()) {
  45. _formKey.currentState.save();
  46. LoadingDialog.showLoadingDialog(_scaffoldKey.currentContext);
  47. _repository.updateProfile(_account).then((value) {
  48. LoadingDialog.hideLoadingDialog(_scaffoldKey.currentContext);
  49. _scaffoldKey.currentState.showSnackBar(SnackBar(
  50. content: Row(
  51. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  52. children: <Widget>[
  53. Text('Cập nhật thành công.'),
  54. Icon(Icons.done),
  55. ],
  56. ),
  57. backgroundColor: Colors.green,
  58. duration: Duration(seconds: 3),
  59. ));
  60. }).catchError((onError) {
  61. _scaffoldKey.currentState.showSnackBar(SnackBar(
  62. content: Row(
  63. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  64. children: <Widget>[
  65. Text('Cập nhật không thành công.'),
  66. Icon(Icons.error),
  67. ],
  68. ),
  69. backgroundColor: Colors.red,
  70. duration: Duration(seconds: 3),
  71. ));
  72. LoadingDialog.hideLoadingDialog(_scaffoldKey.currentContext);
  73. print("error");
  74. });
  75. } else {
  76. _autoValidate = true;
  77. }
  78. }
  79. Widget _userNameField() {
  80. return TextFormField(
  81. keyboardType: TextInputType.text,
  82. enabled: false,
  83. decoration: InputDecoration(labelText: "Tài khoản"),
  84. controller: _userNameController,
  85. validator: (String value) {
  86. return Validators.validateNotNullOrEmpty(value, "Tài khoản");
  87. },
  88. onSaved: (newValue) {},
  89. );
  90. }
  91. Widget _fullNameField() {
  92. return TextFormField(
  93. keyboardType: TextInputType.text,
  94. decoration: InputDecoration(labelText: "Họ và tên"),
  95. controller: _fullNameController,
  96. validator: (String value) {
  97. return Validators.validateNotNullOrEmpty(value, "Họ và tên");
  98. },
  99. onSaved: (newValue) {
  100. _account.fullName = newValue;
  101. },
  102. );
  103. }
  104. Widget _emailField() {
  105. return TextFormField(
  106. keyboardType: TextInputType.emailAddress,
  107. decoration: InputDecoration(labelText: "Email"),
  108. controller: _emailController,
  109. validator: (String value) {
  110. return Validators.validateEmail(value);
  111. },
  112. onSaved: (newValue) {
  113. _account.email = newValue;
  114. },
  115. );
  116. }
  117. Widget _addressField() {
  118. return TextFormField(
  119. keyboardType: TextInputType.text,
  120. decoration: InputDecoration(labelText: "Địa chỉ"),
  121. controller: _addressController,
  122. onSaved: (newValue) {
  123. _account.address = newValue;
  124. },
  125. );
  126. }
  127. Widget _btnChangePassword() {
  128. return FlatButton(
  129. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  130. onPressed: () {
  131. Navigator.push(
  132. context,
  133. MaterialPageRoute(builder: (context) => ChangePasswordScreen()),
  134. );
  135. },
  136. child: Container(
  137. padding:
  138. EdgeInsets.only(top: 15.0, right: 0.0, bottom: 10.5, left: 0.0),
  139. decoration: BoxDecoration(
  140. border:
  141. Border(bottom: BorderSide(width: 0.5, color: Colors.grey)),
  142. ),
  143. child: Row(
  144. children: [
  145. Expanded(
  146. child: Text(
  147. "Cập nhật mật khẩu".toUpperCase(),
  148. style: TextStyle(
  149. fontSize: 14.0,
  150. color: Colors.black,
  151. fontWeight: FontWeight.bold),
  152. )),
  153. Icon(Icons.arrow_forward_ios),
  154. ],
  155. )));
  156. }
  157. @override
  158. Widget build(BuildContext context) => KeyboardDismisser(
  159. child: Scaffold(
  160. appBar: AppBar(
  161. centerTitle: true,
  162. title: Text(
  163. "Thông tin cá nhân",
  164. textAlign: TextAlign.center,
  165. ),
  166. actions: <Widget>[
  167. IconButton(
  168. icon: Icon(Icons.done),
  169. onPressed: () {
  170. FocusScopeNode currentFocus = FocusScope.of(context);
  171. if (!currentFocus.hasPrimaryFocus) {
  172. currentFocus.unfocus();
  173. }
  174. _validateInputs();
  175. })
  176. ],
  177. ),
  178. key: _scaffoldKey,
  179. body: _buildContent()));
  180. Widget _buildContent() {
  181. return StreamBuilder(
  182. stream: getAccountBloc.actions,
  183. builder: (context, AsyncSnapshot<dynamic> snapshot) {
  184. if (snapshot.hasData) {
  185. return Form(
  186. key: _formKey,
  187. autovalidate: _autoValidate,
  188. child: SingleChildScrollView(
  189. padding: EdgeInsets.all(8.0),
  190. child: Column(
  191. children: <Widget>[
  192. _userNameField(),
  193. SizedBox(
  194. height: 8.0,
  195. ),
  196. _fullNameField(),
  197. SizedBox(
  198. height: 8.0,
  199. ),
  200. _emailField(),
  201. SizedBox(
  202. height: 16.0,
  203. ),
  204. _btnChangePassword()
  205. ],
  206. ),
  207. ));
  208. } else {
  209. return Center(
  210. child: CircularProgressIndicator(),
  211. );
  212. }
  213. });
  214. }
  215. }