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.

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