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.

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