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/widget_loading.dart'; import 'package:farm_tpf/presentation/custom_widgets/widget_toast.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:fluttertoast/fluttertoast.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; FlutterToast flutterToast; Account _account = Account(); TextEditingController _userNameController = TextEditingController(); TextEditingController _fullNameController = TextEditingController(); TextEditingController _emailController = TextEditingController(); TextEditingController _addressController = TextEditingController(); var checkChangeLocation = Get.put(CheckChangeAnotherDropdown()); PackageInfo _packageInfo = PackageInfo( version: '1.0.0', buildNumber: '1.', ); Future _initPackageInfo() async { final PackageInfo info = await PackageInfo.fromPlatform(); setState(() { _packageInfo = info; }); } @override void initState() { super.initState(); checkChangeLocation.initValue(); _initPackageInfo(); flutterToast = FlutterToast(context); 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) { flutterToast.showToast(child: WidgetToast(message: "Lỗi tải dữ liệu")); }); } _validateInputs() async { if (_formKey.currentState.validate()) { _formKey.currentState.save(); LoadingDialog.showLoadingDialog(_scaffoldKey.currentContext); _repository.updateProfile(_account).then((value) { LoadingDialog.hideLoadingDialog(_scaffoldKey.currentContext); _scaffoldKey.currentState.showSnackBar(SnackBar( content: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Cập nhật thành công.'), Icon(Icons.done), ], ), backgroundColor: Colors.green, duration: Duration(seconds: 3), )); }).catchError((onError) { _scaffoldKey.currentState.showSnackBar(SnackBar( content: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Cập nhật không thành công.'), Icon(Icons.error), ], ), backgroundColor: Colors.red, duration: Duration(seconds: 3), )); 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 { Get.snackbar(label_country_empty, label_country_empty_message, snackPosition: SnackPosition.BOTTOM); } }, 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 { Get.snackbar(label_province_empty, label_province_empty_message, snackPosition: SnackPosition.BOTTOM); } }, 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 { Get.snackbar(label_district_empty, label_district_empty_message, snackPosition: SnackPosition.BOTTOM); } }, 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; }, ); } Widget _btnChangePassword() { return FlatButton( padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0), onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => ChangePasswordScreen()), ); }, child: Container( padding: EdgeInsets.only(top: 15.0, right: 0.0, bottom: 10.5, left: 0.0), decoration: BoxDecoration( border: Border(bottom: BorderSide(width: 0.5, color: Colors.grey)), ), child: Row( children: [ Expanded( child: Text( "Cập nhật mật khẩu".toUpperCase(), style: TextStyle( fontSize: 14.0, color: Colors.black, fontWeight: FontWeight.bold), )), Icon(Icons.arrow_forward_ios), ], ))); } Widget _textPackageInfo() { return Container( width: double.infinity, alignment: Alignment.centerRight, child: Text( "version:${_packageInfo.version}.${_packageInfo.buildNumber}", style: TextStyle(color: COLOR_CONST.GRAY1_70))); } @override Widget build(BuildContext context) => KeyboardDismisser( child: Scaffold( appBar: AppBar( centerTitle: true, title: Text( "Thông tin cá nhân", textAlign: TextAlign.center, ), actions: [ IconButton( icon: Icon(Icons.done), onPressed: () { FocusScopeNode currentFocus = FocusScope.of(context); if (!currentFocus.hasPrimaryFocus) { currentFocus.unfocus(); } _validateInputs(); }) ], ), 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( children: [ _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, ), _btnChangePassword(), SizedBox( height: 8, ), _textPackageInfo() ], ), )); } else { return Center( child: CircularProgressIndicator(), ); } }); } @override void dispose() { super.dispose(); _userNameController.dispose(); _emailController.dispose(); _fullNameController.dispose(); _addressController.dispose(); } }