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.

467 lines
17KB

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