// ignore_for_file: deprecated_member_use import 'dart:async'; import 'package:flutter/gestures.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:url_launcher/url_launcher_string.dart'; import 'package:didvan/config/design_config.dart'; import 'package:didvan/config/theme_data.dart'; import 'package:didvan/providers/user.dart'; import 'package:didvan/utils/extension.dart'; import 'package:didvan/views/authentication/authentication_state.dart'; import 'package:didvan/views/authentication/widgets/authentication_layout.dart'; import 'package:didvan/views/widgets/didvan/button.dart'; import 'package:didvan/views/widgets/didvan/text.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:pin_code_fields/pin_code_fields.dart'; import 'package:provider/provider.dart'; import 'package:sms_autofill/sms_autofill.dart'; class Verification extends StatefulWidget { const Verification({Key? key}) : super(key: key); @override State createState() => _VerificationState(); } class _VerificationState extends State { Timer? _timer; int _secondsRemaining = 119; bool _isResendButtonEnabled = false; bool _isConfirmButtonEnabled = false; String _completedCode = ""; bool _hasError = false; @override void initState() { final state = context.read(); if (state.username == '') { final user = context.read().user; state.username = user.phoneNumber; } Future.delayed( Duration.zero, state.sendOtpToken, ); _handleTimer(); super.initState(); _listenForSms(); } Future _listenForSms() async { if (kDebugMode) { print("<<<<< در حال تلاش برای دریافت امضای برنامه >>>>>"); } final appSignature = await SmsAutoFill().getAppSignature; if (kDebugMode) { print("App Signature: $appSignature"); } await SmsAutoFill().listenForCode(); } @override Widget build(BuildContext context) { final AuthenticationState state = context.read(); final errorColor = Theme.of(context).colorScheme.error; final primaryColor = Theme.of(context).colorScheme.primary; final borderColor = Theme.of(context).colorScheme.border; final textColor = Theme.of(context).colorScheme.text; return WillPopScope( onWillPop: () async { _timer?.cancel(); if (state.currentPageIndex == 0) { return true; } if (state.currentPageIndex == 2 && !state.hasPassword) { state.currentPageIndex = 0; return false; } state.currentPageIndex--; return false; }, child: AuthenticationLayout( children: [ Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.baseline, textBaseline: TextBaseline.alphabetic, children: [ DidvanText('کد تایید شش رقمی به شماره ', style: Theme.of(context).textTheme.titleSmall, fontWeight: FontWeight.normal, color: Theme.of(context).colorScheme.caption), DidvanText( state.username.toString().convertToPersianDigits(), style: Theme.of(context).textTheme.titleMedium?.copyWith( fontWeight: FontWeight.normal, fontSize: 15, color: Theme.of(context).colorScheme.caption), ), DidvanText(' ارسال شد.', style: Theme.of(context).textTheme.titleSmall, fontWeight: FontWeight.normal, color: Theme.of(context).colorScheme.caption), ], ), const SizedBox(height: 8), InkWell( onTap: () { _timer?.cancel(); state.currentPageIndex = 0; }, borderRadius: DesignConfig.lowBorderRadius, child: Padding( padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0), child: Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ SvgPicture.asset('lib/assets/icons/pen.svg',height: 20,), const SizedBox(width: 8), DidvanText( 'شماره همراه', style: Theme.of(context).textTheme.titleSmall, fontWeight: FontWeight.normal, color: primaryColor, ), ], ), ), ), const SizedBox( height: 24, ), Directionality( textDirection: TextDirection.ltr, child: PinCodeTextField( keyboardType: TextInputType.number, animationType: AnimationType.scale, cursorColor: textColor, textStyle: TextStyle( fontSize: 20, color: _hasError ? const Color.fromARGB(255, 178, 4, 54) : textColor, ), pinTheme: PinTheme( fieldHeight: 58, fieldWidth: 58, selectedColor: _hasError ? const Color.fromARGB(255, 178, 4, 54) : primaryColor, inactiveColor: _hasError ? const Color.fromARGB(255, 178, 4, 54) : borderColor, activeFillColor: _hasError ? const Color.fromARGB(255, 178, 4, 54) : primaryColor, // بستگی به طراحی شما دارد activeColor: _hasError ? const Color.fromARGB(255, 178, 4, 54) : primaryColor, 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) { setState(() { _completedCode = value; _isConfirmButtonEnabled = value.length == 6; if (_hasError) { _hasError = false; } }); }, ), ), _isResendButtonEnabled ? InkWell( onTap: () { _handleTimer(); state.sendOtpToken(); }, borderRadius: DesignConfig.lowBorderRadius, child: Padding( padding: const EdgeInsets.symmetric( horizontal: 8.0, vertical: 4.0), child: DidvanText( 'دریافت مجدد کد', style: Theme.of(context).textTheme.titleSmall, fontWeight: FontWeight.normal, color: const Color.fromARGB(255, 0 ,126, 167), ), ), ) : Row( mainAxisAlignment: MainAxisAlignment.center, children: [ SvgPicture.asset('lib/assets/icons/clock.svg',color: const Color.fromARGB(255, 0 ,126, 167) ,), const SizedBox(width: 5,), DidvanText( '$_secondsRemaining ثانیه', style: Theme.of(context).textTheme.titleSmall?.copyWith( fontWeight: FontWeight.normal, color: const Color.fromARGB(255, 0 ,126, 167), ), ), DidvanText( ' تا دریافت مجدد کد', style: Theme.of(context).textTheme.titleSmall, fontWeight: FontWeight.normal, color: const Color.fromARGB(255, 0 ,126, 167), ), ], ), const SizedBox(height: 10,), DidvanButton( enabled: _isConfirmButtonEnabled, onPressed: () async { if (_completedCode.length == 6) { final result = await state.verifyOtpToken(_completedCode); if (!result) { if (mounted) setState(() => _hasError = true); } } }, title: 'تایید کد', ), const SizedBox( height: 24, ), Padding( padding: const EdgeInsets.symmetric(horizontal: 20), child: RichText( textAlign: TextAlign.center, text: TextSpan( style: Theme.of(context).textTheme.bodySmall, children: [ const TextSpan(text: 'با ورود به اپلیکیشن دیدوان،'), TextSpan( text: ' شرایط ', style: Theme.of(context) .textTheme .bodySmall! .copyWith(color: Theme.of(context).colorScheme.primary), recognizer: TapGestureRecognizer() ..onTap = () => launchUrlString( 'https://didvan.com/terms-of-use#conditions', mode: LaunchMode.inAppWebView), ), const TextSpan(text: 'و\n'), TextSpan( text: ' قوانین حریم خصوصی ', style: Theme.of(context) .textTheme .bodySmall! .copyWith(color: Theme.of(context).colorScheme.primary), recognizer: TapGestureRecognizer() ..onTap = () => launchUrlString( 'https://didvan.com/terms-of-use#privacy', mode: LaunchMode.inAppWebView), ), const TextSpan(text: 'را می‌پذیرم'), ], ), ), ), const SizedBox( height: 24, ), ], ), ); } Future _handleTimer() async { _timer?.cancel(); _isResendButtonEnabled = false; _timer = Timer.periodic(const Duration(seconds: 1), (timer) { try { setState(() { _secondsRemaining -= 1; if (_secondsRemaining == 0) { _isResendButtonEnabled = true; _secondsRemaining = 129; _timer?.cancel(); } }); } catch (e) { _timer?.cancel(); } }); setState(() {}); } }