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

192 lines
5.7 KiB
Dart
Raw Normal View History

2020-05-29 16:14:17 +06:30
import 'package:country_code_picker/country_code_picker.dart';
2020-10-07 02:33:06 +06:30
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_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
2020-10-14 13:54:42 +06:30
import 'package:flutter/cupertino.dart';
2020-05-29 16:14:17 +06:30
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
2020-08-30 21:26:37 +06:30
import 'package:provider/provider.dart';
2020-05-29 16:14:17 +06:30
2020-09-18 04:04:21 +06:30
import '../../helpers/theme.dart';
2020-10-07 02:33:06 +06:30
import '../main/util.dart';
2020-09-18 04:04:21 +06:30
import 'sms_code_page.dart';
2020-05-29 16:14:17 +06:30
class SigninPage extends StatefulWidget {
@override
_SigninPageState createState() => _SigninPageState();
}
class _SigninPageState extends State<SigninPage> {
bool _isLoading = false;
2021-09-10 12:02:08 +06:30
late String dialCode;
2020-05-29 16:14:17 +06:30
2021-09-10 12:02:08 +06:30
TextEditingController phonenumberCtl = new TextEditingController();
2020-05-29 16:14:17 +06:30
@override
void initState() {
super.initState();
phonenumberCtl = new TextEditingController(text: "09");
phonenumberCtl.selection = TextSelection.fromPosition(
TextPosition(offset: phonenumberCtl.text.length));
dialCode = "+95";
}
@override
Widget build(BuildContext context) {
return LocalProgress(
inAsyncCall: _isLoading,
child: new Scaffold(
appBar: AppBar(
2020-06-01 14:24:45 +06:30
centerTitle: true,
leading: new IconButton(
2020-10-14 13:54:42 +06:30
icon: new Icon(CupertinoIcons.back),
2020-06-01 14:24:45 +06:30
onPressed: () => Navigator.of(context).pop(),
),
2020-05-29 16:14:17 +06:30
backgroundColor: primaryColor,
),
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,
2020-06-01 14:24:45 +06:30
color: primaryColor,
2020-05-29 16:14:17 +06:30
fontWeight: FontWeight.bold,
),
),
Container(
padding: EdgeInsets.only(top: 25),
child: LocalText(
context,
'login.phone',
color: labelColor,
fontSize: 16,
),
),
Row(
children: <Widget>[
Container(
decoration: BoxDecoration(
2021-09-10 12:02:08 +06:30
border: Border.all(color: Colors.grey.shade400, width: 1),
2020-05-29 16:14:17 +06:30
borderRadius: BorderRadius.all(Radius.circular(12.0))),
child: CountryCodePicker(
onChanged: _countryChange,
initialSelection: dialCode,
2020-10-08 11:38:05 +06:30
countryFilter: ['mm', 'us'],
2020-05-29 16:14:17 +06:30
showCountryOnly: false,
showOnlyCountryWhenClosed: false,
alignLeft: false,
2021-09-13 11:20:35 +06:30
textStyle: TextStyle(fontSize: 16, color: Colors.black87),
2020-05-29 16:14:17 +06:30
),
),
SizedBox(
width: 10,
),
Flexible(
child: Container(
2020-06-01 14:24:45 +06:30
padding: EdgeInsets.only(top: 10, bottom: 10),
child: TextFormField(
controller: phonenumberCtl,
cursorColor: primaryColor,
textAlign: TextAlign.left,
autofocus: true,
keyboardType: TextInputType.phone,
style: TextStyle(
fontSize: 18,
),
decoration: new InputDecoration(
contentPadding: EdgeInsets.only(top: 8),
enabledBorder: UnderlineInputBorder(
2020-05-29 16:14:17 +06:30
borderSide:
2020-06-01 14:24:45 +06:30
BorderSide(color: primaryColor, width: 1.0)),
focusedBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: primaryColor, width: 1.0)),
),
),
),
2020-05-29 16:14:17 +06:30
),
],
),
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,
),
),
)
],
),
),
],
);
}
_countryChange(CountryCode countryCode) {
setState(() {
2021-09-10 12:02:08 +06:30
dialCode = countryCode.dialCode ?? '+95';
2020-05-29 16:14:17 +06:30
});
}
_next() async {
String phoneNumber = phonenumberCtl.text;
if (phoneNumber.length < 5) {
showMsgDialog(context, "Error", "Invalid phone number");
return;
}
2020-08-30 21:26:37 +06:30
setState(() {
_isLoading = true;
});
2020-05-29 16:14:17 +06:30
try {
phoneNumber = phoneNumber[0] == "0"
? phoneNumber.replaceFirst("0", "")
: phoneNumber;
phoneNumber = dialCode + phoneNumber;
2020-08-30 21:26:37 +06:30
2020-09-04 01:42:58 +06:30
AuthResult auth = await context.read<MainModel>().sendSms(phoneNumber);
2020-08-30 21:26:37 +06:30
if (auth.authStatus == AuthStatus.SMS_SENT) {
2020-10-14 13:54:42 +06:30
await Navigator.of(context).push(CupertinoPageRoute(
builder: (context) => SmsCodePage(phoneNumber: phoneNumber)));
2020-08-30 21:26:37 +06:30
Navigator.pop(context);
2020-09-11 16:14:36 +06:30
} else if (auth.authStatus == AuthStatus.AUTH_VERIFIED) {
2020-09-13 21:49:39 +06:30
await navigateAfterAuthVerified(context);
2020-08-30 21:26:37 +06:30
}
if (auth.authStatus == AuthStatus.ERROR) {
2021-09-10 16:33:52 +06:30
showMsgDialog(context, "Error", auth.authErrorMsg ?? "");
2020-08-30 21:26:37 +06:30
}
2020-05-29 16:14:17 +06:30
} catch (e) {
showMsgDialog(context, "Error", e.toString());
}
2020-08-30 21:26:37 +06:30
setState(() {
_isLoading = false;
});
2020-05-29 16:14:17 +06:30
}
}