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.

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