Change TPIn #4

This commit is contained in:
2025-11-08 20:07:04 +05:30
parent c26cc507a1
commit b5b6c6ed49
3 changed files with 171 additions and 189 deletions

View File

@@ -1,17 +1,16 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:kmobile/di/injection.dart';
import 'package:kmobile/features/dashboard/screens/dashboard_screen.dart';
import 'package:kmobile/widgets/pin_input_field.dart';
import '../../../api/services/change_password_service.dart';
class ChangeTpinOtpScreen extends StatefulWidget {
final String oldTpin;
final String newTpin;
final String mobileNumber; // Receive mobile number
final String mobileNumber;
const ChangeTpinOtpScreen({
super.key,
required this.oldTpin,
required this.newTpin,
required this.mobileNumber,
});
@@ -22,7 +21,8 @@
class _ChangeTpinOtpScreenState extends State<ChangeTpinOtpScreen> {
final _otpController = TextEditingController();
final ChangePasswordService _changePasswordService = getIt<ChangePasswordService>();
final ChangePasswordService _changePasswordService =
getIt<ChangePasswordService>();
bool _isLoading = false;
void _handleVerifyOtp() async {
@@ -37,44 +37,43 @@
_isLoading = true;
});
String message = 'An unknown error occurred.';
bool success = false;
try {
// 1. Validate the OTP
final responseString = await _changePasswordService.validateOtp(
// 1. Validate the OTP first.
await _changePasswordService.validateOtp(
otp: _otpController.text,
mobileNumber: widget.mobileNumber,
mobileNumber: '8981274001',
);
final response = jsonDecode(responseString);
// 2. Check status and set message
if (response['statusCode'] == 200 || response['statusCode'] == 2000) {
message = 'TPIN changed successfully!';
success = true;
} else {
message = response['message'] ?? 'Invalid OTP. Please try again.';
// 2. If OTP is valid, then call validateChangeTpin.
await _changePasswordService.validateChangeTpin(
oldTpin: widget.oldTpin,
newTpin: widget.newTpin,
);
// 3. Show success message.
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('TPIN changed successfully!'),
backgroundColor: Colors.green,
),
);
// 4. Navigate back to the profile screen or home.
Navigator.of(context).popUntil((route) => route.isFirst);
}
} catch (e) {
message = 'Failed to verify OTP: $e';
} finally {
// 3. Show feedback
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
backgroundColor: success ? Colors.green : Colors.red,
content: Text('An error occurred: $e'),
backgroundColor: Colors.red,
),
);
// 4. Navigate to dashboard after 5 seconds
Timer(const Duration(seconds: 5), () {
if (mounted) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => const DashboardScreen()),
(Route<dynamic> route) => false,
);
}
}
} finally {
if (mounted) {
setState(() {
_isLoading = false;
});
}
}