From 8cca27cd76680fa56c1ca9dd959727810fea2119 Mon Sep 17 00:00:00 2001 From: BLoC Date: Mon, 10 Mar 2025 14:38:26 +0300 Subject: [PATCH] feat: auth success and error messaging --- lib/core/utils/toasts.dart | 9 +- .../auth/presentation/bloc/auth_cubit.dart | 3 + .../auth/presentation/pages/auth_screen.dart | 238 +++++++++--------- 3 files changed, 133 insertions(+), 117 deletions(-) diff --git a/lib/core/utils/toasts.dart b/lib/core/utils/toasts.dart index 7a0f3db9..3d4fda0d 100644 --- a/lib/core/utils/toasts.dart +++ b/lib/core/utils/toasts.dart @@ -1,4 +1,5 @@ import 'package:eatvio_app/core/theme/theme_helper.dart'; +import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; const int _toastDuration = 2; @@ -19,10 +20,10 @@ void showErrorToast(String message) { Fluttertoast.showToast( msg: message, toastLength: Toast.LENGTH_LONG, - gravity: ToastGravity.TOP, + gravity: ToastGravity.CENTER, timeInSecForIosWeb: _toastDuration, - backgroundColor: appTheme.whiteA700, - textColor: appTheme.gray, + backgroundColor: appTheme.red, + textColor: Colors.white, fontSize: 16.0, ); } @@ -31,7 +32,7 @@ void showSuccessToast(String message) { Fluttertoast.showToast( msg: message, toastLength: Toast.LENGTH_SHORT, - gravity: ToastGravity.TOP, + gravity: ToastGravity.CENTER, timeInSecForIosWeb: _toastDuration, backgroundColor: appTheme.greenA70001, textColor: appTheme.whiteA700, diff --git a/lib/features/auth/presentation/bloc/auth_cubit.dart b/lib/features/auth/presentation/bloc/auth_cubit.dart index 924247d1..da1c3fef 100644 --- a/lib/features/auth/presentation/bloc/auth_cubit.dart +++ b/lib/features/auth/presentation/bloc/auth_cubit.dart @@ -1,6 +1,7 @@ import 'package:eatvio_app/core/app_router.dart'; import 'package:eatvio_app/core/locator_service.dart'; import 'package:eatvio_app/core/token_storage.dart'; +import 'package:eatvio_app/core/utils/toasts.dart'; import 'package:eatvio_app/features/auth/presentation/bloc/auth_state.dart'; import 'package:eatvio_app/features/auth/repositories/auth_repository.dart'; import 'package:eatvio_app/features/bottom_navigation_bar/presentation/bloc/profile_avatar_cubit/profile_avatar_cubit.dart'; @@ -28,11 +29,13 @@ class AuthCubit extends Cubit { token: token, ), ); + showSuccessToast('Успешный вход'); await TokenStorage.saveTokenInStorage(token); await sl().getMyProfile(); AppRouter.navigatorKey.currentContext?.go('/my-profile-screen'); } catch (e) { print(e); + showErrorToast('Неверный адрес электронной почты или пароль'); emit(AuthState.error(errorMessage: '$e')); } } diff --git a/lib/features/auth/presentation/pages/auth_screen.dart b/lib/features/auth/presentation/pages/auth_screen.dart index 0e1bce7a..bcaf2db6 100644 --- a/lib/features/auth/presentation/pages/auth_screen.dart +++ b/lib/features/auth/presentation/pages/auth_screen.dart @@ -2,9 +2,11 @@ import 'package:eatvio_app/core/const.dart'; import 'package:eatvio_app/core/locator_service.dart'; import 'package:eatvio_app/core/theme/theme_helper.dart'; import 'package:eatvio_app/core/utils/image_constant.dart'; +import 'package:eatvio_app/core/utils/toasts.dart'; import 'package:eatvio_app/core/widgets/custom_text_form_field.dart'; import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit.dart'; import 'package:eatvio_app/generated/l10n.dart'; +import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_native_splash/flutter_native_splash.dart'; @@ -12,6 +14,8 @@ import 'package:flutter_svg/flutter_svg.dart'; import 'package:go_router/go_router.dart'; import 'package:url_launcher/url_launcher.dart'; +import '../bloc/auth_state.dart'; + class AuthScreen extends StatefulWidget { const AuthScreen(); @@ -32,120 +36,128 @@ class _AuthScreenState extends State { FlutterNativeSplash.remove(); return BlocProvider( create: (BuildContext context) => authCubit, - child: Scaffold( - appBar: AppBar( - centerTitle: true, - title: SvgPicture.asset( - ImageConstant.imgLogoEatvio, - height: 64, - fit: BoxFit.contain, - ), - ), - body: SafeArea( - child: Padding( - padding: EdgeInsets.all(16), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text(s.email), - const SizedBox(height: 4), - CustomTextFormField( - controller: emailController, - textInputAction: TextInputAction.next, - keyboardType: TextInputType.emailAddress, - textInputType: TextInputType.emailAddress, - hintText: 'Введите email', - ), - const SizedBox(height: 16), - Text(s.password), - const SizedBox(height: 4), - CustomTextFormField( - obscureText: hidePassword, - controller: passwordController, - hintText: 'Введите пароль', - suffix: IconButton( - onPressed: () { - setState(() { - hidePassword = !hidePassword; - }); - }, - icon: Icon( - hidePassword ? Icons.visibility_outlined : Icons.visibility_off_outlined, - color: Colors.grey, - ), - ), - ), - const SizedBox(height: 16), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text('Нет аккаунта? '), - GestureDetector( - onTap: () => GoRouter.of(context).goNamed('webViewRegistrationScreen'), - child: Text( - 'Зарегистрироваться', - style: TextStyle( - color: appTheme.blueA400, - decorationColor: appTheme.blueA400, - decoration: TextDecoration.underline, - ), - ), - ), - ], - ), - Spacer(), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - 'Нажимая "Войти", Вы соглашаетесь с ', - style: TextStyle( - fontSize: 12, - ), - ), - GestureDetector( - onTap: () async { - await launchUrl(Uri.parse(baseUrl + '/oferta_bs.pdf')); - }, - child: Text( - 'Публичной офертой', - style: TextStyle( - fontSize: 12, - color: appTheme.blueA400, - decorationColor: appTheme.blueA400, - decoration: TextDecoration.underline, - ), - ), - ), - ], - ), - const SizedBox(height: 16), - Container( - height: 48, - width: MediaQuery.of(context).size.width - 32, - child: FloatingActionButton.extended( - heroTag: 'authButton', - highlightElevation: 0, - elevation: 0, - focusElevation: 0, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(20), - ), - backgroundColor: appTheme.blueA400, - onPressed: () => authCubit.getTokenByCredentials( - emailController.text, - passwordController.text, - ), - label: Text( - 'Войти', - style: theme.textTheme.titleSmall!.copyWith(color: Colors.white), - ), - ), - ), - ], + child: BlocBuilder( + builder: (BuildContext context, AuthState state) { + return Scaffold( + appBar: AppBar( + centerTitle: true, + title: SvgPicture.asset( + ImageConstant.imgLogoEatvio, + height: 64, + fit: BoxFit.contain, + ), ), - ), - ), + body: SafeArea( + child: Padding( + padding: EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(s.email), + const SizedBox(height: 4), + CustomTextFormField( + controller: emailController, + textInputAction: TextInputAction.next, + keyboardType: TextInputType.emailAddress, + textInputType: TextInputType.emailAddress, + hintText: 'Введите email', + ), + const SizedBox(height: 16), + Text(s.password), + const SizedBox(height: 4), + CustomTextFormField( + obscureText: hidePassword, + controller: passwordController, + hintText: 'Введите пароль', + suffix: IconButton( + onPressed: () { + setState(() { + hidePassword = !hidePassword; + }); + }, + icon: Icon( + hidePassword ? Icons.visibility_outlined : Icons.visibility_off_outlined, + color: Colors.grey, + ), + ), + ), + const SizedBox(height: 16), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text('Нет аккаунта? '), + GestureDetector( + onTap: () => GoRouter.of(context).goNamed('webViewRegistrationScreen'), + child: Text( + 'Зарегистрироваться', + style: TextStyle( + color: appTheme.blueA400, + decorationColor: appTheme.blueA400, + decoration: TextDecoration.underline, + ), + ), + ), + ], + ), + Spacer(), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + 'Нажимая "Войти", Вы соглашаетесь с ', + style: TextStyle( + fontSize: 12, + ), + ), + GestureDetector( + onTap: () async { + await launchUrl(Uri.parse(baseUrl + '/oferta_bs.pdf')); + }, + child: Text( + 'Публичной офертой', + style: TextStyle( + fontSize: 12, + color: appTheme.blueA400, + decorationColor: appTheme.blueA400, + decoration: TextDecoration.underline, + ), + ), + ), + ], + ), + const SizedBox(height: 16), + Container( + height: 48, + width: MediaQuery.of(context).size.width - 32, + child: FloatingActionButton.extended( + heroTag: 'authButton', + highlightElevation: 0, + elevation: 0, + focusElevation: 0, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(20), + ), + backgroundColor: state is AuthStateLoading ? Colors.black.withAlpha(50) : appTheme.blueA400, + onPressed: state is AuthStateLoading + ? null + : () => authCubit.getTokenByCredentials( + emailController.text, + passwordController.text, + ), + label: state is AuthStateLoading + ? CupertinoActivityIndicator() + : Text( + 'Войти', + style: theme.textTheme.titleSmall!.copyWith(color: Colors.white), + ), + ), + ), + ], + ), + ), + ), + ); + }, ), ); }