From 94697a5324874134ed3c936f5ef93cd3cd82b16e Mon Sep 17 00:00:00 2001 From: Rakshitha Salian <rakshitha.salian@niveus.com> Date: Thu, 30 May 2024 16:00:16 +0530 Subject: [PATCH] Refactoring code --- .../dialogues/success_dialogue.dart | 63 ++++++++++--------- lib/serverRequest/all_request.dart | 7 ++- .../controller/verify_otp_controller.dart | 29 ++++++--- .../verifyOTP/view/verify_otp_screen.dart | 12 ---- .../verifyOTP/widgets/pincode_text_field.dart | 11 +--- 5 files changed, 59 insertions(+), 63 deletions(-) diff --git a/lib/customWidget/dialogues/success_dialogue.dart b/lib/customWidget/dialogues/success_dialogue.dart index 1e3d146..5fd057f 100644 --- a/lib/customWidget/dialogues/success_dialogue.dart +++ b/lib/customWidget/dialogues/success_dialogue.dart @@ -10,40 +10,41 @@ Future<dynamic> verificationSuccess( required String title, required String subtitle}) { return Get.dialog( - barrierDismissible: false, barrierColor: AppColor.black.withOpacity(0.5), Center( child: SingleChildScrollView( - child: AlertDialog( - shape: const RoundedRectangleBorder( - borderRadius: BorderRadius.all(Radius.circular(12.0))), - backgroundColor: AppColor.white, - contentPadding: EdgeInsets.zero, - content: Padding( - padding: EdgeInsets.all(24.0.w), - child: Column( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - Image.asset( - assetName, - width: 64, - height: 64, - ), - AppText( - text: title, - fontSize: TextSize.textSubHeadingSize, - fontWeight: FontWeight.w500, - textColor: AppColor.slateGray, - ).paddingOnly(top: 16.0), - AppText( - text: subtitle, - fontSize: TextSize.textSmallSize, - fontWeight: FontWeight.w400, - textColor: AppColor.lightGrey, - maxLine: 2, - textAlign: TextAlign.center, - ).paddingOnly(top: 8.0), - ], + child: PopScope(canPop: false, + child: AlertDialog( + shape: const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(12.0))), + backgroundColor: AppColor.white, + contentPadding: EdgeInsets.zero, + content: Padding( + padding: EdgeInsets.all(24.0.w), + child: Column( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Image.asset( + assetName, + width: 64, + height: 64, + ), + AppText( + text: title, + fontSize: TextSize.textSubHeadingSize, + fontWeight: FontWeight.w500, + textColor: AppColor.slateGray, + ).paddingOnly(top: 16.0), + AppText( + text: subtitle, + fontSize: TextSize.textSmallSize, + fontWeight: FontWeight.w400, + textColor: AppColor.lightGrey, + maxLine: 2, + textAlign: TextAlign.center, + ).paddingOnly(top: 8.0), + ], + ), ), ), ), diff --git a/lib/serverRequest/all_request.dart b/lib/serverRequest/all_request.dart index e88eceb..437072a 100644 --- a/lib/serverRequest/all_request.dart +++ b/lib/serverRequest/all_request.dart @@ -115,8 +115,7 @@ class AllHttpRequest { try { ///Generate authorization token for send otp String url = '$baseUrlForSendOtp/api/sendsms/token?action=generate'; - // String username = 'Exidehtptrans1'; - // String password = '_<P)>lnL0{'; + String basicAuth = 'Basic ${base64.encode(utf8.encode('${AppConstants.username}:${AppConstants.password}'))}'; Map<String, String>? headers = { @@ -134,10 +133,12 @@ class AllHttpRequest { sendOtp(); } else { Fluttertoast.showToast(msg: apiError.tr); + log("Error generate auth--${response.statusCode}"); throw Error(); } } catch (e) { Fluttertoast.showToast(msg: apiError.tr); + log("Error generate auth--$e"); throw Error(); } } @@ -165,10 +166,12 @@ class AllHttpRequest { ///If success do nothing } else { Fluttertoast.showToast(msg: apiError.tr); + log("Error generate auth--${response.statusCode}"); throw Error(); } } catch (e) { Fluttertoast.showToast(msg: apiError.tr); + log("Error generate auth--$e"); throw Error(); } } diff --git a/lib/views/verifyOTP/controller/verify_otp_controller.dart b/lib/views/verifyOTP/controller/verify_otp_controller.dart index 04324bb..4e58037 100644 --- a/lib/views/verifyOTP/controller/verify_otp_controller.dart +++ b/lib/views/verifyOTP/controller/verify_otp_controller.dart @@ -11,9 +11,8 @@ import '../../../customWidget/dialogues/success_dialogue.dart'; class VerifyOtpController extends GetxController { final baseController = Get.find<BaseController>(); - RxInt counter = 20.obs; + RxInt counter = 90.obs; Timer? timer; - RxString enteredOtp = "".obs; StreamController<ErrorAnimationType> errorController = StreamController<ErrorAnimationType>(); TextEditingController otpTextController = TextEditingController(); @@ -26,10 +25,16 @@ class VerifyOtpController extends GetxController { super.onInit(); } + @override + void onClose() { + timer?.cancel(); + super.onClose(); + } + onClickVerifyHandler(BuildContext context) { verifyLoader(true); Future.delayed( - const Duration(seconds: 30), + const Duration(seconds: 2), () => { if (baseController.generatedOtp.value == otpTextController.value.text) @@ -42,10 +47,9 @@ class VerifyOtpController extends GetxController { } else { - otpTextController.clear(), - verifyLoader(false), + resetLoading(), Fluttertoast.showToast(msg: incorrectOTPError.tr) - } + }, }); update(); @@ -53,11 +57,12 @@ class VerifyOtpController extends GetxController { onClickResendOTP() { resetTimer(); - startTimer(); - otpTextController.clear(); + resetLoading(); + + ///During resend otp auth is fetched from state variable and is not regenerated + AllHttpRequest().sendOtp(); update(); - AllHttpRequest().generateAuth(); } startTimer() { @@ -76,4 +81,10 @@ class VerifyOtpController extends GetxController { ///Set counter to 90s counter = 90.obs; } + + resetLoading() { + ///Reset Loading and clear otp in pintextfield + otpTextController.clear(); + verifyLoader(false); + } } diff --git a/lib/views/verifyOTP/view/verify_otp_screen.dart b/lib/views/verifyOTP/view/verify_otp_screen.dart index 1b9d85c..7a0104f 100644 --- a/lib/views/verifyOTP/view/verify_otp_screen.dart +++ b/lib/views/verifyOTP/view/verify_otp_screen.dart @@ -1,22 +1,10 @@ -import 'package:exide_crr/constants/image_constants.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; -import 'package:get_storage/get_storage.dart'; -import 'package:google_fonts/google_fonts.dart'; -import 'package:pin_code_fields/pin_code_fields.dart'; -import '../../../appLocalization/language_key.dart'; import '../../../constants/app_constants.dart'; -import '../../../constants/font_constants.dart'; -import '../../../customWidget/app_text.dart'; -import '../../../customWidget/button_widget.dart'; import '../../../customWidget/otp_text_field/otp_text_field.dart'; -import '../../../customWidget/otp_text_field/otp_text_field_style.dart'; import '../../../res/app_color_text_size.dart'; -import '../../../serverRequest/all_request.dart'; -import '../../../utils/res/styles.dart'; import '../controller/verify_otp_controller.dart'; -import '../../../customWidget/dialogues/success_dialogue.dart'; import '../widgets/floating_button.dart'; import '../widgets/pincode_text_field.dart'; import '../widgets/resend_otp_text.dart'; diff --git a/lib/views/verifyOTP/widgets/pincode_text_field.dart b/lib/views/verifyOTP/widgets/pincode_text_field.dart index 7edec68..01c249c 100644 --- a/lib/views/verifyOTP/widgets/pincode_text_field.dart +++ b/lib/views/verifyOTP/widgets/pincode_text_field.dart @@ -1,15 +1,12 @@ import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; -import 'package:google_fonts/google_fonts.dart'; import 'package:pin_code_fields/pin_code_fields.dart'; - -import '../../../constants/font_constants.dart'; import '../../../res/app_color_text_size.dart'; import '../../../utils/res/styles.dart'; import '../controller/verify_otp_controller.dart'; -Widget pinCodeField(VerifyOtpController controller,BuildContext context) { +Widget pinCodeField(VerifyOtpController controller, BuildContext context) { return PinCodeTextField( keyboardType: TextInputType.number, length: 4, @@ -33,10 +30,6 @@ Widget pinCodeField(VerifyOtpController controller,BuildContext context) { animationDuration: const Duration(milliseconds: 300), errorAnimationController: controller.errorController, controller: controller.otpTextController, - // Pass it here - onChanged: (text) { - controller.enteredOtp(text); - }, appContext: context, - ).paddingOnly(top: 8.0.h, bottom: 16.0.h, right: 92.w); + ).paddingOnly(top: 8.0.h, bottom: 16, right: 92.w); } -- GitLab