import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:provider/provider.dart'; import 'package:fcs/model/language_model.dart'; import 'package:fcs/model/user_model.dart'; import 'package:fcs/vo/user.dart'; import 'package:fcs/widget/local_text.dart'; import 'package:fcs/widget/localization/app_translations.dart'; import 'package:fcs/widget/progress.dart'; import '../fcs/common/theme.dart' as Theme; import 'confirm_email.dart'; import 'util.dart'; class ChangePhoneNumber extends StatefulWidget { final User user; ChangePhoneNumber( this.user, { Key key, }) : super(key: key); @override _ChangePhoneNumberState createState() => new _ChangePhoneNumberState(); } class _ChangePhoneNumberState extends State with SingleTickerProviderStateMixin { final GlobalKey _scaffoldKey = new GlobalKey(); final FocusNode myFocusNodePhone = FocusNode(); final FocusNode myFocusNodenewPhone = FocusNode(); TextEditingController _phoneController = new TextEditingController(); TextEditingController _newPhoneController = new TextEditingController(); final formKey = GlobalKey(); bool _isLoading = false; @override Widget build(BuildContext context) { return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( key: _scaffoldKey, body: SingleChildScrollView( child: Container( width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height >= 775.0 ? MediaQuery.of(context).size.height : 580.0, child: Column( mainAxisSize: MainAxisSize.max, children: [ Padding( padding: EdgeInsets.only(top: 35.0, bottom: 10), child: ListTile( leading: IconButton( icon: Icon(Icons.arrow_back), onPressed: () { Navigator.of(context).pop(); }, ), title: LocalText( context, 'change.phone', color: Colors.black87, fontSize: 17, ), ), ), Expanded( flex: 2, child: PageView( children: [ new ConstrainedBox( constraints: const BoxConstraints.expand(), child: _buildReset(context), ), ], ), ), ], ), ), ), ), ); } @override void dispose() { myFocusNodenewPhone.dispose(); myFocusNodePhone.dispose(); super.dispose(); } @override void initState() { super.initState(); _phoneController.text = widget.user.phone; _newPhoneController.text = "09"; // SystemChrome.setPreferredOrientations([ // DeviceOrientation.portraitUp, // DeviceOrientation.portraitDown, // ]); } Widget _buildReset(BuildContext context) { return Container( child: ListView( children: [ Column( children: [ Form( key: formKey, child: Card( elevation: 2.0, color: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), child: Container( width: 300.0, child: Column( children: [ Padding( padding: EdgeInsets.only(left: 25.0, right: 25.0), child: TextFormField( focusNode: myFocusNodePhone, controller: _phoneController, readOnly: true, style: TextStyle( fontFamily: "WorkSansSemiBold", fontSize: 16.0, color: Colors.black), decoration: InputDecoration( border: InputBorder.none, icon: Icon( FontAwesomeIcons.phone, color: Colors.black, size: 22.0, ), labelText: AppTranslations.of(context) .text("login.phone"), labelStyle: Provider.of(context).isEng ? TextStyle( fontFamily: "WorkSansSemiBold", color: Colors.grey) : TextStyle( fontFamily: "MyanmarUnicode", color: Colors.grey), ), ), ), Container( width: 250.0, height: 1.0, color: Colors.grey[400], ), Padding( padding: EdgeInsets.only(left: 25.0, right: 25.0), child: TextFormField( focusNode: myFocusNodenewPhone, controller: _newPhoneController, keyboardType: TextInputType.phone, style: TextStyle( fontFamily: "WorkSansSemiBold", fontSize: 16.0, color: Colors.black), decoration: InputDecoration( border: InputBorder.none, icon: Icon( FontAwesomeIcons.phone, color: Colors.black, size: 22.0, ), labelText: AppTranslations.of(context) .text("change.new.phone"), labelStyle: Provider.of(context).isEng ? TextStyle( fontFamily: "WorkSansSemiBold", color: Colors.grey) : TextStyle( fontFamily: "MyanmarUnicode", color: Colors.grey), ), validator: _validatePhone, ), ), ], ), ), ), ), SizedBox( height: 15, ), Container( // margin: EdgeInsets.only(top: 320.0), decoration: new BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(5.0)), color: Theme.primaryColor, ), child: MaterialButton( highlightColor: Colors.transparent, splashColor: Theme.LoginColors.loginGradientEnd, //shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(5.0))), child: Padding( padding: const EdgeInsets.symmetric( vertical: 10.0, horizontal: 42.0), child: LocalText( context, 'change', color: Colors.white, fontSize: 18.0, ), ), onPressed: () => _change(context)), ), ], ), ], ), ); } void _change(BuildContext context) async { if (!formKey.currentState.validate()) { return; } var _phone = _newPhoneController.text; setState(() { _isLoading = true; }); UserModel userModel = Provider.of(context); try { await userModel.changePhone(widget.user.phoneNumber, _phone); Navigator.push( context, MaterialPageRoute( builder: (context) => ConfirmEmail( id: widget.user.phoneNumber, phoneNumber: _phone, ))); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { Future.delayed(Duration(seconds: 1), () { if (mounted) { setState(() { _isLoading = false; }); } }); } } String _validatePhone(value) { if (value.isEmpty) { return AppTranslations.of(context).text("change.phone_empty"); } if (!value.startsWith("09")) { return 'Only "09".'; } return null; } }