Files
fcs/lib/pages/signin/signin_page.dart

150 lines
4.2 KiB
Dart

import 'package:country_picker/country_picker.dart';
import 'package:fcs/domain/entities/auth_result.dart';
import 'package:fcs/domain/entities/auth_status.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/signin/signin_logic.dart';
import 'package:fcs/pages/widgets/local_app_bar.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import '../../helpers/theme.dart';
import '../main/util.dart';
import '../widgets/input_phone.dart';
import 'sms_code_page.dart';
class SigninPage extends StatefulWidget {
@override
_SigninPageState createState() => _SigninPageState();
}
class _SigninPageState extends State<SigninPage> {
bool _isLoading = false;
late String dialCode;
TextEditingController phonenumberCtl = new TextEditingController();
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
Widget build(BuildContext context) {
return LocalProgress(
inAsyncCall: _isLoading,
child: new Scaffold(appBar: LocalAppBar(), body: _buildLogin(context)),
);
}
Widget _buildLogin(BuildContext context) {
return ListView(
padding: EdgeInsets.only(top: 5, left: 15, right: 15),
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 40),
child: LocalText(
context,
'login.title',
fontSize: 21,
color: primaryColor,
fontWeight: FontWeight.bold,
),
),
Container(
padding: EdgeInsets.only(top: 25),
child: LocalText(
context,
'login.phone',
color: labelColor,
fontSize: 16,
),
),
InputPhone(
autofocus: true,
validationLabel: "login.phone_empty",
phoneCtl: phonenumberCtl,
selectedCountry: _selectedCountry,
onValueChange: (country) {
setState(() {
_selectedCountry = country;
});
},
),
SizedBox(
height: 20,
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
InkWell(
onTap: () => _next(),
child: CircleAvatar(
minRadius: 25,
backgroundColor: primaryColor,
child: Icon(
FontAwesomeIcons.arrowRight,
color: Colors.white,
),
),
)
],
),
),
],
);
}
_next() async {
if (phonenumberCtl.text.length < 5) {
showMsgDialog(context, "Error", "Invalid phone number");
return;
}
setState(() {
_isLoading = true;
});
bool isMyanmar = _selectedCountry.phoneCode == "95";
String dialCode = isMyanmar ? "+959" : "+${_selectedCountry.phoneCode}";
try {
String phoneNumber = dialCode + phonenumberCtl.text;
AuthResult auth = await context.read<MainModel>().sendSms(phoneNumber);
if (auth.authStatus == AuthStatus.SMS_SENT) {
await Navigator.of(context).push(CupertinoPageRoute(
builder: (context) => SmsCodePage(
phoneNumber: phoneNumber,
forceResendingToken: auth.forceResendingToken)));
Navigator.pop(context);
} else if (auth.authStatus == AuthStatus.AUTH_VERIFIED) {
await navigateAfterAuthVerified(context);
}
if (auth.authStatus == AuthStatus.ERROR) {
showMsgDialog(context, "Error", auth.authErrorMsg ?? "");
}
} catch (e) {
showMsgDialog(context, "Error", e.toString());
}
setState(() {
_isLoading = false;
});
}
}