forked from eatvio/eatvio_app
284 lines
12 KiB
Dart
284 lines
12 KiB
Dart
import 'dart:async';
|
|
import 'package:eatvio_app/core/app_router.dart';
|
|
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/websockets/ws_manager.dart';
|
|
import 'package:eatvio_app/core/widgets/custom_text_form_field.dart';
|
|
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_cubit.dart';
|
|
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_state.dart';
|
|
import 'package:eatvio_app/features/bottom_navigation_bar/presentation/bloc/profile_avatar_cubit/profile_avatar_cubit.dart';
|
|
import 'package:eatvio_app/features/chat/presentation/bloc/chats_cubit/chats_cubit.dart';
|
|
import 'package:eatvio_app/features/dev_tools/presentation/dev_tools_screen.dart';
|
|
import 'package:eatvio_app/features/notifications/presentation/notifications_cubit/notifications_cubit.dart';
|
|
import 'package:eatvio_app/generated/l10n.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/gestures.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
|
|
import 'package:flutter_native_splash/flutter_native_splash.dart';
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
class AuthScreen extends StatefulWidget {
|
|
const AuthScreen({super.key});
|
|
|
|
@override
|
|
State<AuthScreen> createState() => _AuthScreenState();
|
|
}
|
|
|
|
class _AuthScreenState extends State<AuthScreen> {
|
|
final emailController = TextEditingController();
|
|
final passwordController = TextEditingController();
|
|
bool checkboxState = false;
|
|
bool hidePassword = true;
|
|
final authCubit = sl<AuthCubit>();
|
|
late StreamSubscription<bool> keyboardSubscription;
|
|
bool isFocused = false;
|
|
int tapsCount = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
var keyboardVisibilityController = KeyboardVisibilityController();
|
|
keyboardSubscription = keyboardVisibilityController.onChange.listen((bool visible) {
|
|
setState(() {
|
|
isFocused = visible;
|
|
});
|
|
});
|
|
}
|
|
|
|
Future<void> _reconnectWebSocket(BuildContext context) async {
|
|
try {
|
|
await sl<WsManager>().clear();
|
|
await sl<WsManager>().initialize();
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
if (context.mounted) {
|
|
try {
|
|
await sl<ProfileAvatarCubit>().getMyProfile();
|
|
await sl<ChatsCubit>().getChatRoomsWithLoading();
|
|
await sl<ChatsCubit>().connectToWebSocket();
|
|
await sl<NotificationsCubit>().getNotifications();
|
|
await sl<NotificationsCubit>().connectToWebSocket();
|
|
showSuccessToast('Успешный вход');
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
AppRouter.navigatorKey.currentContext?.go(
|
|
'/feed-screen',
|
|
extra: {'needToInitializeWebSocket': false},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final s = S.of(context);
|
|
FlutterNativeSplash.remove();
|
|
return BlocProvider<AuthCubit>(
|
|
create: (BuildContext context) => authCubit,
|
|
child: BlocBuilder<AuthCubit, AuthState>(
|
|
builder: (BuildContext context, AuthState state) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
centerTitle: true,
|
|
toolbarHeight: isFocused ? 64 : MediaQuery.of(context).size.width - 64,
|
|
title: GestureDetector(
|
|
onTap: () {
|
|
tapsCount++;
|
|
if (tapsCount == 7) {
|
|
tapsCount = 0;
|
|
GoRouter.of(context).pushNamed(DevToolsScreen.name);
|
|
}
|
|
},
|
|
child: SvgPicture.asset(
|
|
ImageConstant.imgLogoEatvio,
|
|
height: isFocused ? 64 : MediaQuery.of(context).size.width - 64,
|
|
fit: BoxFit.contain,
|
|
),
|
|
),
|
|
),
|
|
body: SafeArea(
|
|
child: Padding(
|
|
padding: const 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),
|
|
Center(
|
|
child: GestureDetector(
|
|
onTap: () => GoRouter.of(context).goNamed('forgetPasswordScreen'),
|
|
child: Text(
|
|
'Забыли пароль?',
|
|
style: TextStyle(
|
|
color: appTheme.blueA400,
|
|
decorationColor: appTheme.blueA400,
|
|
decoration: TextDecoration.underline,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Text('Нет аккаунта? '),
|
|
GestureDetector(
|
|
onTap: () => GoRouter.of(context).goNamed('registrationScreen'),
|
|
child: Text(
|
|
'Зарегистрироваться',
|
|
style: TextStyle(
|
|
color: appTheme.blueA400,
|
|
decorationColor: appTheme.blueA400,
|
|
decoration: TextDecoration.underline,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const Spacer(),
|
|
Center(
|
|
child: RichText(
|
|
textAlign: TextAlign.center,
|
|
text: TextSpan(
|
|
text: 'Нажимая "Войти" или "Зарегистрироваться", Вы соглашаетесь \n',
|
|
style: const TextStyle(
|
|
fontSize: 12,
|
|
color: Colors.black,
|
|
),
|
|
children: [
|
|
const TextSpan(text: 'c '),
|
|
TextSpan(
|
|
text: ' Публичной офертой',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: appTheme.blueA400,
|
|
decorationColor: appTheme.blueA400,
|
|
decoration: TextDecoration.underline,
|
|
),
|
|
recognizer: TapGestureRecognizer()..onTap = () async => await launchUrl(Uri.parse('$baseUrl/oferta_bs.pdf')),
|
|
),
|
|
const TextSpan(text: ' и '),
|
|
TextSpan(
|
|
text: 'Политикой конфиденциальности\nи защитой персональных данных',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: appTheme.blueA400,
|
|
decorationColor: appTheme.blueA400,
|
|
decoration: TextDecoration.underline,
|
|
),
|
|
recognizer: TapGestureRecognizer()..onTap = () async => await launchUrl(Uri.parse('$baseUrl/docs/privacy.pdf')),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
Row(
|
|
children: [
|
|
SizedBox(
|
|
height: 48,
|
|
width: MediaQuery.of(context).size.width - 88,
|
|
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
|
|
: () async {
|
|
final isAuthenticated = await authCubit.getTokenByCredentials(
|
|
emailController.text,
|
|
passwordController.text,
|
|
);
|
|
if (isAuthenticated) {
|
|
if (context.mounted) {
|
|
_reconnectWebSocket(context);
|
|
}
|
|
}
|
|
},
|
|
label: state is AuthStateLoading
|
|
? const CupertinoActivityIndicator()
|
|
: Text(
|
|
'Войти',
|
|
style: theme.textTheme.titleSmall!.copyWith(color: Colors.white),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
SizedBox(
|
|
height: 48,
|
|
width: 48,
|
|
child: FloatingActionButton.extended(
|
|
heroTag: 'scanQrCodeButton',
|
|
highlightElevation: 0,
|
|
elevation: 0,
|
|
focusElevation: 0,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
backgroundColor: state is AuthStateLoading ? Colors.black.withAlpha(50) : appTheme.blueA400,
|
|
onPressed: state is AuthStateLoading
|
|
? null
|
|
: () {
|
|
GoRouter.of(context).pushNamed('scanQRCodeScreen');
|
|
},
|
|
label: state is AuthStateLoading
|
|
? const CupertinoActivityIndicator()
|
|
: const Icon(
|
|
Icons.qr_code_scanner_rounded,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|