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.

510 lines
18KB

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