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.

513 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. Get.snackbar(label_country_empty, label_country_empty_message,
  203. snackPosition: SnackPosition.BOTTOM);
  204. }
  205. },
  206. child: Container(
  207. padding: EdgeInsets.only(
  208. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  209. decoration: BoxDecoration(
  210. border: kBorderTextField,
  211. ),
  212. child: Row(
  213. children: [
  214. GetBuilder<ChangeSupply>(
  215. builder: (_) => Expanded(
  216. child: Text(
  217. checkChangeLocation.currentProvince.name ??
  218. "Tỉnh/Thành Phố",
  219. style: TextStyle(
  220. fontSize: 14.0, color: Colors.black87)))),
  221. Icon(
  222. Icons.arrow_drop_down,
  223. color: Colors.grey,
  224. ),
  225. ],
  226. )));
  227. });
  228. }
  229. Widget _btnSelectDistrict() {
  230. return GetBuilder<CheckChangeAnotherDropdown>(builder: (data) {
  231. return FlatButton(
  232. padding:
  233. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  234. onPressed: () {
  235. if (Get.isSnackbarOpen) Get.back();
  236. if (checkChangeLocation.currentProvince.id != null) {
  237. Navigator.of(context)
  238. .push(MaterialPageRoute(
  239. builder: (_) => LocationScreen(
  240. titleName: "Quận/Huyện",
  241. type: LocationType.district,
  242. filterId: checkChangeLocation.currentProvince.id,
  243. selectedId: checkChangeLocation.currentDistrict.id),
  244. fullscreenDialog: false))
  245. .then((value) {
  246. if (value != null) {
  247. var result = value as LocationUnit;
  248. checkChangeLocation.changeDistrict(result);
  249. }
  250. });
  251. } else {
  252. Get.snackbar(label_province_empty, label_province_empty_message,
  253. snackPosition: SnackPosition.BOTTOM);
  254. }
  255. },
  256. child: Container(
  257. padding: EdgeInsets.only(
  258. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  259. decoration: BoxDecoration(
  260. border: kBorderTextField,
  261. ),
  262. child: Row(
  263. children: [
  264. GetBuilder<ChangeSupply>(
  265. builder: (_) => Expanded(
  266. child: Text(
  267. checkChangeLocation.currentDistrict.name ??
  268. "Quận/Huyện",
  269. style: TextStyle(
  270. fontSize: 14.0, color: Colors.black87)))),
  271. Icon(
  272. Icons.arrow_drop_down,
  273. color: Colors.grey,
  274. ),
  275. ],
  276. )));
  277. });
  278. }
  279. Widget _btnSelectWard() {
  280. return GetBuilder<CheckChangeAnotherDropdown>(builder: (data) {
  281. return FlatButton(
  282. padding:
  283. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  284. onPressed: () {
  285. if (Get.isSnackbarOpen) Get.back();
  286. if (checkChangeLocation.currentDistrict.id != null) {
  287. Navigator.of(context)
  288. .push(MaterialPageRoute(
  289. builder: (_) => LocationScreen(
  290. titleName: "Phường/Xã",
  291. type: LocationType.ward,
  292. filterId: checkChangeLocation.currentDistrict.id,
  293. selectedId: checkChangeLocation.currentWard.id),
  294. fullscreenDialog: false))
  295. .then((value) {
  296. if (value != null) {
  297. var result = value as LocationUnit;
  298. checkChangeLocation.changeWard(result);
  299. }
  300. });
  301. } else {
  302. Get.snackbar(label_district_empty, label_district_empty_message,
  303. snackPosition: SnackPosition.BOTTOM);
  304. }
  305. },
  306. child: Container(
  307. padding: EdgeInsets.only(
  308. top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  309. decoration: BoxDecoration(
  310. border: kBorderTextField,
  311. ),
  312. child: Row(
  313. children: [
  314. GetBuilder<ChangeSupply>(
  315. builder: (_) => Expanded(
  316. child: Text(
  317. checkChangeLocation.currentWard.name ??
  318. "Phường/Xã",
  319. style: TextStyle(
  320. fontSize: 14.0, color: Colors.black87)))),
  321. Icon(
  322. Icons.arrow_drop_down,
  323. color: Colors.grey,
  324. ),
  325. ],
  326. )));
  327. });
  328. }
  329. Widget _addressField() {
  330. return TextFormField(
  331. keyboardType: TextInputType.text,
  332. decoration: InputDecoration(labelText: "Địa chỉ"),
  333. controller: _addressController,
  334. onSaved: (newValue) {
  335. _account.address = newValue;
  336. },
  337. );
  338. }
  339. Widget _btnChangePassword() {
  340. return FlatButton(
  341. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  342. onPressed: () {
  343. Navigator.push(
  344. context,
  345. MaterialPageRoute(builder: (context) => ChangePasswordScreen()),
  346. );
  347. },
  348. child: Container(
  349. padding:
  350. EdgeInsets.only(top: 15.0, right: 0.0, bottom: 10.5, left: 0.0),
  351. decoration: BoxDecoration(
  352. border:
  353. Border(bottom: BorderSide(width: 0.5, color: Colors.grey)),
  354. ),
  355. child: Row(
  356. children: [
  357. Expanded(
  358. child: Text(
  359. "Cập nhật mật khẩu".toUpperCase(),
  360. style: TextStyle(
  361. fontSize: 14.0,
  362. color: Colors.black,
  363. fontWeight: FontWeight.bold),
  364. )),
  365. Icon(Icons.arrow_forward_ios),
  366. ],
  367. )));
  368. }
  369. Widget _textPackageInfo() {
  370. return Container(
  371. width: double.infinity,
  372. alignment: Alignment.centerRight,
  373. child: Text(
  374. "version:${_packageInfo.version}.${_packageInfo.buildNumber}",
  375. style: TextStyle(color: COLOR_CONST.GRAY1_70)));
  376. }
  377. @override
  378. Widget build(BuildContext context) => KeyboardDismisser(
  379. child: Scaffold(
  380. appBar: AppBar(
  381. centerTitle: true,
  382. title: Text(
  383. "Thông tin cá nhân",
  384. textAlign: TextAlign.center,
  385. ),
  386. actions: <Widget>[
  387. IconButton(
  388. icon: Icon(Icons.done),
  389. onPressed: () {
  390. FocusScopeNode currentFocus = FocusScope.of(context);
  391. if (!currentFocus.hasPrimaryFocus) {
  392. currentFocus.unfocus();
  393. }
  394. _validateInputs();
  395. })
  396. ],
  397. ),
  398. key: _scaffoldKey,
  399. body: _buildContent()));
  400. Widget _buildContent() {
  401. return StreamBuilder(
  402. stream: getAccountBloc.actions,
  403. builder: (context, AsyncSnapshot<dynamic> snapshot) {
  404. if (snapshot.hasData) {
  405. return Form(
  406. key: _formKey,
  407. autovalidate: _autoValidate,
  408. child: SingleChildScrollView(
  409. padding: EdgeInsets.all(8.0),
  410. child: Column(
  411. children: <Widget>[
  412. _userNameField(),
  413. SizedBox(
  414. height: 8.0,
  415. ),
  416. _fullNameField(),
  417. SizedBox(
  418. height: 8.0,
  419. ),
  420. _emailField(),
  421. SizedBox(
  422. height: 8.0,
  423. ),
  424. Container(
  425. width: double.infinity,
  426. child: Text(
  427. "Quốc gia",
  428. style:
  429. TextStyle(color: Colors.black54, fontSize: 13.0),
  430. ),
  431. ),
  432. _btnSelectCountry(),
  433. Container(
  434. width: double.infinity,
  435. child: Text(
  436. "Tỉnh/Thành phố",
  437. style:
  438. TextStyle(color: Colors.black54, fontSize: 13.0),
  439. ),
  440. ),
  441. _btnSelectProvince(),
  442. Container(
  443. width: double.infinity,
  444. child: Text(
  445. "Quận/Huyện",
  446. style:
  447. TextStyle(color: Colors.black54, fontSize: 13.0),
  448. ),
  449. ),
  450. _btnSelectDistrict(),
  451. Container(
  452. width: double.infinity,
  453. child: Text(
  454. "Phường/Xã",
  455. style:
  456. TextStyle(color: Colors.black54, fontSize: 13.0),
  457. ),
  458. ),
  459. _btnSelectWard(),
  460. SizedBox(
  461. height: 8.0,
  462. ),
  463. _addressField(),
  464. SizedBox(
  465. height: 16.0,
  466. ),
  467. _btnChangePassword(),
  468. SizedBox(
  469. height: 8,
  470. ),
  471. _textPackageInfo()
  472. ],
  473. ),
  474. ));
  475. } else {
  476. return Center(
  477. child: CircularProgressIndicator(),
  478. );
  479. }
  480. });
  481. }
  482. @override
  483. void dispose() {
  484. super.dispose();
  485. _userNameController.dispose();
  486. _emailController.dispose();
  487. _fullNameController.dispose();
  488. _addressController.dispose();
  489. }
  490. }