import 'package:country_picker/country_picker.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/customer/model/customer_model.dart'; import 'package:fcs/pages/main/util.dart'; import 'package:fcs/pages/widgets/input_text.dart'; import 'package:fcs/pages/widgets/local_app_bar.dart'; import 'package:fcs/pages/widgets/progress.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../widgets/input_phone.dart'; class InvitationCreate extends StatefulWidget { @override _InvitationCreateState createState() => _InvitationCreateState(); } class _InvitationCreateState extends State { TextEditingController _nameController = new TextEditingController(); TextEditingController _phoneController = new TextEditingController(); bool _isLoading = false; final _inviteFormKey = GlobalKey(); late Country _selectedCountry; @override void initState() { super.initState(); _selectedCountry = Country( name: 'Myanmar', countryCode: 'MM', displayName: 'Myanmar(MM)', displayNameNoCountryCode: 'Myanmar', e164Key: '', e164Sc: -1, example: '', geographic: false, level: -1, phoneCode: '95'); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { final nameBox = InputText( labelTextKey: 'customer.name', iconData: Icons.person, controller: _nameController, autovalidateMode: AutovalidateMode.onUserInteraction, validator: (value) { if (value == null || value.isEmpty) { return "Please insert name"; } return null; }, ); final phoneBox = InputPhone( lableKey: "customer.phone", validationLabel: "customer.phone_empty", phoneCtl: _phoneController, selectedCountry: _selectedCountry, onValueChange: (country) { setState(() { _selectedCountry = country; }); }, ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: LocalAppBar( labelKey: "invitation.form.title", backgroundColor: Colors.white, labelColor: primaryColor, arrowColor: primaryColor, onBack: () { if (isDataChanged()) { showConfirmDialog(context, "back.button_confirm", () { Navigator.of(context).pop(); }); } else { Navigator.of(context).pop(); } }, ), body: Form( key: _inviteFormKey, child: Container( padding: EdgeInsets.all(18), child: ListView( children: [ nameBox, SizedBox(height: 10), phoneBox, SizedBox(height: 30), fcsButton(context, getLocalString(context, "invite.btn"), callack: _invite), ], ), ), ), ), ); } _invite() async { if (!_inviteFormKey.currentState!.validate()) { return null; } setState(() { _isLoading = true; }); try { String userName = _nameController.text; bool isMyanmar = _selectedCountry.phoneCode == "95"; String dialCode = isMyanmar ? "+959" : "+${_selectedCountry.phoneCode}"; String phoneNumber = dialCode + _phoneController.text; CustomerModel customerModel = Provider.of(context, listen: false); await customerModel.inviteUser(userName, phoneNumber); Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } isDataChanged() { String userName = _nameController.text; String phoneNumber = _phoneController.text; return userName != "" || phoneNumber != ""; } }