import 'package:eatvio_app/core/app_export.dart'; import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart'; import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_state.dart'; import 'package:eatvio_app/presentation/registration/additional_information_screen/additional_information_screen.dart'; import 'package:eatvio_app/presentation/registration/allergies_screen/allergies_screen.dart'; import 'package:eatvio_app/presentation/registration/blood_pressure_screen/blood_pressure_screen.dart'; import 'package:eatvio_app/presentation/registration/body_circumferences_screen/body_circumferences_screen.dart'; import 'package:eatvio_app/presentation/registration/city_of_living_screen/city_of_living_screen.dart'; import 'package:eatvio_app/presentation/registration/credentials_screen/credentials_screen.dart'; import 'package:eatvio_app/presentation/registration/date_of_birth_screen/date_of_birth_screen.dart'; import 'package:eatvio_app/presentation/registration/favorite_foods_screen/favorite_foods_screen.dart'; import 'package:eatvio_app/presentation/registration/favorite_sports_screen/favorite_sports_screen.dart'; import 'package:eatvio_app/presentation/registration/gender_screen/gender_screen.dart'; import 'package:eatvio_app/presentation/registration/height_screen/height_screen.dart'; import 'package:eatvio_app/presentation/registration/language_select_screen/language_select_screen.dart'; import 'package:eatvio_app/presentation/registration/medicine_intake_screen/medicine_intake_screen.dart'; import 'package:eatvio_app/presentation/registration/name_screen/name_screen.dart'; import 'package:eatvio_app/presentation/registration/weight_screen/weight_screen.dart'; import 'package:eatvio_app/widgets/app_bar/appbar_title_image.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:validators/validators.dart'; class RegistrationScreen extends StatefulWidget { const RegistrationScreen({super.key}); @override State createState() => _RegistrationScreenState(); } class _RegistrationScreenState extends State { double progressValue = 1 / 15; int step = 1; final pageController = PageController(); _showPopDialog() { showDialog( context: context, builder: (context) => CupertinoAlertDialog( content: Text('Отменить регистрацию? Заполненные данные не будут сохранены.'), actions: [ CupertinoDialogAction( onPressed: () { Navigator.pop(context); GoRouter.of(context).pop(); }, child: Text('Да'), ), CupertinoDialogAction( onPressed: () { Navigator.pop(context); }, isDefaultAction: true, child: Text('Нет'), ) ], ), ); } @override Widget build(BuildContext context) { return BlocProvider( create: (BuildContext context) => RegistrationCubit(), child: BlocBuilder( builder: (BuildContext context, RegistrationState state) { return PopScope( canPop: false, onPopInvoked: (_) => _showPopDialog(), child: Scaffold( floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, floatingActionButton: Row( children: [ Container( height: 48, width: MediaQuery.of(context).size.width * 0.2, padding: EdgeInsets.only(left: 16, right: 4), child: FloatingActionButton.extended( heroTag: 'backButtonRegistration', highlightElevation: 0, elevation: 0, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), ), backgroundColor: Color(0xFFEAF2FD), onPressed: () { if (pageController.page == 0) _showPopDialog(); setState(() { pageController.previousPage( duration: const Duration(milliseconds: 350), curve: Curves.easeInOut, ); }); }, label: Icon( Icons.arrow_back_ios_new_rounded, color: appTheme.blueA400, ), ), ), Container( height: 48, width: MediaQuery.of(context).size.width * 0.8, padding: EdgeInsets.only(left: 4, right: 16), child: FloatingActionButton.extended( heroTag: 'nextButtonRegistration', highlightElevation: 0, elevation: 0, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), ), backgroundColor: appTheme.blueA400, onPressed: () { if (canGoNext(state)) { setState(() { pageController.nextPage( duration: const Duration(milliseconds: 350), curve: Curves.easeInOut, ); }); } }, label: Text( 'Продолжить', style: theme.textTheme.titleSmall, ), ), ), ], ), appBar: AppBar( automaticallyImplyLeading: true, centerTitle: true, title: AppbarTitleImage(imagePath: ImageConstant.imgLogoEatvio), actions: [ Padding( padding: const EdgeInsets.only(right: 16), child: Text('шаг $step из 15'), ), ], bottom: PreferredSize( preferredSize: Size(MediaQuery.of(context).size.width, 32), child: Padding( padding: const EdgeInsets.all(16), child: TweenAnimationBuilder( duration: const Duration(milliseconds: 350), curve: Curves.easeInOut, tween: Tween( begin: 0, end: progressValue, ), builder: (context, value, _) => LinearProgressIndicator( value: value, borderRadius: BorderRadius.circular(16), minHeight: 8, backgroundColor: Colors.grey.shade100, valueColor: AlwaysStoppedAnimation(appTheme.blueA400), ), ), ), ), ), body: PageView( physics: NeverScrollableScrollPhysics(), controller: pageController, onPageChanged: (index) { setState(() { step = index + 1; progressValue = step / 15; }); }, children: [ LanguageSelectScreen(sheet: state.sheet), CredentialsScreen(sheet: state.sheet), NameScreen(sheet: state.sheet), GenderScreen(sheet: state.sheet), DateOfBirthScreen(sheet: state.sheet), CityOfLivingScreen(sheet: state.sheet), // todo refactor BodyCircumferencesScreen(sheet: state.sheet), WeightScreen(sheet: state.sheet), HeightScreen(sheet: state.sheet), BloodPressureScreen(sheet: state.sheet), AllergiesScreen(sheet: state.sheet), MedicineIntakeScreen(sheet: state.sheet), FavoriteSportsScreen(sheet: state.sheet), FavoriteFoodsScreen(sheet: state.sheet), AdditionalInformationScreen(sheet: state.sheet), ], ), ), ); }, ), ); } bool canGoNext(state) { switch (step) { case 2: List errors = validateCredentialsFields(state.sheet.email, state.sheet.name, state.sheet.password, state.sheet.passwordConfirmation); if (errors.isEmpty) { return true; } else { String errorMessage = ''; for (var error in errors) { errorMessage += error + '\n\n'; } showDialog( context: context, builder: (context) => AlertDialog( title: Text('Ошибка'), content: Text(errorMessage), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text('OK'), ), ], ), ); return false; } case 3: List errors = validateNameFields(state.sheet.firstName, state.sheet.lastName); if (errors.isEmpty) { return true; } else { String errorMessage = ''; for (var error in errors) { errorMessage += error + '\n\n'; } showDialog( context: context, builder: (context) => AlertDialog( title: Text('Ошибка'), content: Text(errorMessage), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text('OK'), ), ], ), ); return false; } case 7: List errors = validateCircumferencesFields(state.sheet.chestCircumference, state.sheet.waistCircumference, state.sheet.hipCircumference); if (errors.isEmpty) { return true; } else { String errorMessage = ''; for (var error in errors) { errorMessage += error + '\n\n'; } showDialog( context: context, builder: (context) => AlertDialog( title: Text('Ошибка'), content: Text(errorMessage), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text('OK'), ), ], ), ); return false; } default: return true; } } List validateCircumferencesFields( String chestCircumference, String waistCircumference, String hipCircumference, ) { List errors = []; if (chestCircumference.isEmpty | waistCircumference.isEmpty | hipCircumference.isEmpty) { errors.add('circumference_empty'.tr); } if (![chestCircumference, waistCircumference, hipCircumference].every((circumference) => int.tryParse(circumference) != null)) { errors.add('circumference_is_not_a_number'.tr); } return errors; } List validateCredentialsFields(String email, String name, String password, String passwordConfirmation) { List errors = []; if (!isEmail(email)) { errors.add('email_invalid'.tr); } if (email.isEmpty) { errors.add('email_empty'.tr); } if (name.length < 6) { errors.add('name_short'.tr); } if (name.isEmpty) { errors.add('name_empty'.tr); } if (password.isEmpty) { errors.add('name_empty'.tr); } if (password.length < 6) { errors.add('password_short'.tr); } if (password != passwordConfirmation) { errors.add('password_confirmation_mismatch'.tr); } return errors; } List validateNameFields(String firstName, String lastName) { List errors = []; if (firstName.isEmpty) { errors.add('First name cannot be empty'); } if (firstName.length < 1) { errors.add('First name is too short'); } if (lastName.isEmpty) { errors.add('Last name cannot be empty'); } if (lastName.length < 1) { errors.add('Last name is too short'); } return errors; } }