import 'dart:ui'; import 'package:country_code_picker/country_code_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'; class InvitationCreate extends StatefulWidget { @override _InvitationCreateState createState() => _InvitationCreateState(); } class _InvitationCreateState extends State { TextEditingController _nameController = new TextEditingController(); TextEditingController _phoneController = new TextEditingController(); bool _isLoading = false; late String dialCode; @override void initState() { super.initState(); dialCode = "+95"; } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { final nameBox = InputText( labelTextKey: 'customer.name', iconData: Icons.person, controller: _nameController); 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: Container( padding: EdgeInsets.all(18), child: ListView( children: [ nameBox, SizedBox(height: 10), Row( children: [ Padding( padding: const EdgeInsets.all(8.0), child: Icon( Icons.phone, color: primaryColor, ), ), Container( decoration: BoxDecoration( border: Border.all(color: Colors.grey.shade400, width: 1), borderRadius: BorderRadius.all(Radius.circular(12.0))), child: CountryCodePicker( onChanged: _countryChange, initialSelection: dialCode, countryFilter: ['mm', 'us'], showCountryOnly: false, showOnlyCountryWhenClosed: false, alignLeft: false, textStyle: TextStyle(fontSize: 16, color: Colors.black87), searchDecoration: InputDecoration( focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.black, width: 1.0))), ), ), SizedBox( width: 10, ), Flexible( child: Container( padding: EdgeInsets.only(top: 10, bottom: 10), child: TextFormField( controller: _phoneController, cursorColor: primaryColor, textAlign: TextAlign.left, keyboardType: TextInputType.phone, style: TextStyle( fontSize: 18, ), decoration: InputDecoration( fillColor: Colors.white, labelText: getLocalString(context, "customer.phone"), labelStyle: TextStyle(fontSize: 16, color: Colors.grey), filled: true, focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.grey, width: 1.0)), ), ), ), ), ], ), SizedBox(height: 20), fcsButton(context, getLocalString(context, "invite.btn"), callack: _invite), ], ), ), ), ); } _countryChange(CountryCode countryCode) { setState(() { dialCode = countryCode.dialCode!; }); } _invite() async { String userName = _nameController.text; String phoneNumber = dialCode + _phoneController.text; if (userName == "" || phoneNumber == "") { showMsgDialog(context, "Error", "Invalid name or phone number"); return; } setState(() { _isLoading = true; }); try { 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 != ""; } }