feat : captcha widget.dart

This commit is contained in:
2025-05-14 15:01:03 +03:30
parent a132b21b18
commit 3a017b5956
10 changed files with 57 additions and 305 deletions

View File

@@ -1,5 +1,5 @@
enum ApiEnvironment {
dam(url: 'https://api.dam.rasadyar.net');
dam(url: 'https://api.dam.rasadyar.net/');
const ApiEnvironment({required this.url});

View File

@@ -8,9 +8,12 @@ class DioRemoteManager {
DioRemote? _currentClient;
ApiEnvironment? _currentEnv;
DioRemote setEnvironment(ApiEnvironment env) {
Future<DioRemote> setEnvironment([
ApiEnvironment env = ApiEnvironment.dam,
]) async {
if (_currentEnv != env) {
_currentClient = DioRemote(env.baseUrl);
await _currentClient?.init();
_currentEnv = env;
}
return _currentClient!;
@@ -30,10 +33,10 @@ class DioRemoteManager {
Future<void> switchAuthEnvironment(ApiEnvironment env) async {
final manager = diAuth.get<DioRemoteManager>();
final dioRemote = manager.setEnvironment(env);
final dioRemote = await manager.setEnvironment(env);
if (diAuth.isRegistered<AuthRepositoryImpl>()) {
await diAuth.unregister<AuthRepositoryImpl>();
await diAuth.unregister<AuthRepositoryImpl>();
}
diAuth.registerLazySingleton<AuthRepositoryImpl>(

View File

@@ -12,14 +12,10 @@ Future<void> setupAuthDI() async {
diAuth.registerLazySingleton(() => DioRemoteManager());
final manager = diAuth.get<DioRemoteManager>();
final dioRemote = manager.setEnvironment(ApiEnvironment.dam);
diAuth.registerLazySingleton<AuthRepositoryImpl>(
final dioRemote = await manager.setEnvironment(ApiEnvironment.dam);
diAuth.registerCachedFactory<AuthRepositoryImpl>(
() => AuthRepositoryImpl(dioRemote),
);
diAuth.registerLazySingleton(() => AuthService());
diAuth.registerLazySingleton(() => TokenStorageService());
//hive
//await diAuth.registerCachedFactoryAsync(() async=>await ,)
}

View File

@@ -34,17 +34,18 @@ class AuthRepositoryImpl implements AuthRepository {
@override
Future<CaptchaResponseModel?> captcha() async {
final response = await safeCall<DioResponse<CaptchaResponseModel>>(
final response = await safeCall(
call:
() async => await _httpClient.post<CaptchaResponseModel>(
'$_BASE_URL/login/',
'captcha/',
headers: {'Content-Type': 'application/json'},
fromJson: CaptchaResponseModel.fromJson
),
onSuccess: (response) {
iLog(response);
},
onError: (error, trace) {
throw Exception('Error during sign in: $error');
throw Exception('Error during captcha : $error');
},
);

View File

@@ -1,6 +1,8 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:rasadyar_auth/auth.dart';
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
import 'package:rasadyar_core/core.dart';
enum AuthType { useAndPass, otp }
@@ -18,8 +20,7 @@ class AuthLogic extends GetxController {
Rx<TextEditingController> phoneOtpNumberController =
TextEditingController().obs;
Rx<TextEditingController> otpCodeController = TextEditingController().obs;
CaptchaController captchaController = CaptchaController();
CaptchaController captchaOtpController = CaptchaController();
RxnString phoneNumber = RxnString(null);
RxnString password = RxnString(null);
@@ -32,6 +33,8 @@ class AuthLogic extends GetxController {
RxInt secondsRemaining = 120.obs;
Timer? _timer;
AuthRepositoryImpl authRepository = diAuth.get<AuthRepositoryImpl>();
void startTimer() {
_timer?.cancel();
secondsRemaining.value = 120;
@@ -55,6 +58,12 @@ class AuthLogic extends GetxController {
return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
}
@override
void onInit() {
super.onInit();
}
@override
void onReady() {
// TODO: implement onReady
@@ -66,4 +75,6 @@ class AuthLogic extends GetxController {
_timer?.cancel();
super.onClose();
}
}

View File

@@ -1,12 +1,12 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_auth/presentation/widget/captcha/view.dart';
import 'package:rasadyar_auth/presentation/widget/clear_button.dart';
import 'package:rasadyar_core/core.dart';
import 'logic.dart';
class AuthPage extends GetView<AuthLogic> {
const AuthPage({super.key});
@@ -21,7 +21,7 @@ class AuthPage extends GetView<AuthLogic> {
ObxValue((types) {
switch (types.value) {
case AuthType.otp:
return otpForm();
//return otpForm();
case AuthType.useAndPass:
return useAndPassFrom();
}
@@ -222,7 +222,7 @@ class AuthPage extends GetView<AuthLogic> {
}, controller.passwordController),
SizedBox(height: 26),
CaptchaWidget(controller: controller.captchaController),
CaptchaWidget(),
SizedBox(height: 23),
RElevated(
@@ -237,9 +237,6 @@ class AuthPage extends GetView<AuthLogic> {
initialEntryMode: PersianDatePickerEntryMode.calendarOnly,
initialDatePickerMode: PersianDatePickerMode.year,
);
if (data.value.currentState?.validate() == true &&
controller.captchaController.validate()) {}
},
width: Get.width,
height: 48,
@@ -251,7 +248,7 @@ class AuthPage extends GetView<AuthLogic> {
}, controller.formKey);
}
Widget otpForm() {
/* Widget otpForm() {
return ObxValue((status) {
switch (status.value) {
case OtpStatus.init:
@@ -262,7 +259,7 @@ class AuthPage extends GetView<AuthLogic> {
return confirmCodeForm();
}
}, controller.otpStatus);
}
}*/
Widget sendCodeForm() {
return ObxValue((data) {
@@ -335,14 +332,13 @@ class AuthPage extends GetView<AuthLogic> {
SizedBox(height: 26),
CaptchaWidget(controller: controller.captchaOtpController),
CaptchaWidget(),
SizedBox(height: 23),
RElevated(
text: 'ارسال رمز یکبار مصرف',
onPressed: () {
if (data.value.currentState?.validate() == true &&
controller.captchaOtpController.validate()) {
if (data.value.currentState?.validate() == true) {
controller.otpStatus.value = OtpStatus.sent;
controller.startTimer();
}
@@ -473,7 +469,6 @@ class AuthPage extends GetView<AuthLogic> {
TapGestureRecognizer()
..onTap = () {
controller.otpStatus.value = OtpStatus.init;
controller.captchaOtpController.clear();
},
text: ' ویرایش',
style: AppFonts.yekan14.copyWith(
@@ -489,8 +484,7 @@ class AuthPage extends GetView<AuthLogic> {
text: 'ورود',
onPressed: () {
if (controller.formKeyOtp.value.currentState?.validate() ==
true &&
controller.captchaOtpController.validate()) {}
true) {}
},
width: Get.width,
height: 48,
@@ -514,11 +508,4 @@ class AuthPage extends GetView<AuthLogic> {
],
);
}
Widget clearButton(VoidCallback onTap) {
return GestureDetector(
onTap: onTap,
child: Icon(CupertinoIcons.multiply_circle, size: 24),
);
}
}

View File

@@ -0,0 +1,8 @@
import 'package:flutter/cupertino.dart';
Widget clearButton(VoidCallback onTap) {
return GestureDetector(
onTap: onTap,
child: Icon(CupertinoIcons.multiply_circle, size: 24),
);
}