feature(registration): refactor i 2.

This commit is contained in:
mso7nueh
2024-07-14 22:16:59 +03:00
parent da447d6960
commit d49ce2120a
9 changed files with 430 additions and 357 deletions

View File

@@ -175,7 +175,7 @@ class GlossaryScreen extends StatelessWidget {
),
CustomDropDown(
items: state.productGroups,
defaultValue: state.productGroups?[0],
value: state.productGroups?[0],
fillColor: Colors.grey[200]!,
hintText: 'Выберите категорию',
borderDecoration: OutlineInputBorder(

View File

@@ -1,82 +1,49 @@
import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart';
import 'package:flutter/cupertino.dart';
import 'package:go_router/go_router.dart';
import 'bloc/date_of_birth_bloc.dart';
import 'models/date_of_birth_model.dart';
import 'package:eatvio_app/core/app_export.dart';
import 'package:eatvio_app/data/models/userRegistrationModel/user_registration_model.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class DateOfBirthScreen extends StatefulWidget {
const DateOfBirthScreen({Key? key}) : super(key: key);
final UserRegistrationSheet sheet;
static Widget builder(BuildContext context, sheet) {
return BlocProvider<DateOfBirthBloc>(
create: (context) => DateOfBirthBloc(
DateOfBirthState(
dateOfBirthModelObj: DateOfBirthModel(
userRegistrationSheet: sheet,
),
),
)..add(DateOfBirthInitialEvent()),
child: DateOfBirthScreen(),
);
}
const DateOfBirthScreen({super.key, required this.sheet});
@override
State<DateOfBirthScreen> createState() => _DateOfBirthScreenState();
}
class _DateOfBirthScreenState extends State<DateOfBirthScreen> {
DateTime selectedDate = DateTime.now();
late DateTime selectedDate;
@override
void initState() {
super.initState();
if (widget.sheet.birthdate.isNotEmpty) {
selectedDate = DateFormat('yyyy-mm-dd').parse(widget.sheet.birthdate);
} else {
selectedDate = DateTime.now();
}
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
padding: EdgeInsets.only(bottom: 80),
child: BlocBuilder<DateOfBirthBloc, DateOfBirthState>(
builder: (context, state) {
return Container(
width: double.maxFinite,
padding: EdgeInsets.symmetric(horizontal: 16.h, vertical: 9.v),
child: Column(
children: [
Text("date_of_birth".tr, style: theme.textTheme.headlineSmall),
const SizedBox(height: 32),
_buildDateInput(context),
// BlocBuilder<DateOfBirthBloc, DateOfBirthState>(
// builder: (context, state) {
// return NextStepButton(
// text: "continue".tr.toUpperCase(),
// margin: EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
// buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
// onPressed: onTapContinue(state, context),
// );
// },
// ),
],
),
);
},
child: Container(
width: double.maxFinite,
padding: EdgeInsets.symmetric(horizontal: 16.h, vertical: 9.v),
child: Column(
children: [
Text("date_of_birth".tr, style: theme.textTheme.headlineSmall),
const SizedBox(height: 32),
_buildDateInput(context),
],
),
),
);
}
onTapContinue(DateOfBirthState state, BuildContext context) {
return () {
UserRegistrationSheet sheet = state.dateOfBirthModelObj!.userRegistrationSheet;
// sheet.birthdate = selectedDate.toString();
GoRouter.of(context).goNamed('cityOfLivingScreen', extra: sheet);
};
}
Function onTapBack(BuildContext context) {
return () {
onTapArrowLeft(context);
};
}
/// Section Widget
_buildDateInput(BuildContext context) {
return Container(
@@ -105,14 +72,16 @@ class _DateOfBirthScreenState extends State<DateOfBirthScreen> {
onTapCalendar(BuildContext context) {
return () {
// openDatePickerDialog(context);
_showDialog(
CupertinoDatePicker(
initialDateTime: DateTime.now(),
mode: CupertinoDatePickerMode.date,
onDateTimeChanged: (DateTime newTime) {
onDateTimeChanged: (DateTime newDateTime) {
setState(() {
selectedDate = newTime;
selectedDate = newDateTime;
print(selectedDate);
final registrationCubit = context.read<RegistrationCubit>();
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(birthdate: selectedDate.toString()));
});
},
),
@@ -123,50 +92,26 @@ class _DateOfBirthScreenState extends State<DateOfBirthScreen> {
void _showDialog(Widget child) {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) => Container(
height: 216,
padding: const EdgeInsets.only(top: 6.0),
// The Bottom margin is provided to align the popup above the system
// navigation bar.
margin: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
),
// Provide a background color for the popup.
color: CupertinoColors.systemBackground.resolveFrom(context),
// Use a SafeArea widget to avoid system overlaps.
child: SafeArea(
top: false,
child: child,
),
),
builder: (BuildContext context) =>
Container(
height: 216,
padding: const EdgeInsets.only(top: 6.0),
// The Bottom margin is provided to align the popup above the system
// navigation bar.
margin: EdgeInsets.only(
bottom: MediaQuery
.of(context)
.viewInsets
.bottom,
),
// Provide a background color for the popup.
color: CupertinoColors.systemBackground.resolveFrom(context),
// Use a SafeArea widget to avoid system overlaps.
child: SafeArea(
top: false,
child: child,
),
),
);
}
/// Navigates to the previous screen.
onTapArrowLeft(BuildContext context) {
GoRouter.of(context).pop();
}
/// Displays a date picker dialog to update the selected date
///
/// This function returns a `Future` that completes with `void`.
Future<void> openDatePickerDialog(BuildContext context) async {
DateTime? dateTime = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1924),
lastDate: DateTime(
DateTime.now().year,
DateTime.now().month,
DateTime.now().day,
),
);
if (dateTime != null) {
if (mounted) {
setState(() {
selectedDate = dateTime;
});
}
}
}
}

View File

@@ -1,111 +1,79 @@
import 'package:go_router/go_router.dart';
import 'bloc/gender_bloc.dart';
import 'models/gender_model.dart';
import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart';
import 'package:eatvio_app/data/models/userRegistrationModel/user_registration_model.dart';
import 'package:eatvio_app/core/app_export.dart';
import 'package:flutter/material.dart';
class GenderScreen extends StatefulWidget {
const GenderScreen({Key? key}) : super(key: key);
final UserRegistrationSheet sheet;
static Widget builder(BuildContext context, sheet) {
return BlocProvider<GenderBloc>(
create: (context) => GenderBloc(
GenderState(
genderModelObj: GenderModel(userRegistrationSheet: sheet),
),
)..add(GenderInitialEvent()),
child: GenderScreen(),
);
}
const GenderScreen({super.key, required this.sheet});
@override
State<GenderScreen> createState() => _GenderScreenState();
}
class _GenderScreenState extends State<GenderScreen> {
late String currentValue;
@override
void initState() {
super.initState();
currentValue = widget.sheet.gender;
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
padding: EdgeInsets.only(bottom: 80),
child: BlocBuilder<GenderBloc, GenderState>(
builder: (context, state) {
return Container(
width: double.maxFinite,
padding: EdgeInsets.symmetric(horizontal: 16.h, vertical: 9.v),
child: Column(
children: [
Text("your_gender".tr, style: theme.textTheme.headlineSmall),
RadioListTile(
title: Text('Мужской'),
value: 'male',
groupValue: state.selectedRadioValue,
onChanged: onMale(context),
secondary: Image.asset(
'assets/images/img_male.png',
width: 40,
height: 40,
),
),
RadioListTile(
title: Text('Женский'),
value: 'female',
groupValue: state.selectedRadioValue,
onChanged: onFemale(context),
secondary: Image.asset(
'assets/images/img_female.png',
width: 40,
height: 40,
),
),
// BlocBuilder<GenderBloc, GenderState>(
// builder: (context, state) {
// return NextStepButton(
// text: "continue".tr.toUpperCase(),
// margin: EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
// buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
// onPressed: onTapNext(state, context),
// );
// },
// ),
],
child: Container(
width: double.maxFinite,
padding: EdgeInsets.symmetric(horizontal: 16.h, vertical: 9.v),
child: Column(
children: [
Text("your_gender".tr, style: theme.textTheme.headlineSmall),
RadioListTile(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
title: Text('Мужской'),
value: 'male',
groupValue: currentValue,
onChanged: (newValue) {
final registrationCubit = context.read<RegistrationCubit>();
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(gender: 'male'));
setState(() {
currentValue = 'male';
});
},
secondary: Image.asset(
'assets/images/img_male.png',
width: 40,
height: 40,
),
),
);
},
RadioListTile(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
title: Text('Женский'),
value: 'female',
groupValue: currentValue,
onChanged: (newValue) {
final registrationCubit = context.read<RegistrationCubit>();
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(gender: 'female'));
setState(() {
currentValue = 'female';
});
},
secondary: Image.asset(
'assets/images/img_female.png',
width: 40,
height: 40,
),
),
],
),
),
);
}
onTapNext(GenderState state, BuildContext context) {
return () {
UserRegistrationSheet sheet = state.genderModelObj!.userRegistrationSheet;
// sheet.gender = state.selectedRadioValue!;
GoRouter.of(context).goNamed('dateOfBirthScreen', extra: sheet);
};
}
onMale(BuildContext context) {
return (value) {
BlocProvider.of<GenderBloc>(context).add(GenderSelectEvent(value: 'male'));
};
}
onFemale(BuildContext context) {
return (value) {
BlocProvider.of<GenderBloc>(context).add(GenderSelectEvent(value: 'female'));
};
}
Function onTapBack() {
return () {
GoRouter.of(context).pop();
};
}
/// Navigates to the previous screen.
onTapArrowLeft(BuildContext context) {
GoRouter.of(context).pop();
}
}

View File

@@ -18,12 +18,14 @@ class _LanguageSelectScreenState extends State<LanguageSelectScreen> {
SelectionPopupModel(id: 1, title: "Русский", isSelected: true),
];
late SelectionPopupModel value;
SelectionPopupModel? value;
@override
void initState() {
super.initState();
value = SelectionPopupModel(title: widget.sheet.lang);
if (widget.sheet.lang.isNotEmpty) {
value = dropdownItemList.first;
}
}
@override
@@ -45,23 +47,13 @@ class _LanguageSelectScreenState extends State<LanguageSelectScreen> {
),
const SizedBox(height: 32),
CustomDropDown(
icon: Container(
margin: EdgeInsets.fromLTRB(30.h, 11.v, 17.h, 11.v),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(1.h),
),
child: CustomImageView(
imagePath: ImageConstant.imgFavorite,
height: 12.adaptSize,
width: 12.adaptSize,
),
),
value: value,
hintText: "Не выбрано".tr,
items: dropdownItemList,
onChanged: (SelectionPopupModel newValue) => setState(() {
value = newValue;
final registrationCubit = context.read<RegistrationCubit>();
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(lang: value.title));
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(lang: value!.title));
}),
),
],

