import 'package:farm_tpf/custom_model/LocationUnit.dart'; import 'package:farm_tpf/custom_model/account.dart'; import 'package:farm_tpf/data/repository/user_repository.dart'; import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart'; import 'package:farm_tpf/presentation/custom_widgets/button_widget.dart'; import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart'; import 'package:farm_tpf/presentation/custom_widgets/widget_toast.dart'; import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart'; import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_supply.dart'; import 'package:farm_tpf/presentation/screens/location_unit/sc_location.dart'; import 'package:farm_tpf/presentation/screens/profile/controller/check_change_another_dropdown.dart'; import 'package:farm_tpf/presentation/screens/profile/sc_change_password.dart'; import 'package:farm_tpf/utils/const_color.dart'; import 'package:farm_tpf/utils/const_common.dart'; import 'package:farm_tpf/utils/const_string.dart'; import 'package:farm_tpf/utils/const_style.dart'; import 'package:farm_tpf/utils/validators.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:keyboard_dismisser/keyboard_dismisser.dart'; import 'package:package_info/package_info.dart'; import 'bloc_get_account.dart'; class UpdateProfileScreen extends StatefulWidget { static Route route() { return MaterialPageRoute(builder: (_) => UpdateProfileScreen()); } @override _UpdateProfileScreenState createState() => _UpdateProfileScreenState(); } class _UpdateProfileScreenState extends State { final GlobalKey _scaffoldKey = new GlobalKey(); final _repository = UserRepository(); GlobalKey _formKey = GlobalKey(); bool _autoValidate = false; Account _account = Account(); TextEditingController _userNameController = TextEditingController(); TextEditingController _fullNameController = TextEditingController(); TextEditingController _emailController = TextEditingController(); TextEditingController _addressController = TextEditingController(); var checkChangeLocation = Get.put(CheckChangeAnotherDropdown()); @override void initState() { super.initState(); checkChangeLocation.initValue(); getAccountBloc.getAccount((data) { _account = data; _userNameController.text = _account.login; _fullNameController.text = _account.fullName.toString(); _emailController.text = _account.email.toString(); _addressController.text = _account.address; checkChangeLocation.changeCountryByIdAndName( _account.countryId, _account.countryName); checkChangeLocation.changeProvinceByIdAndName( _account.cityId, _account.cityName); checkChangeLocation.changeDistrictByIdAndName( _account.districtId, _account.districtName); checkChangeLocation.changeWardByIdAndName( _account.wardId, _account.wardName); }, (err) { Utils.showSnackBarError(message: "Lỗi tải dữ liệu"); }); } _validateInputs() async { if (_formKey.currentState.validate()) { _formKey.currentState.save(); _account.countryId = checkChangeLocation.currentCountry.id; _account.cityId = checkChangeLocation.currentProvince.id; _account.districtId = checkChangeLocation.currentDistrict.id; _account.wardId = checkChangeLocation.currentWard.id; LoadingDialog.showLoadingDialog(_scaffoldKey.currentContext); _repository.updateProfile(_account).then((value) { LoadingDialog.hideLoadingDialog(_scaffoldKey.currentContext); Utils.showSnackBarSuccess(message: 'Cập nhật thành công.'); }).catchError((onError) { Utils.showSnackBarError(message: 'Cập nhật không thành công.'); LoadingDialog.hideLoadingDialog(_scaffoldKey.currentContext); print("error"); }); } else { _autoValidate = true; } } Widget _userNameField() { return TextFormField( keyboardType: TextInputType.text, enabled: false, decoration: InputDecoration(labelText: "Tài khoản"), controller: _userNameController, validator: (String value) { return Validators.validateNotNullOrEmpty(value, "Tài khoản"); }, onSaved: (newValue) {}, ); } Widget _fullNameField() { return TextFormField( keyboardType: TextInputType.text, decoration: InputDecoration(labelText: "Họ và tên"), controller: _fullNameController, validator: (String value) { return Validators.validateNotNullOrEmpty(value, "Họ và tên"); }, onSaved: (newValue) { _account.fullName = newValue; }, ); } Widget _emailField() { return TextFormField( keyboardType: TextInputType.emailAddress, decoration: InputDecoration(labelText: "Email"), controller: _emailController, validator: (String value) { return Validators.validateEmail(value); }, onSaved: (newValue) { _account.email = newValue; }, ); } Widget _btnSelectCountry() { return GetBuilder(builder: (data) { return FlatButton( padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0), onPressed: () { if (Get.isSnackbarOpen) { Get.back(); } Navigator.of(context) .push(MaterialPageRoute( builder: (_) => LocationScreen( titleName: "Quốc gia", type: LocationType.country, filterId: null, selectedId: checkChangeLocation.currentCountry.id), fullscreenDialog: false)) .then((value) { if (value != null) { var result = value as LocationUnit; checkChangeLocation.changeCountry(result); } }); }, child: Container( padding: EdgeInsets.only( top: 0.0, right: 0.0, bottom: 10.5, left: 0.0), decoration: BoxDecoration( border: kBorderTextField, ), child: Row( children: [ GetBuilder( builder: (_) => Expanded( child: Text( checkChangeLocation.currentCountry.name ?? "Quốc gia", style: TextStyle( fontSize: 14.0, color: Colors.black87)))), Icon( Icons.arrow_drop_down, color: Colors.grey, ), ], ))); }); } Widget _btnSelectProvince() { return GetBuilder(builder: (data) { return FlatButton( padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0), onPressed: () { if (Get.isSnackbarOpen) { Get.back(); } if (checkChangeLocation.currentCountry.id != null) { Navigator.of(context) .push(MaterialPageRoute( builder: (_) => LocationScreen( titleName: "Tỉnh/Thành phố", type: LocationType.province, filterId: checkChangeLocation.currentCountry.id, selectedId: checkChangeLocation.currentProvince.id), fullscreenDialog: false)) .then((value) { if (value != null) { var result = value as LocationUnit; checkChangeLocation.changeProvince(result); } }); } else { Utils.showSnackBarWarning(message: label_country_empty); } }, child: Container( padding: EdgeInsets.only( top: 0.0, right: 0.0, bottom: 10.5, left: 0.0), decoration: BoxDecoration( border: kBorderTextField, ), child: Row( children: [ GetBuilder( builder: (_) => Expanded( child: Text( checkChangeLocation.currentProvince.name ?? "Tỉnh/Thành Phố", style: TextStyle( fontSize: 14.0, color: Colors.black87)))), Icon( Icons.arrow_drop_down, color: Colors.grey, ), ], ))); }); } Widget _btnSelectDistrict() { return GetBuilder(builder: (data) { return FlatButton( padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0), onPressed: () { if (Get.isSnackbarOpen) Get.back(); if (checkChangeLocation.currentProvince.id != null) { Navigator.of(context) .push(MaterialPageRoute( builder: (_) => LocationScreen( titleName: "Quận/Huyện", type: LocationType.district, filterId: checkChangeLocation.currentProvince.id, selectedId: checkChangeLocation.currentDistrict.id), fullscreenDialog: false)) .then((value) { if (value != null) { var result = value as LocationUnit; checkChangeLocation.changeDistrict(result); } }); } else { Utils.showSnackBarWarning(message: label_province_empty); } }, child: Container( padding: EdgeInsets.only( top: 0.0, right: 0.0, bottom: 10.5, left: 0.0), decoration: BoxDecoration( border: kBorderTextField, ), child: Row( children: [ GetBuilder( builder: (_) => Expanded( child: Text( checkChangeLocation.currentDistrict.name ?? "Quận/Huyện", style: TextStyle( fontSize: 14.0, color: Colors.black87)))), Icon( Icons.arrow_drop_down, color: Colors.grey, ), ], ))); }); } Widget _btnSelectWard() { return GetBuilder(builder: (data) { return FlatButton( padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0), onPressed: () { if (Get.isSnackbarOpen) Get.back(); if (checkChangeLocation.currentDistrict.id != null) { Navigator.of(context) .push(MaterialPageRoute( builder: (_) => LocationScreen( titleName: "Phường/Xã", type: LocationType.ward, filterId: checkChangeLocation.currentDistrict.id, selectedId: checkChangeLocation.currentWard.id), fullscreenDialog: false)) .then((value) { if (value != null) { var result = value as LocationUnit; checkChangeLocation.changeWard(result); } }); } else { Utils.showSnackBarWarning(message: label_district_empty); } }, child: Container( padding: EdgeInsets.only( top: 0.0, right: 0.0, bottom: 10.5, left: 0.0), decoration: BoxDecoration( border: kBorderTextField, ), child: Row( children: [ GetBuilder( builder: (_) => Expanded( child: Text( checkChangeLocation.currentWard.name ?? "Phường/Xã", style: TextStyle( fontSize: 14.0, color: Colors.black87)))), Icon( Icons.arrow_drop_down, color: Colors.grey, ), ], ))); }); } Widget _addressField() { return TextFormField( keyboardType: TextInputType.text, decoration: InputDecoration(labelText: "Địa chỉ"), controller: _addressController, onSaved: (newValue) { _account.address = newValue; }, ); } @override Widget build(BuildContext context) => KeyboardDismisser( child: Scaffold( backgroundColor: Colors.white, appBar: AppBarWidget( isBack: true, action: InkWell( child: Text( 'Huỷ', style: TextStyle(color: Colors.red, fontWeight: FontWeight.normal), ), onTap: () { if (Get.isSnackbarOpen) Get.back(); Get.back(); }, ), ), key: _scaffoldKey, body: _buildContent())); Widget _buildContent() { return StreamBuilder( stream: getAccountBloc.actions, builder: (context, AsyncSnapshot snapshot) { if (snapshot.hasData) { return Form( key: _formKey, autovalidate: _autoValidate, child: SingleChildScrollView( padding: EdgeInsets.all(8.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Tài khoản', style: TextStyle( fontWeight: FontWeight.w500, fontSize: 22), ), _userNameField(), SizedBox( height: 8.0, ), _fullNameField(), SizedBox( height: 8.0, ), _emailField(), SizedBox( height: 8.0, ), Container( width: double.infinity, child: Text( "Quốc gia", style: TextStyle(color: Colors.black54, fontSize: 13.0), ), ), _btnSelectCountry(), Container( width: double.infinity, child: Text( "Tỉnh/Thành phố", style: TextStyle(color: Colors.black54, fontSize: 13.0), ), ), _btnSelectProvince(), Container( width: double.infinity, child: Text( "Quận/Huyện", style: TextStyle(color: Colors.black54, fontSize: 13.0), ), ), _btnSelectDistrict(), Container( width: double.infinity, child: Text( "Phường/Xã", style: TextStyle(color: Colors.black54, fontSize: 13.0), ), ), _btnSelectWard(), SizedBox( height: 8.0, ), _addressField(), SizedBox( height: 16.0, ), ButtonWidget( title: 'CẬP NHẬT', onPressed: () { FocusScopeNode currentFocus = FocusScope.of(context); if (!currentFocus.hasPrimaryFocus) { currentFocus.unfocus(); } _validateInputs(); }), ], ), )); } else { return Center( child: CircularProgressIndicator(), ); } }); } @override void dispose() { super.dispose(); _userNameController.dispose(); _emailController.dispose(); _fullNameController.dispose(); _addressController.dispose(); } }