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.

531 lines
19KB

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