Files
fcs/lib/pages/customer/invitation_create.dart

146 lines
3.9 KiB
Dart
Raw Normal View History

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