Merge pull request 'Добавление таймера для подтверждения почты' (#643) from misha/eatvio_app:issue-642 into main

Reviewed-on: eatvio/eatvio_app#643
This commit is contained in:
mso7nueh
2025-08-20 09:39:16 +00:00

View File

@@ -1,5 +1,10 @@
import 'dart:async';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/custom_text_style.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/utils/toasts.dart';
import 'package:eatvio_app/core/widgets/custom_outlined_button.dart';
import 'package:eatvio_app/features/registration/presentation/bloc/verify_email_cubit/verify_email_cubit.dart';
import 'package:eatvio_app/features/registration/presentation/bloc/verify_email_cubit/verify_email_state.dart';
import 'package:flutter/material.dart';
@@ -18,11 +23,14 @@ class _VerifyEmailScreenState extends State<VerifyEmailScreen> {
final List<TextEditingController> _controllers = List.generate(4, (_) => TextEditingController());
String email = '';
late final GoRouter goRouter;
late Timer _timer;
int _secondsRemaining = 60;
bool _canSendAgain = false;
@override
void initState() {
super.initState();
goRouter = GoRouter.of(context);
goRouter = GoRouter.of(context);
_startTimer();
}
@override
@@ -36,6 +44,23 @@ class _VerifyEmailScreenState extends State<VerifyEmailScreen> {
super.dispose();
}
void _startTimer() {
setState(() {
_canSendAgain = false;
_secondsRemaining = 60;
});
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
setState(() {
if (_secondsRemaining > 0) {
_secondsRemaining--;
} else {
_canSendAgain = true;
_timer.cancel();
}
});
});
}
void _checkAndSubmitCode(BuildContext context) {
final code = _controllers.map((c) => c.text).join();
if (code.length == 4) {
@@ -74,18 +99,25 @@ class _VerifyEmailScreenState extends State<VerifyEmailScreen> {
children: [
Text("Код отправлен на почту\n$email", textAlign: TextAlign.center),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: List.generate(4, (index) => _buildCodeField(context, index)),
),
const SizedBox(height: 20),
const Text("Запросить код повторно", style: TextStyle(color: Colors.grey)),
TextButton(
onPressed: () {
context.read<VerifyEmailCubit>().sendEmailCode();
Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: List.generate(4, (index) => _buildCodeField(context, index))),
const SizedBox(height: 40),
CustomOutlinedButton(
text: 'Запросить код повторно',
height: 52,
isDisabled: !_canSendAgain,
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.whiteA700),
buttonStyle: ButtonStyle(
side: const WidgetStatePropertyAll(BorderSide.none),
backgroundColor: WidgetStatePropertyAll(_canSendAgain ? appTheme.blueA400 : appTheme.gray),
),
onPressed: () async {
await context.read<VerifyEmailCubit>().sendEmailCode();
_startTimer();
},
child: const Text("Не приходит код?"),
),
const SizedBox(height: 8),
if (!_canSendAgain)
Text("Зпросить код повторно можно через $_secondsRemaining с.", style: TextStyles.textSMedium.copyWith(color: appTheme.gray)),
],
),
),