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.

475 lines
16KB

  1. import 'package:farm_tpf/custom_model/account.dart';
  2. import 'package:farm_tpf/data/repository/user_repository.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/actions/state_management_helper/change_supply.dart';
  6. import 'package:farm_tpf/presentation/screens/location_unit/sc_location.dart';
  7. import 'package:farm_tpf/presentation/screens/profile/sc_change_password.dart';
  8. import 'package:farm_tpf/utils/const_color.dart';
  9. import 'package:farm_tpf/utils/const_common.dart';
  10. import 'package:farm_tpf/utils/const_style.dart';
  11. import 'package:farm_tpf/utils/validators.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:fluttertoast/fluttertoast.dart';
  14. import 'package:get/get.dart';
  15. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  16. import 'package:package_info/package_info.dart';
  17. import 'bloc_get_account.dart';
  18. class UpdateProfileScreen extends StatefulWidget {
  19. static Route route() {
  20. return MaterialPageRoute<void>(builder: (_) => UpdateProfileScreen());
  21. }
  22. @override
  23. _UpdateProfileScreenState createState() => _UpdateProfileScreenState();
  24. }
  25. class _UpdateProfileScreenState extends State<UpdateProfileScreen> {
  26. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  27. final _repository = UserRepository();
  28. GlobalKey<FormState> _formKey = GlobalKey();
  29. bool _autoValidate = false;
  30. FlutterToast flutterToast;
  31. Account _account = Account();
  32. TextEditingController _userNameController = TextEditingController();
  33. TextEditingController _fullNameController = TextEditingController();
  34. TextEditingController _emailController = TextEditingController();
  35. TextEditingController _addressController = TextEditingController();
  36. PackageInfo _packageInfo = PackageInfo(
  37. version: '1.0.0',
  38. buildNumber: '1.',
  39. );
  40. Future<void> _initPackageInfo() async {
  41. final PackageInfo info = await PackageInfo.fromPlatform();
  42. setState(() {
  43. _packageInfo = info;
  44. });
  45. }
  46. @override
  47. void initState() {
  48. super.initState();
  49. _initPackageInfo();
  50. flutterToast = FlutterToast(context);
  51. getAccountBloc.getAccount((data) {
  52. _account = data;
  53. _userNameController.text = _account.login;
  54. _fullNameController.text = _account.fullName.toString();
  55. _emailController.text = _account.email.toString();
  56. _addressController.text = _account.address;
  57. }, (err) {
  58. flutterToast.showToast(child: WidgetToast(message: "Lỗi tải dữ liệu"));
  59. });
  60. }
  61. _validateInputs() async {
  62. if (_formKey.currentState.validate()) {
  63. _formKey.currentState.save();
  64. LoadingDialog.showLoadingDialog(_scaffoldKey.currentContext);
  65. _repository.updateProfile(_account).then((value) {
  66. LoadingDialog.hideLoadingDialog(_scaffoldKey.currentContext);
  67. _scaffoldKey.currentState.showSnackBar(SnackBar(
  68. content: Row(
  69. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  70. children: <Widget>[
  71. Text('Cập nhật thành công.'),
  72. Icon(Icons.done),
  73. ],
  74. ),
  75. backgroundColor: Colors.green,
  76. duration: Duration(seconds: 3),
  77. ));
  78. }).catchError((onError) {
  79. _scaffoldKey.currentState.showSnackBar(SnackBar(
  80. content: Row(
  81. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  82. children: <Widget>[
  83. Text('Cập nhật không thành công.'),
  84. Icon(Icons.error),
  85. ],
  86. ),
  87. backgroundColor: Colors.red,
  88. duration: Duration(seconds: 3),
  89. ));
  90. LoadingDialog.hideLoadingDialog(_scaffoldKey.currentContext);
  91. print("error");
  92. });
  93. } else {
  94. _autoValidate = true;
  95. }
  96. }
  97. Widget _userNameField() {
  98. return TextFormField(
  99. keyboardType: TextInputType.text,
  100. enabled: false,
  101. decoration: InputDecoration(labelText: "Tài khoản"),
  102. controller: _userNameController,
  103. validator: (String value) {
  104. return Validators.validateNotNullOrEmpty(value, "Tài khoản");
  105. },
  106. onSaved: (newValue) {},
  107. );
  108. }
  109. Widget _fullNameField() {
  110. return TextFormField(
  111. keyboardType: TextInputType.text,
  112. decoration: InputDecoration(labelText: "Họ và tên"),
  113. controller: _fullNameController,
  114. validator: (String value) {
  115. return Validators.validateNotNullOrEmpty(value, "Họ và tên");
  116. },
  117. onSaved: (newValue) {
  118. _account.fullName = newValue;
  119. },
  120. );
  121. }
  122. Widget _emailField() {
  123. return TextFormField(
  124. keyboardType: TextInputType.emailAddress,
  125. decoration: InputDecoration(labelText: "Email"),
  126. controller: _emailController,
  127. validator: (String value) {
  128. return Validators.validateEmail(value);
  129. },
  130. onSaved: (newValue) {
  131. _account.email = newValue;
  132. },
  133. );
  134. }
  135. Widget _btnSelectCountry() {
  136. return GetBuilder<ChangeSupply>(builder: (data) {
  137. return FlatButton(
  138. padding:
  139. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  140. onPressed: () {
  141. Navigator.of(context)
  142. .push(MaterialPageRoute(
  143. builder: (_) => LocationScreen(
  144. titleName: "Quốc gia",
  145. type: LocationType.country,
  146. filterId: null,
  147. selectedId: null),
  148. fullscreenDialog: false))
  149. .then((value) {
  150. if (value != null) {}
  151. });
  152. },
  153. child: Container(
  154. padding: EdgeInsets.only(
  155. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  156. decoration: BoxDecoration(
  157. border: kBorderTextField,
  158. ),
  159. child: Row(
  160. children: [
  161. GetBuilder<ChangeSupply>(
  162. builder: (_) => Expanded(
  163. child: Text("sdfd" ?? "Quốc gia",
  164. style: TextStyle(
  165. fontSize: 14.0, color: Colors.black87)))),
  166. Icon(
  167. Icons.arrow_drop_down,
  168. color: Colors.grey,
  169. ),
  170. ],
  171. )));
  172. });
  173. }
  174. Widget _btnSelectProvince() {
  175. return GetBuilder<ChangeSupply>(builder: (data) {
  176. return FlatButton(
  177. padding:
  178. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  179. onPressed: () {
  180. Navigator.of(context)
  181. .push(MaterialPageRoute(
  182. builder: (_) => LocationScreen(
  183. titleName: "Tỉnh/Thành phố",
  184. type: LocationType.province,
  185. filterId: 1,
  186. selectedId: null),
  187. fullscreenDialog: false))
  188. .then((value) {
  189. if (value != null) {}
  190. });
  191. },
  192. child: Container(
  193. padding: EdgeInsets.only(
  194. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  195. decoration: BoxDecoration(
  196. border: kBorderTextField,
  197. ),
  198. child: Row(
  199. children: [
  200. GetBuilder<ChangeSupply>(
  201. builder: (_) => Expanded(
  202. child: Text("sdfd" ?? "Tỉnh/Thành Phố",
  203. style: TextStyle(
  204. fontSize: 14.0, color: Colors.black87)))),
  205. Icon(
  206. Icons.arrow_drop_down,
  207. color: Colors.grey,
  208. ),
  209. ],
  210. )));
  211. });
  212. }
  213. Widget _btnSelectDistrict() {
  214. return GetBuilder<ChangeSupply>(builder: (data) {
  215. return FlatButton(
  216. padding:
  217. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  218. onPressed: () {
  219. Navigator.of(context)
  220. .push(MaterialPageRoute(
  221. builder: (_) => LocationScreen(
  222. titleName: "Quận/Huyện",
  223. type: LocationType.district,
  224. filterId: 79,
  225. selectedId: null),
  226. fullscreenDialog: false))
  227. .then((value) {
  228. if (value != null) {}
  229. });
  230. },
  231. child: Container(
  232. padding: EdgeInsets.only(
  233. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  234. decoration: BoxDecoration(
  235. border: kBorderTextField,
  236. ),
  237. child: Row(
  238. children: [
  239. GetBuilder<ChangeSupply>(
  240. builder: (_) => Expanded(
  241. child: Text("sdfd" ?? "Quận/Huyện",
  242. style: TextStyle(
  243. fontSize: 14.0, color: Colors.black87)))),
  244. Icon(
  245. Icons.arrow_drop_down,
  246. color: Colors.grey,
  247. ),
  248. ],
  249. )));
  250. });
  251. }
  252. Widget _btnSelectWard() {
  253. return GetBuilder<ChangeSupply>(builder: (data) {
  254. return FlatButton(
  255. padding:
  256. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  257. onPressed: () {
  258. Navigator.of(context)
  259. .push(MaterialPageRoute(
  260. builder: (_) => LocationScreen(
  261. titleName: "Phường/Xã",
  262. type: LocationType.ward,
  263. filterId: 760,
  264. selectedId: null),
  265. fullscreenDialog: false))
  266. .then((value) {
  267. if (value != null) {}
  268. });
  269. },
  270. child: Container(
  271. padding: EdgeInsets.only(
  272. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  273. decoration: BoxDecoration(
  274. border: kBorderTextField,
  275. ),
  276. child: Row(
  277. children: [
  278. GetBuilder<ChangeSupply>(
  279. builder: (_) => Expanded(
  280. child: Text("sdfd" ?? "Phường/Xã",
  281. style: TextStyle(
  282. fontSize: 14.0, color: Colors.black87)))),
  283. Icon(
  284. Icons.arrow_drop_down,
  285. color: Colors.grey,
  286. ),
  287. ],
  288. )));
  289. });
  290. }
  291. Widget _addressField() {
  292. return TextFormField(
  293. keyboardType: TextInputType.text,
  294. decoration: InputDecoration(labelText: "Địa chỉ"),
  295. controller: _addressController,
  296. onSaved: (newValue) {
  297. _account.address = newValue;
  298. },
  299. );
  300. }
  301. Widget _btnChangePassword() {
  302. return FlatButton(
  303. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  304. onPressed: () {
  305. Navigator.push(
  306. context,
  307. MaterialPageRoute(builder: (context) => ChangePasswordScreen()),
  308. );
  309. },
  310. child: Container(
  311. padding:
  312. EdgeInsets.only(top: 15.0, right: 0.0, bottom: 10.5, left: 0.0),
  313. decoration: BoxDecoration(
  314. border:
  315. Border(bottom: BorderSide(width: 0.5, color: Colors.grey)),
  316. ),
  317. child: Row(
  318. children: [
  319. Expanded(
  320. child: Text(
  321. "Cập nhật mật khẩu".toUpperCase(),
  322. style: TextStyle(
  323. fontSize: 14.0,
  324. color: Colors.black,
  325. fontWeight: FontWeight.bold),
  326. )),
  327. Icon(Icons.arrow_forward_ios),
  328. ],
  329. )));
  330. }
  331. Widget _textPackageInfo() {
  332. return Container(
  333. width: double.infinity,
  334. alignment: Alignment.centerRight,
  335. child: Text(
  336. "version:${_packageInfo.version}.${_packageInfo.buildNumber}",
  337. style: TextStyle(color: COLOR_CONST.GRAY1_70)));
  338. }
  339. @override
  340. Widget build(BuildContext context) => KeyboardDismisser(
  341. child: Scaffold(
  342. appBar: AppBar(
  343. centerTitle: true,
  344. title: Text(
  345. "Thông tin cá nhân",
  346. textAlign: TextAlign.center,
  347. ),
  348. actions: <Widget>[
  349. IconButton(
  350. icon: Icon(Icons.done),
  351. onPressed: () {
  352. FocusScopeNode currentFocus = FocusScope.of(context);
  353. if (!currentFocus.hasPrimaryFocus) {
  354. currentFocus.unfocus();
  355. }
  356. _validateInputs();
  357. })
  358. ],
  359. ),
  360. key: _scaffoldKey,
  361. body: _buildContent()));
  362. Widget _buildContent() {
  363. return StreamBuilder(
  364. stream: getAccountBloc.actions,
  365. builder: (context, AsyncSnapshot<dynamic> snapshot) {
  366. if (snapshot.hasData) {
  367. return Form(
  368. key: _formKey,
  369. autovalidate: _autoValidate,
  370. child: SingleChildScrollView(
  371. padding: EdgeInsets.all(8.0),
  372. child: Column(
  373. children: <Widget>[
  374. _userNameField(),
  375. SizedBox(
  376. height: 8.0,
  377. ),
  378. _fullNameField(),
  379. SizedBox(
  380. height: 8.0,
  381. ),
  382. _emailField(),
  383. SizedBox(
  384. height: 8.0,
  385. ),
  386. Container(
  387. width: double.infinity,
  388. child: Text(
  389. "Quốc gia",
  390. style:
  391. TextStyle(color: Colors.black54, fontSize: 13.0),
  392. ),
  393. ),
  394. _btnSelectCountry(),
  395. Container(
  396. width: double.infinity,
  397. child: Text(
  398. "Tỉnh/Thành phố",
  399. style:
  400. TextStyle(color: Colors.black54, fontSize: 13.0),
  401. ),
  402. ),
  403. _btnSelectProvince(),
  404. Container(
  405. width: double.infinity,
  406. child: Text(
  407. "Quận/Huyện",
  408. style:
  409. TextStyle(color: Colors.black54, fontSize: 13.0),
  410. ),
  411. ),
  412. _btnSelectDistrict(),
  413. Container(
  414. width: double.infinity,
  415. child: Text(
  416. "Phường/Xã",
  417. style:
  418. TextStyle(color: Colors.black54, fontSize: 13.0),
  419. ),
  420. ),
  421. _btnSelectWard(),
  422. SizedBox(
  423. height: 8.0,
  424. ),
  425. _addressField(),
  426. SizedBox(
  427. height: 16.0,
  428. ),
  429. _btnChangePassword(),
  430. SizedBox(
  431. height: 8,
  432. ),
  433. _textPackageInfo()
  434. ],
  435. ),
  436. ));
  437. } else {
  438. return Center(
  439. child: CircularProgressIndicator(),
  440. );
  441. }
  442. });
  443. }
  444. @override
  445. void dispose() {
  446. super.dispose();
  447. _userNameController.dispose();
  448. _emailController.dispose();
  449. _fullNameController.dispose();
  450. _addressController.dispose();
  451. }
  452. }