forked from eatvio/eatvio_app
feat(email): verify screen
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
import 'package:eatvio_app/features/registration/presentation/bloc/verify_email_cubit/verify_email_state.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class VerifyEmailCubit extends Cubit<VerifyEmailState> {
|
||||
final ProfileRepository profileRepository;
|
||||
|
||||
VerifyEmailCubit({
|
||||
required this.profileRepository,
|
||||
}) : super(const VerifyEmailState.loading());
|
||||
|
||||
sendEmailCode() async {
|
||||
try {
|
||||
final profile = await profileRepository.getMyProfile();
|
||||
await profileRepository.sendEmailCode();
|
||||
emit(VerifyEmailState.loaded(email: profile.email));
|
||||
} catch (e) {
|
||||
emit(const VerifyEmailState.error(errorMessage: 'Ошибка отправки кода.\nПовторите попытку позже'));
|
||||
}
|
||||
}
|
||||
|
||||
tryVerify(String code) async {
|
||||
try {
|
||||
emit(const VerifyEmailState.verifyLoading());
|
||||
final result = await profileRepository.tryVerifyEmailByCode(code);
|
||||
if (result == true) {
|
||||
emit(const VerifyEmailState.verifySuccess());
|
||||
} else {
|
||||
emit(const VerifyEmailState.verifyError());
|
||||
}
|
||||
} catch (e) {
|
||||
emit(const VerifyEmailState.verifyError());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
part 'verify_email_state.freezed.dart';
|
||||
|
||||
@freezed
|
||||
abstract class VerifyEmailState with _$VerifyEmailState {
|
||||
const factory VerifyEmailState.loading() = VerifyEmailStateLoading;
|
||||
|
||||
const factory VerifyEmailState.loaded({
|
||||
required String email,
|
||||
}) = VerifyEmailStateLoaded;
|
||||
|
||||
const factory VerifyEmailState.error({
|
||||
required String errorMessage,
|
||||
}) = VerifyEmailStateError;
|
||||
|
||||
const factory VerifyEmailState() = _VerifyEmailState;
|
||||
|
||||
const factory VerifyEmailState.verifyLoading() = VerifyEmailStateVerifyLoading;
|
||||
|
||||
const factory VerifyEmailState.verifySuccess() = VerifyEmailStateVerifySuccess;
|
||||
|
||||
const factory VerifyEmailState.verifyError() = VerifyEmailStateVerifyError;
|
||||
}
|
||||
@@ -1,4 +1,10 @@
|
||||
import 'package:eatvio_app/core/locator_service.dart';
|
||||
import 'package:eatvio_app/core/utils/toasts.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';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class VerifyEmailScreen extends StatefulWidget {
|
||||
const VerifyEmailScreen({super.key});
|
||||
@@ -10,6 +16,7 @@ class VerifyEmailScreen extends StatefulWidget {
|
||||
class _VerifyEmailScreenState extends State<VerifyEmailScreen> {
|
||||
final List<FocusNode> _focusNodes = List.generate(4, (_) => FocusNode());
|
||||
final List<TextEditingController> _controllers = List.generate(4, (_) => TextEditingController());
|
||||
String email = '';
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
@@ -22,36 +29,66 @@ class _VerifyEmailScreenState extends State<VerifyEmailScreen> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _checkAndSubmitCode(BuildContext context) {
|
||||
final code = _controllers.map((c) => c.text).join();
|
||||
if (code.length == 4) {
|
||||
context.read<VerifyEmailCubit>().tryVerify(code);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Подтвердите почту")),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text("Код отправлен на почту\nnazvanie@mail.com", textAlign: TextAlign.center),
|
||||
const SizedBox(height: 20),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: List.generate(4, (index) => _buildCodeField(index)),
|
||||
return BlocProvider<VerifyEmailCubit>(
|
||||
create: (_) => sl<VerifyEmailCubit>()..sendEmailCode(),
|
||||
child: BlocConsumer<VerifyEmailCubit, VerifyEmailState>(
|
||||
listener: (context, state) {
|
||||
if (state is VerifyEmailStateLoaded) {
|
||||
setState(() {
|
||||
email = state.email;
|
||||
});
|
||||
} else if (state is VerifyEmailStateVerifySuccess) {
|
||||
showSuccessToast('Почта успешно подтверждена');
|
||||
GoRouter.of(context).pop();
|
||||
} else if (state is VerifyEmailStateVerifyError) {
|
||||
showErrorToast("Неверный код. Попробуйте снова.");
|
||||
for (var controller in _controllers) {
|
||||
controller.clear();
|
||||
}
|
||||
FocusScope.of(context).requestFocus(_focusNodes[0]);
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Подтвердите почту")),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
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();
|
||||
},
|
||||
child: const Text("Не приходит код?"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
const Text("Запросить код повторно", style: TextStyle(color: Colors.grey)),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
// логика запроса нового кода
|
||||
},
|
||||
child: const Text("Не приходит код?"),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCodeField(int index) {
|
||||
Widget _buildCodeField(BuildContext context, int index) {
|
||||
return SizedBox(
|
||||
width: 50,
|
||||
child: TextField(
|
||||
@@ -63,9 +100,7 @@ class _VerifyEmailScreenState extends State<VerifyEmailScreen> {
|
||||
style: const TextStyle(fontSize: 24),
|
||||
decoration: InputDecoration(
|
||||
counterText: "",
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
border: OutlineInputBorder(borderRadius: BorderRadius.circular(8.0)),
|
||||
),
|
||||
onChanged: (value) {
|
||||
if (value.isNotEmpty && index < 3) {
|
||||
@@ -74,6 +109,7 @@ class _VerifyEmailScreenState extends State<VerifyEmailScreen> {
|
||||
if (value.isEmpty && index > 0) {
|
||||
FocusScope.of(context).requestFocus(_focusNodes[index - 1]);
|
||||
}
|
||||
_checkAndSubmitCode(context);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@@ -69,7 +69,6 @@ class _VerifyScreenState extends State<VerifyScreen> {
|
||||
InkWell(
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
GoRouter.of(context).pop();
|
||||
},
|
||||
child: Ink(
|
||||
width: MediaQuery.of(context).size.width - 32,
|
||||
|
||||
Reference in New Issue
Block a user