D1APP-112 Otp

This commit is contained in:
MohammadTaha Basiri 2022-03-09 16:42:01 +03:30
parent 63ec3d168e
commit d7eff24d29
3 changed files with 146 additions and 58 deletions

View File

@ -17,6 +17,7 @@ class RequestHelper {
static const String updateProfilePhoto = _baseUserUrl + '/profile/photo'; static const String updateProfilePhoto = _baseUserUrl + '/profile/photo';
static const String checkUsername = _baseUserUrl + '/CheckUsername'; static const String checkUsername = _baseUserUrl + '/CheckUsername';
static const String updateProfile = _baseUserUrl + '/profile/edit'; static const String updateProfile = _baseUserUrl + '/profile/edit';
static const String otp = _baseUserUrl + '/otp';
static String bookmarks({String? type}) => static String bookmarks({String? type}) =>
_baseUserUrl + '/marked/${type ?? ''}'; _baseUserUrl + '/marked/${type ?? ''}';

View File

@ -10,7 +10,6 @@ class AuthenticationState extends CoreProvier {
int _currentPageIndex = 0; int _currentPageIndex = 0;
String username = ''; String username = '';
String password = ''; String password = '';
String verificationCode = '';
set currentPageIndex(int value) { set currentPageIndex(int value) {
_currentPageIndex = value; _currentPageIndex = value;
@ -57,4 +56,30 @@ class AuthenticationState extends CoreProvier {
} }
return null; return null;
} }
Future<void> sendOtpToken() async {
final service = RequestService(RequestHelper.otp + '?username=$username');
await service.httpGet();
}
Future<bool> verifyOtpToken(String token) async {
appState = AppState.isolatedBusy;
final service = RequestService(RequestHelper.otp, body: {
'code': token,
'username': username,
});
await service.post();
appState = AppState.idle;
if (service.isSuccess) {
currentPageIndex++;
return true;
}
ActionSheetUtils.showAlert(AlertData(
message: service.isSuccess
? service.result['message']
: 'کد وارد شده صحیح نمی‌باشد',
));
return false;
}
} }

View File

@ -1,5 +1,8 @@
import 'dart:async';
import 'package:didvan/config/design_config.dart'; import 'package:didvan/config/design_config.dart';
import 'package:didvan/config/theme_data.dart'; import 'package:didvan/config/theme_data.dart';
import 'package:didvan/providers/user_provider.dart';
import 'package:didvan/views/authentication/authentication_state.dart'; import 'package:didvan/views/authentication/authentication_state.dart';
import 'package:didvan/views/authentication/widgets/authentication_layout.dart'; import 'package:didvan/views/authentication/widgets/authentication_layout.dart';
import 'package:didvan/views/widgets/didvan/button.dart'; import 'package:didvan/views/widgets/didvan/button.dart';
@ -8,69 +11,128 @@ import 'package:flutter/material.dart';
import 'package:pin_code_fields/pin_code_fields.dart'; import 'package:pin_code_fields/pin_code_fields.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
class Verification extends StatelessWidget { class Verification extends StatefulWidget {
const Verification({Key? key}) : super(key: key); const Verification({Key? key}) : super(key: key);
@override
State<Verification> createState() => _VerificationState();
}
class _VerificationState extends State<Verification> {
Timer? _timer;
int _secondsRemaining = 119;
bool _isResendButtonEnabled = false;
@override
void initState() {
final state = context.read<AuthenticationState>();
if (state.username == '') {
final user = context.read<UserProvider>().user;
state.username = user.phoneNumber;
}
Future.delayed(
Duration.zero,
state.sendOtpToken,
);
_handleTimer();
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final AuthenticationState state = context.read<AuthenticationState>(); final AuthenticationState state = context.read<AuthenticationState>();
return AuthenticationLayout( return WillPopScope(
appBarTitle: 'تغییر رمز عبور', onWillPop: () async {
children: [ _timer?.cancel();
DidvanText( return true;
'کد 6 رقمی ارسال شده به موبایل', },
style: Theme.of(context).textTheme.subtitle2, child: AuthenticationLayout(
fontWeight: FontWeight.normal, appBarTitle: 'تغییر رمز عبور',
), children: [
const SizedBox( DidvanText(
height: 8, 'کد 6 رقمی ارسال شده به موبایل',
), style: Theme.of(context).textTheme.subtitle2,
DidvanText( fontWeight: FontWeight.normal,
state.username,
style: Theme.of(context).textTheme.subtitle1,
),
const SizedBox(
height: 8,
),
DidvanText(
'را وارد کنید:',
style: Theme.of(context).textTheme.subtitle2,
fontWeight: FontWeight.normal,
),
const SizedBox(
height: 24,
),
Directionality(
textDirection: TextDirection.ltr,
child: PinCodeTextField(
keyboardType: TextInputType.number,
animationType: AnimationType.scale,
pinTheme: PinTheme(
fieldHeight: 48,
fieldWidth: 48,
inactiveColor: Theme.of(context).colorScheme.border,
activeFillColor: Theme.of(context).colorScheme.primary,
activeColor: Theme.of(context).colorScheme.primary,
borderRadius: DesignConfig.lowBorderRadius,
borderWidth: 1,
shape: PinCodeFieldShape.box,
),
appContext: context,
length: 6,
onChanged: (value) => state.verificationCode = value,
), ),
), const SizedBox(
const Spacer(), height: 8,
DidvanButton( ),
onPressed: () { DidvanText(
state.currentPageIndex++; state.username,
}, style: Theme.of(context).textTheme.subtitle1,
title: 'ارسال مجدد کد', ),
), const SizedBox(
const SizedBox( height: 8,
height: 48, ),
), DidvanText(
], 'را وارد کنید:',
style: Theme.of(context).textTheme.subtitle2,
fontWeight: FontWeight.normal,
),
const SizedBox(
height: 24,
),
Directionality(
textDirection: TextDirection.ltr,
child: PinCodeTextField(
keyboardType: TextInputType.number,
animationType: AnimationType.scale,
cursorColor: Theme.of(context).colorScheme.text,
pinTheme: PinTheme(
fieldHeight: 48,
fieldWidth: 48,
selectedColor: Theme.of(context).colorScheme.primary,
inactiveColor: Theme.of(context).colorScheme.border,
activeFillColor: Theme.of(context).colorScheme.primary,
activeColor: Theme.of(context).colorScheme.primary,
borderRadius: DesignConfig.lowBorderRadius,
borderWidth: 1,
shape: PinCodeFieldShape.box,
),
appContext: context,
length: 6,
onCompleted: (value) async {
final result = await state.verifyOtpToken(value);
if (result) {
_timer?.cancel();
}
},
onChanged: (value) {},
),
),
const Spacer(),
DidvanButton(
enabled: _isResendButtonEnabled,
onPressed: () {
_handleTimer();
state.sendOtpToken();
},
title: !_isResendButtonEnabled
? '$_secondsRemaining ثانیه دیگر'
: 'ارسال مجدد کد',
),
const SizedBox(
height: 48,
),
],
),
); );
} }
Future<void> _handleTimer() async {
_timer?.cancel();
_isResendButtonEnabled = false;
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
setState(() {
_secondsRemaining -= 1;
if (_secondsRemaining == 0) {
_isResendButtonEnabled = true;
_secondsRemaining = 129;
_timer?.cancel();
}
});
});
setState(() {});
}
} }