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,13 +11,43 @@ 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(
onWillPop: () async {
_timer?.cancel();
return true;
},
child: AuthenticationLayout(
appBarTitle: 'تغییر رمز عبور', appBarTitle: 'تغییر رمز عبور',
children: [ children: [
DidvanText( DidvanText(
@ -45,9 +78,11 @@ class Verification extends StatelessWidget {
child: PinCodeTextField( child: PinCodeTextField(
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
animationType: AnimationType.scale, animationType: AnimationType.scale,
cursorColor: Theme.of(context).colorScheme.text,
pinTheme: PinTheme( pinTheme: PinTheme(
fieldHeight: 48, fieldHeight: 48,
fieldWidth: 48, fieldWidth: 48,
selectedColor: Theme.of(context).colorScheme.primary,
inactiveColor: Theme.of(context).colorScheme.border, inactiveColor: Theme.of(context).colorScheme.border,
activeFillColor: Theme.of(context).colorScheme.primary, activeFillColor: Theme.of(context).colorScheme.primary,
activeColor: Theme.of(context).colorScheme.primary, activeColor: Theme.of(context).colorScheme.primary,
@ -57,20 +92,47 @@ class Verification extends StatelessWidget {
), ),
appContext: context, appContext: context,
length: 6, length: 6,
onChanged: (value) => state.verificationCode = value, onCompleted: (value) async {
final result = await state.verifyOtpToken(value);
if (result) {
_timer?.cancel();
}
},
onChanged: (value) {},
), ),
), ),
const Spacer(), const Spacer(),
DidvanButton( DidvanButton(
enabled: _isResendButtonEnabled,
onPressed: () { onPressed: () {
state.currentPageIndex++; _handleTimer();
state.sendOtpToken();
}, },
title: 'ارسال مجدد کد', title: !_isResendButtonEnabled
? '$_secondsRemaining ثانیه دیگر'
: 'ارسال مجدد کد',
), ),
const SizedBox( const SizedBox(
height: 48, 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(() {});
}
} }