147 lines
4.3 KiB
Dart
147 lines
4.3 KiB
Dart
// lib/features/auth/screens/verification_screen.dart
|
|
|
|
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:kmobile/features/auth/controllers/auth_cubit.dart';
|
|
import 'package:kmobile/features/auth/controllers/auth_state.dart';
|
|
import 'package:kmobile/features/auth/screens/mpin_screen.dart';
|
|
import 'package:kmobile/features/auth/screens/sms_verification_helper.dart';
|
|
import '../../../app.dart';
|
|
|
|
class VerificationScreen extends StatefulWidget {
|
|
final String customerNo;
|
|
final String password;
|
|
|
|
const VerificationScreen({
|
|
super.key,
|
|
required this.customerNo,
|
|
required this.password,
|
|
});
|
|
|
|
@override
|
|
State<VerificationScreen> createState() => _VerificationScreenState();
|
|
}
|
|
|
|
class _VerificationScreenState extends State<VerificationScreen> {
|
|
final SmsVerificationHelper _smsVerificationHelper = SmsVerificationHelper();
|
|
late Timer _timer;
|
|
int _start = 120;
|
|
String _message = "Attempting verification...";
|
|
String? _error;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
context.read<AuthCubit>().startVerification();
|
|
startTimer();
|
|
_verifySmsAndLogin();
|
|
}
|
|
|
|
void startTimer() {
|
|
const oneSec = Duration(seconds: 1);
|
|
_timer = Timer.periodic(
|
|
oneSec,
|
|
(Timer timer) {
|
|
if (_start == 0) {
|
|
timer.cancel();
|
|
if (mounted) {
|
|
setState(() {
|
|
_error = "Verification timed out.";
|
|
});
|
|
}
|
|
} else {
|
|
setState(() {
|
|
_start--;
|
|
});
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> _verifySmsAndLogin() async {
|
|
await _smsVerificationHelper.initiateSmsSequence(context: context);
|
|
// After SMS sequence completes, proceed with login
|
|
_timer.cancel(); // Stop the timer
|
|
if (mounted) {
|
|
context.read<AuthCubit>().login(widget.customerNo, widget.password);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_timer.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: BlocListener<AuthCubit, AuthState>(
|
|
listenWhen: (previous, current) {
|
|
return current is! AuthVerificationInProgress && current is! AuthInitial;
|
|
},
|
|
listener: (context, state) {
|
|
if (state is Authenticated) {
|
|
_timer.cancel();
|
|
Navigator.of(context).pushReplacement(
|
|
MaterialPageRoute(
|
|
builder: (_) => MPinScreen(
|
|
mode: MPinMode.set,
|
|
onCompleted: (_) {
|
|
Navigator.of(
|
|
context,
|
|
rootNavigator: true,
|
|
).pushReplacement(
|
|
MaterialPageRoute(
|
|
builder: (_) => const NavigationScaffold(),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
} else if (state is AuthError) {
|
|
_timer.cancel();
|
|
setState(() {
|
|
_error = state.message;
|
|
});
|
|
}
|
|
},
|
|
child: Center(
|
|
child: _error != null
|
|
? Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(Icons.error, color: Colors.red, size: 80),
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
_error!,
|
|
textAlign: TextAlign.center,
|
|
style: const TextStyle(color: Colors.red, fontSize: 18),
|
|
),
|
|
const SizedBox(height: 24),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: const Text("Back to Login"),
|
|
)
|
|
],
|
|
)
|
|
: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const CircularProgressIndicator(),
|
|
const SizedBox(height: 16),
|
|
Text(_message),
|
|
const SizedBox(height: 16),
|
|
Text("Time remaining: $_start seconds"),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|