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.

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