Files
fcs/lib/pages/profile/confirm_phone_number.dart

233 lines
7.1 KiB
Dart
Raw Normal View History

// ignore_for_file: use_build_context_synchronously
import 'dart:async';
import 'package:fcs/constants.dart';
import 'package:fcs/pages/widgets/local_app_bar.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/material.dart';
import 'package:pin_input_text_field/pin_input_text_field.dart';
import 'package:provider/provider.dart';
import '../../domain/entities/auth_result.dart';
import '../../domain/entities/auth_status.dart';
import '../../helpers/theme.dart';
import '../main/model/main_model.dart';
import '../main/util.dart';
import '../widgets/local_text.dart';
class ConfirmPhoneNumber extends StatefulWidget {
final String phoneNumber;
final String? forceResendingToken;
const ConfirmPhoneNumber(
{super.key, required this.phoneNumber, this.forceResendingToken});
@override
State<ConfirmPhoneNumber> createState() => _ConfirmPhoneNumberState();
}
class _ConfirmPhoneNumberState extends State<ConfirmPhoneNumber> {
final _formKey = GlobalKey<FormState>();
bool _isLoading = false;
late String pin;
late bool allNumberEntered;
late Timer _timer;
int _start = resendCountSec;
bool canResend = false;
String? _forceResendingToken;
@override
void initState() {
pin = "";
allNumberEntered = false;
_forceResendingToken = widget.forceResendingToken;
super.initState();
startTimer();
}
void startTimer() {
_timer = Timer.periodic(
const Duration(seconds: 1),
(t) => setState(
() {
if (_start < 1) {
t.cancel();
canResend = true;
} else {
_start = _start - 1;
}
},
),
);
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
bool isMyanmar = widget.phoneNumber.startsWith("+959");
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: const LocalAppBar(
labelKey: 'profile.confirm_new_phone.title',
backgroundColor: Colors.white,
labelColor: primaryColor,
arrowColor: primaryColor),
body: Form(
key: _formKey,
child: ListView(
padding: const EdgeInsets.only(left: 15, right: 15, top: 10),
children: [
Container(
padding: const EdgeInsets.only(top: 40),
child: Row(
children: [
Flexible(
child: LocalText(context, "sms.six.digit",
fontSize: 16, color: labelColor),
),
],
),
),
Text(
widget.phoneNumber.startsWith("+959")
? "0${widget.phoneNumber.substring(3)}"
: widget.phoneNumber,
style: const TextStyle(color: primaryColor, fontSize: 16),
),
const SizedBox(
height: 30,
),
LocalText(context, "sms.code", color: labelColor, fontSize: 16),
Container(
padding: const EdgeInsets.only(top: 10),
child: PinInputTextField(
pinLength: 6,
cursor: Cursor(
color: primaryColor,
enabled: true,
width: 2,
height: 23),
decoration: BoxLooseDecoration(
textStyle: TextStyle(
color:
Theme.of(context).textTheme.labelMedium!.color,
fontSize: 20),
strokeColorBuilder:
const FixedColorBuilder(labelColor)),
textInputAction: TextInputAction.done,
autoFocus: true,
onChanged: _pinChange,
),
),
const SizedBox(height: 40),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 30),
child: fcsButton(
context,
getLocalString(context, 'profile.change_phone.btn'),
callack: allNumberEntered
? () {
_change();
}
: null,
),
),
const SizedBox(height: 30),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor:
canResend ? Colors.white : Colors.grey,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
side: BorderSide(
color: canResend
? primaryColor
: Colors.grey.shade400))),
onPressed: canResend
? () {
_resend(isMyanmar);
}
: null,
child: LocalText(context, "sms.resend",
color:
canResend ? primaryColor : Colors.grey.shade400,
fontSize: 16),
),
],
),
Row(
children: <Widget>[
LocalText(context, 'sms.resend.seconds',
fontSize: 15,
translationVariables: [_start.toString()],
color: primaryColor),
],
),
const SizedBox(height: 20),
],
)),
),
);
}
_pinChange(pin) {
setState(() {
this.pin = pin;
allNumberEntered = this.pin.length == 6;
});
}
_resend(bool isMyanmar) async {
try {
setState(() {
_start = resendCountSec;
canResend = false;
});
_timer.cancel();
startTimer();
var mainModel = context.read<MainModel>();
AuthResult auth = await mainModel.sendSms(widget.phoneNumber,
forceResendingToken: _forceResendingToken);
if (auth.authStatus == AuthStatus.SMS_SENT) {
_forceResendingToken = auth.forceResendingToken;
}
} catch (e) {
await showMsgDialog(context, "Error", e.toString());
}
}
_change() async {
setState(() {
_isLoading = true;
});
try {
// var mainModel = context.read<MainModel>();
// await mainModel.updatePhoneNumber(pin);
Navigator.pop(context, true);
} catch (e) {
await showMsgDialog(context, "Error", e.toString());
} finally {
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
}
}