View File

@@ -1,22 +1,27 @@
import 'package:go_router/go_router.dart';
import 'bloc/name_bloc.dart';
import 'models/name_model.dart';
import 'package:eatvio_app/data/models/userRegistrationModel/user_registration_model.dart';
import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart';
import 'package:eatvio_app/core/app_export.dart';
import 'package:eatvio_app/widgets/custom_text_form_field.dart';
import 'package:flutter/material.dart';
class NameScreen extends StatelessWidget {
const NameScreen({Key? key}) : super(key: key);
class NameScreen extends StatefulWidget {
final UserRegistrationSheet sheet;
static Widget builder(BuildContext context, sheet) {
return BlocProvider<NameBloc>(
create: (context) => NameBloc(
NameState(
nameModelObj: NameModel(userRegistrationSheet: sheet),
),
)..add(NameInitialEvent()),
child: NameScreen(),
);
const NameScreen({required this.sheet, super.key});
@override
State<NameScreen> createState() => _NameScreenState();
}
class _NameScreenState extends State<NameScreen> {
final firstNameController = TextEditingController();
final lastNameController = TextEditingController();
@override
void initState() {
super.initState();
firstNameController.text = widget.sheet.firstName;
lastNameController.text = widget.sheet.lastName;
}
@override
@@ -32,101 +37,27 @@ class NameScreen extends StatelessWidget {
SizedBox(height: 16.v),
Text("first_name".tr, style: theme.textTheme.bodyLarge),
SizedBox(height: 3.v),
BlocSelector<NameBloc, NameState, TextEditingController?>(
selector: (state) => state.firstNameController,
builder: (context, firstNameController) {
return CustomTextFormField(controller: firstNameController, textInputAction: TextInputAction.done);
CustomTextFormField(
controller: firstNameController,
textInputAction: TextInputAction.done,
onChanged: (newValue) {
final registrationCubit = context.read<RegistrationCubit>();
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(firstName: newValue));
},
),
SizedBox(height: 19.v),
Text("last_name".tr, style: theme.textTheme.bodyLarge),
SizedBox(height: 5.v),
BlocSelector<NameBloc, NameState, TextEditingController?>(
selector: (state) => state.lastNameController,
builder: (context, lastNameController) {
return CustomTextFormField(
controller: lastNameController,
);
CustomTextFormField(
controller: lastNameController,
onChanged: (newValue) {
final registrationCubit = context.read<RegistrationCubit>();
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(lastName: newValue));
},
),
// BlocBuilder<NameBloc, NameState>(
// builder: (context, state) {
// return NextStepButton(
// text: "continue".tr.toUpperCase(),
// margin: EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
// buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
// onPressed: () => _onTapNext(context, state), // Assign callback function here
// );
// },
// ),
)
],
),
),
);
}
/// Navigates to the previous screen.
onTapArrowLeft(BuildContext context) {
GoRouter.of(context).pop();
}
// _onTapNext(BuildContext context, NameState state) {
// if (state.firstNameController != null && state.lastNameController != null) {
// List<String> errors = validateFields(state.firstNameController!.text, state.lastNameController!.text);
//
// if (errors.isEmpty) {
// UserRegistrationSheet sheet = state.nameModelObj!.userRegistrationSheet;
// sheet.firstName = state.firstNameController!.text;
// sheet.lastName = state.lastNameController!.text;
// AppRouter.navigatorKey.currentContext?.goNamed('genderScreen', extra: sheet);
// } else {
// String errorMessage = '';
//
// for (var error in errors) {
// errorMessage += error + '\n\n';
// }
//
// showDialog(
// context: context,
// builder: (context) => AlertDialog(
// title: Text('Error'),
// content: Text(errorMessage),
// actions: [
// TextButton(
// onPressed: () => onTapOk(context),
// child: Text('OK'),
// ),
// ],
// ),
// );
// }
// }
// }
void onTapOk(BuildContext context) {
Navigator.pop(context);
}
List<String> validateFields(String firstName, String lastName) {
List<String> 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;
}
}