// ignore_for_file: use_build_context_synchronously import 'package:country_picker/country_picker.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../../domain/entities/auth_result.dart'; import '../../domain/entities/auth_status.dart'; import '../../domain/entities/user.dart'; import '../main/model/main_model.dart'; import '../main/util.dart'; import '../widgets/display_text.dart'; import '../widgets/input_phone.dart'; import '../widgets/local_app_bar.dart'; import '../widgets/progress.dart'; import 'confirm_phone_number.dart'; class ChangePhoneNumber extends StatefulWidget { final User user; const ChangePhoneNumber({super.key, required this.user}); @override State createState() => _ChangePhoneNumberState(); } class _ChangePhoneNumberState extends State { bool _isLoading = false; TextEditingController _newPhoneCtl = TextEditingController(); late Country _selectedCountry; final _formKey = GlobalKey(); @override void initState() { _selectedCountry = Country( name: 'Myanmar', countryCode: 'MM', displayName: 'Myanmar(MM)', displayNameNoCountryCode: 'Myanmar', e164Key: '', e164Sc: -1, example: '', geographic: false, level: -1, phoneCode: '95'); super.initState(); } @override Widget build(BuildContext context) { final currentPhoneBox = DisplayText( text: widget.user.phone, labelTextKey: "profile.current_phone", iconData: Icons.phone); final newPhoneBox = InputPhone( autofocus: true, lableKey: "profile.new_phone", validationLabel: "profile.new_phone_empty", phoneCtl: _newPhoneCtl, selectedCountry: _selectedCountry, onValueChange: (country) { setState(() { _selectedCountry = country; }); }, ); final sendBtn = Padding( padding: const EdgeInsets.symmetric(horizontal: 30), child: fcsButton( context, getLocalString(context, 'profile.send_sms'), callack: _sendSMS, ), ); return Form( key: _formKey, child: LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: const LocalAppBar( labelKey: 'profile.change_phone.title', backgroundColor: Colors.white, labelColor: primaryColor, arrowColor: primaryColor, ), body: ListView( padding: const EdgeInsets.only(left: 15, right: 15, top: 10), children: [ currentPhoneBox, const SizedBox(height: 15), newPhoneBox, const SizedBox(height: 30), sendBtn ], ), ), ), ); } Future _sendSMS() async { if (_formKey.currentState != null && !_formKey.currentState!.validate()) { return; } if (_newPhoneCtl.text.length < 5) { showMsgDialog(context, "Error", "Invalid phone number"); return; } setState(() { _isLoading = true; }); try { bool isMyanmar = _selectedCountry.phoneCode == "95"; String dialCode = isMyanmar ? "+959" : "+${_selectedCountry.phoneCode}"; String phoneNumber = dialCode + _newPhoneCtl.text; AuthResult auth = await context.read().sendSms(phoneNumber); if (auth.authStatus == AuthStatus.SMS_SENT) { bool? updated = await Navigator.of(context, rootNavigator: true).push( CupertinoPageRoute( builder: (context) => ConfirmPhoneNumber( phoneNumber: phoneNumber, forceResendingToken: auth.forceResendingToken))); if (updated ?? false) { Navigator.pop(context, true); } } } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } }