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.

481 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/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>(
  115. init: checkChangeLocation,
  116. builder: (data) {
  117. return TextButton(
  118. onPressed: () {
  119. if (Get.isSnackbarOpen) {
  120. Get.back();
  121. }
  122. Navigator.of(context)
  123. .push(MaterialPageRoute(
  124. builder: (_) => LocationScreen(
  125. titleName: "Quốc gia",
  126. type: LocationType.country,
  127. filterId: -1,
  128. selectedId: checkChangeLocation.currentCountry?.id ?? -1,
  129. ),
  130. fullscreenDialog: false))
  131. .then((value) {
  132. if (value != null) {
  133. var result = value as LocationUnit;
  134. checkChangeLocation.changeCountry(result);
  135. }
  136. });
  137. },
  138. child: Container(
  139. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  140. decoration: BoxDecoration(
  141. border: kBorderTextField,
  142. ),
  143. child: Row(
  144. children: [
  145. GetBuilder<ChangeSupply>(
  146. init: 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>(
  167. init: CheckChangeAnotherDropdown(),
  168. builder: (data) {
  169. return TextButton(
  170. onPressed: () {
  171. if (Get.isSnackbarOpen) {
  172. Get.back();
  173. }
  174. if (checkChangeLocation.currentCountry?.id != null) {
  175. Navigator.of(context)
  176. .push(
  177. MaterialPageRoute(
  178. builder: (_) => LocationScreen(
  179. titleName: "Tỉnh/Thành phố",
  180. type: LocationType.province,
  181. filterId: checkChangeLocation.currentCountry?.id ?? -1,
  182. selectedId: checkChangeLocation.currentProvince?.id ?? -1,
  183. ),
  184. fullscreenDialog: false,
  185. ),
  186. )
  187. .then((value) {
  188. if (value != null) {
  189. var result = value as LocationUnit;
  190. checkChangeLocation.changeProvince(result);
  191. }
  192. });
  193. } else {
  194. Utils.showSnackBarWarning(message: label_country_empty);
  195. }
  196. },
  197. child: Container(
  198. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  199. decoration: BoxDecoration(
  200. border: kBorderTextField,
  201. ),
  202. child: Row(
  203. children: [
  204. GetBuilder<ChangeSupply>(
  205. init: ChangeSupply(),
  206. builder: (_) => Expanded(
  207. child: Text(
  208. checkChangeLocation.currentProvince?.name ?? "Tỉnh/Thành Phố",
  209. style: TextStyle(
  210. fontSize: 14.0,
  211. color: Colors.black87,
  212. ),
  213. ),
  214. ),
  215. ),
  216. Icon(
  217. Icons.arrow_drop_down,
  218. color: Colors.grey,
  219. ),
  220. ],
  221. )));
  222. });
  223. }
  224. Widget _btnSelectDistrict() {
  225. return GetBuilder<CheckChangeAnotherDropdown>(
  226. init: CheckChangeAnotherDropdown(),
  227. builder: (data) {
  228. return TextButton(
  229. onPressed: () {
  230. if (Get.isSnackbarOpen) Get.back();
  231. if (checkChangeLocation.currentProvince?.id != null) {
  232. Navigator.of(context)
  233. .push(
  234. MaterialPageRoute(
  235. builder: (_) => LocationScreen(
  236. titleName: "Quận/Huyện",
  237. type: LocationType.district,
  238. filterId: checkChangeLocation.currentProvince?.id ?? -1,
  239. selectedId: checkChangeLocation.currentDistrict?.id ?? -1,
  240. ),
  241. fullscreenDialog: false,
  242. ),
  243. )
  244. .then((value) {
  245. if (value != null) {
  246. var result = value as LocationUnit;
  247. checkChangeLocation.changeDistrict(result);
  248. }
  249. });
  250. } else {
  251. Utils.showSnackBarWarning(message: label_province_empty);
  252. }
  253. },
  254. child: Container(
  255. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  256. decoration: BoxDecoration(
  257. border: kBorderTextField,
  258. ),
  259. child: Row(
  260. children: [
  261. GetBuilder<ChangeSupply>(
  262. init: ChangeSupply(),
  263. builder: (_) => Expanded(
  264. child: Text(
  265. checkChangeLocation.currentDistrict?.name ?? "Quận/Huyện",
  266. style: TextStyle(
  267. fontSize: 14.0,
  268. color: Colors.black87,
  269. ),
  270. ),
  271. ),
  272. ),
  273. Icon(
  274. Icons.arrow_drop_down,
  275. color: Colors.grey,
  276. ),
  277. ],
  278. )));
  279. });
  280. }
  281. Widget _btnSelectWard() {
  282. return GetBuilder<CheckChangeAnotherDropdown>(
  283. init: CheckChangeAnotherDropdown(),
  284. builder: (data) {
  285. return TextButton(
  286. onPressed: () {
  287. if (Get.isSnackbarOpen) Get.back();
  288. if (checkChangeLocation.currentDistrict?.id != null) {
  289. Navigator.of(context)
  290. .push(
  291. MaterialPageRoute(
  292. builder: (_) => LocationScreen(
  293. titleName: "Phường/Xã",
  294. type: LocationType.ward,
  295. filterId: checkChangeLocation.currentDistrict?.id ?? -1,
  296. selectedId: checkChangeLocation.currentWard?.id ?? -1,
  297. ),
  298. fullscreenDialog: false,
  299. ),
  300. )
  301. .then((value) {
  302. if (value != null) {
  303. var result = value as LocationUnit;
  304. checkChangeLocation.changeWard(result);
  305. }
  306. });
  307. } else {
  308. Utils.showSnackBarWarning(message: label_district_empty);
  309. }
  310. },
  311. child: Container(
  312. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  313. decoration: BoxDecoration(
  314. border: kBorderTextField,
  315. ),
  316. child: Row(
  317. children: [
  318. GetBuilder<ChangeSupply>(
  319. init: ChangeSupply(),
  320. builder: (_) => Expanded(
  321. child: Text(
  322. checkChangeLocation.currentWard?.name ?? "Phường/Xã",
  323. style: TextStyle(
  324. fontSize: 14.0,
  325. color: Colors.black87,
  326. ),
  327. ),
  328. ),
  329. ),
  330. Icon(
  331. Icons.arrow_drop_down,
  332. color: Colors.grey,
  333. ),
  334. ],
  335. )));
  336. });
  337. }
  338. Widget _addressField() {
  339. return TextFormField(
  340. keyboardType: TextInputType.text,
  341. decoration: InputDecoration(labelText: "Địa chỉ"),
  342. controller: _addressController,
  343. onSaved: (newValue) {
  344. _account.address = newValue;
  345. },
  346. );
  347. }
  348. @override
  349. Widget build(BuildContext context) => KeyboardDismisser(
  350. child: Scaffold(
  351. backgroundColor: Colors.white,
  352. appBar: AppBarWidget(
  353. isBack: true,
  354. action: InkWell(
  355. child: Text(
  356. 'Huỷ',
  357. style: TextStyle(color: Colors.red, fontWeight: FontWeight.normal),
  358. ),
  359. onTap: () {
  360. if (Get.isSnackbarOpen) Get.back();
  361. Get.back();
  362. },
  363. ),
  364. ),
  365. key: _scaffoldKey,
  366. body: _buildContent()));
  367. Widget _buildContent() {
  368. return StreamBuilder(
  369. stream: getAccountBloc.actions,
  370. builder: (context, AsyncSnapshot<dynamic> snapshot) {
  371. if (snapshot.hasData) {
  372. return Form(
  373. key: _formKey,
  374. child: SingleChildScrollView(
  375. padding: EdgeInsets.all(8.0),
  376. child: Column(
  377. crossAxisAlignment: CrossAxisAlignment.start,
  378. children: <Widget>[
  379. Text(
  380. 'Tài khoản',
  381. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
  382. ),
  383. _userNameField(),
  384. SizedBox(
  385. height: 8.0,
  386. ),
  387. _fullNameField(),
  388. SizedBox(
  389. height: 8.0,
  390. ),
  391. _emailField(),
  392. SizedBox(
  393. height: 8.0,
  394. ),
  395. Container(
  396. width: double.infinity,
  397. child: Text(
  398. "Quốc gia",
  399. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  400. ),
  401. ),
  402. _btnSelectCountry(),
  403. Container(
  404. width: double.infinity,
  405. child: Text(
  406. "Tỉnh/Thành phố",
  407. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  408. ),
  409. ),
  410. _btnSelectProvince(),
  411. Container(
  412. width: double.infinity,
  413. child: Text(
  414. "Quận/Huyện",
  415. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  416. ),
  417. ),
  418. _btnSelectDistrict(),
  419. Container(
  420. width: double.infinity,
  421. child: Text(
  422. "Phường/Xã",
  423. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  424. ),
  425. ),
  426. _btnSelectWard(),
  427. SizedBox(
  428. height: 8.0,
  429. ),
  430. _addressField(),
  431. SizedBox(
  432. height: 16.0,
  433. ),
  434. ButtonWidget(
  435. title: 'CẬP NHẬT',
  436. onPressed: () {
  437. FocusScopeNode currentFocus = FocusScope.of(context);
  438. if (!currentFocus.hasPrimaryFocus) {
  439. currentFocus.unfocus();
  440. }
  441. _validateInputs();
  442. }),
  443. ],
  444. ),
  445. ));
  446. } else {
  447. return Center(
  448. child: CircularProgressIndicator(),
  449. );
  450. }
  451. });
  452. }
  453. @override
  454. void dispose() {
  455. super.dispose();
  456. _userNameController.dispose();
  457. _emailController.dispose();
  458. _fullNameController.dispose();
  459. _addressController.dispose();
  460. }
  461. }