Refactoring

This commit is contained in:
2024-03-05 16:42:23 +03:00
parent 2723861b04
commit ba10b5bdb8
11 changed files with 337 additions and 264 deletions

View File

@@ -64,94 +64,106 @@ class AuthScreen extends StatelessWidget {
margin:
EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () {
Navigator.pushNamed(
context, AppRoutes.languageSelectScreen);
}),
onPressed: onTapNoAccount(context)),
NextStepButton(
text: "Тест дневника".toUpperCase(),
margin:
EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () {
Navigator.pushNamed(context, AppRoutes.diaryEdit);
}),
onPressed: onTapDiaryTest(context)),
BlocBuilder<AuthBloc, AuthState>(builder: (context, state) {
return NextStepButton(
text: "authentificate".tr.toUpperCase(),
margin:
EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () async {
List<String> errors = validateFields(
state.emailController!.text,
state.passwordController!.text);
if (errors.isEmpty) {
final Map<String, dynamic> credentials = {
'email': state.emailController!.text,
'password': state.passwordController!.text,
};
final response = await http.post(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/login'),
headers: {'Content-Type': 'application/json'},
body: json.encode(credentials),
encoding: utf8);
if (response.statusCode == 200) {
var res = await jsonDecode(response.body);
final box = await Hive.openBox('token');
final token = res['token'];
box.put('token', token);
Navigator.pushNamed(
context,
AppRoutes.myProfile,
);
} else {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Ошибка'),
content: Text('wrong_credentials'.tr),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('OK'),
),
],
),
);
}
} 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'),
),
],
),
);
}
});
onPressed: onTapAuth(state, context));
}),
])));
}
onTapAuth(AuthState state, BuildContext context) {
return () async {
List<String> errors = validateFields(
state.emailController!.text, state.passwordController!.text);
if (errors.isEmpty) {
final Map<String, dynamic> credentials = {
'email': state.emailController!.text,
'password': state.passwordController!.text,
};
final response = await http.post(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/login'),
headers: {'Content-Type': 'application/json'},
body: json.encode(credentials),
encoding: utf8);
if (response.statusCode == 200) {
var res = await jsonDecode(response.body);
final box = await Hive.openBox('token');
final token = res['token'];
box.put('token', token);
Navigator.pushNamed(
context,
AppRoutes.myProfile,
);
} else {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Ошибка'),
content: Text('wrong_credentials'.tr),
actions: [
TextButton(
onPressed: onTapOk(context),
child: Text('OK'),
),
],
),
);
}
} 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: onTapOk(context),
child: Text('OK'),
),
],
),
);
}
};
}
onTapOk(BuildContext context) {
return () {
Navigator.pop(context);
};
}
onTapDiaryTest(BuildContext context) {
return () {
Navigator.pushNamed(context, AppRoutes.diaryEdit);
};
}
onTapNoAccount(BuildContext context) {
return () {
Navigator.pushNamed(context, AppRoutes.languageSelectScreen);
};
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
_buildAppBar(BuildContext context) {
return CustomAppBar(
leadingWidth: 29.h,
centerTitle: true,

View File

@@ -84,7 +84,7 @@ class MyProfileScreen extends StatelessWidget {
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context, String username) {
_buildAppBar(BuildContext context, String username) {
return CustomAppBar(
height: 54.v,
leadingWidth: 28.h,

View File

@@ -70,7 +70,7 @@ class MySettingsScreen extends StatelessWidget {
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context, String username) {
_buildAppBar(BuildContext context, String username) {
return CustomAppBar(
height: 54.v,
leadingWidth: 28.h,

View File

@@ -90,7 +90,7 @@ class AdditionalInformationScreen extends StatelessWidget {
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
_buildAppBar(BuildContext context) {
return CustomAppBar(
leadingWidth: 31.h,
leading: AppbarLeadingImage(

View File

@@ -98,7 +98,7 @@ class _AllergiesScreenState extends State<AllergiesScreen> {
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
_buildAppBar(BuildContext context) {
return CustomAppBar(
leadingWidth: 31.h,
leading: AppbarLeadingImage(

View File

@@ -43,24 +43,30 @@ class _BloodPressureScreenState extends State<BloodPressureScreen> {
padding: EdgeInsets.symmetric(horizontal: 12.h, vertical: 9.v),
child: Column(children: [
SizedBox(height: 7.v),
Padding(
padding: EdgeInsets.symmetric(horizontal: 4.h),
child: Container(
height: 16.v,
width: 343.h,
decoration: BoxDecoration(
color: theme.colorScheme.onPrimary,
borderRadius: BorderRadius.circular(8.h),
border: Border.all(
color: theme.colorScheme.primary,
width: 1.h)),
child: ClipRRect(
borderRadius: BorderRadius.circular(8.h),
child: LinearProgressIndicator(
value: 0.62,
backgroundColor: theme.colorScheme.onPrimary,
valueColor: AlwaysStoppedAnimation<Color>(
appTheme.blueA400))))),
Container(
height: 16.v,
width: 343.h,
padding: EdgeInsets.symmetric(
horizontal:
4.h), // Apply padding directly to the Container
decoration: BoxDecoration(
color: theme.colorScheme.onPrimary,
borderRadius: BorderRadius.circular(8.h),
border: Border.all(
color: theme.colorScheme.primary,
width: 1.h,
),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(8.h),
child: LinearProgressIndicator(
value: 0.62,
backgroundColor: theme.colorScheme.onPrimary,
valueColor:
AlwaysStoppedAnimation<Color>(appTheme.blueA400),
),
),
),
SizedBox(height: 21.v),
SizedBox(
width: 224.h,
@@ -234,7 +240,7 @@ class _BloodPressureScreenState extends State<BloodPressureScreen> {
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
_buildAppBar(BuildContext context) {
return CustomAppBar(
leadingWidth: 29.h,
leading: AppbarLeadingImage(

View File

@@ -105,68 +105,76 @@ class BodyCircumferencesScreen extends StatelessWidget {
margin: EdgeInsets.only(
left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () {
List<String> errors = validateFields(
state.chestCircumferenceController!.text,
state.waistCircumferenceController!.text,
state.hipCircumferenceController!.text);
if (errors.isEmpty) {
UserRegistrationSheet sheet = state
.bodyCircumferencesModelObj!
.userRegistrationSheet;
sheet.chestCircumference =
state.chestCircumferenceController!.text;
sheet.waistCircumference =
state.waistCircumferenceController!.text;
sheet.hipCircumference =
state.hipCircumferenceController!.text;
Navigator.pushNamed(
context,
AppRoutes.weightScreen,
arguments: sheet,
);
} 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'),
),
],
),
);
}
});
onPressed: onTapContinue(state, context));
}),
]))));
}
onTapContinue(BodyCircumferencesState state, BuildContext context) {
return () {
List<String> errors = validateFields(
state.chestCircumferenceController!.text,
state.waistCircumferenceController!.text,
state.hipCircumferenceController!.text);
if (errors.isEmpty) {
UserRegistrationSheet sheet =
state.bodyCircumferencesModelObj!.userRegistrationSheet;
sheet.chestCircumference = state.chestCircumferenceController!.text;
sheet.waistCircumference = state.waistCircumferenceController!.text;
sheet.hipCircumference = state.hipCircumferenceController!.text;
Navigator.pushNamed(
context,
AppRoutes.weightScreen,
arguments: sheet,
);
} 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: onTapOk(context),
child: Text('OK'),
),
],
),
);
}
};
}
onTapOk(BuildContext context) {
return () {
Navigator.pop(context);
};
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
_buildAppBar(BuildContext context) {
return CustomAppBar(
leadingWidth: 29.h,
leading: AppbarLeadingImage(
imagePath: ImageConstant.imgArrowLeft,
margin: EdgeInsets.only(left: 16.h, top: 15.v, bottom: 15.v),
onTap: () {
onTapArrowLeft(context);
}),
onTap: onTapBack(context)),
centerTitle: true,
title: AppbarTitleImage(imagePath: ImageConstant.imgLogoEatvio));
}
Function onTapBack(BuildContext context) {
return () {
onTapArrowLeft(context);
};
}
/// Navigates to the previous screen.
onTapArrowLeft(BuildContext context) {
NavigatorService.goBack();

View File

@@ -71,11 +71,7 @@ class CityOfLivingScreen extends StatelessWidget {
width: 12.adaptSize)),
hintText: "Не выбрано".tr,
items: cityOfLivingModelObj?.dropdownItemList ?? [],
onChanged: (value) {
context
.read<CityOfLivingBloc>()
.add(ChangeDropDownEvent(value: value));
});
onChanged: onDropDownChange(context));
}),
SizedBox(height: 22.v),
Text("city".tr, style: theme.textTheme.bodyLarge),
@@ -97,11 +93,7 @@ class CityOfLivingScreen extends StatelessWidget {
hintText: "Не выбрано".tr,
items:
cityOfLivingModelObj?.dropdownItemList1 ?? [],
onChanged: (value) {
context
.read<CityOfLivingBloc>()
.add(ChangeDropDown1Event(value: value));
});
onChanged: onDropDownChangeCity(context));
}),
Spacer(flex: 65),
BlocBuilder<CityOfLivingBloc, CityOfLivingState>(
@@ -111,33 +103,53 @@ class CityOfLivingScreen extends StatelessWidget {
margin: EdgeInsets.only(
left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () {
UserRegistrationSheet sheet =
state.cityOfLivingModelObj!.userRegistrationSheet;
Navigator.pushNamed(
context,
AppRoutes.bodyCircumferencesScreen,
arguments: sheet,
);
});
onPressed: onTapContinue(state, context));
}),
]))));
}
onTapContinue(CityOfLivingState state, BuildContext context) {
return () {
UserRegistrationSheet sheet =
state.cityOfLivingModelObj!.userRegistrationSheet;
Navigator.pushNamed(
context,
AppRoutes.bodyCircumferencesScreen,
arguments: sheet,
);
};
}
onDropDownChangeCity(BuildContext context) {
return (value) {
context.read<CityOfLivingBloc>().add(ChangeDropDown1Event(value: value));
};
}
onDropDownChange(BuildContext context) {
return (value) {
context.read<CityOfLivingBloc>().add(ChangeDropDownEvent(value: value));
};
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
_buildAppBar(BuildContext context) {
return CustomAppBar(
leadingWidth: 29.h,
leading: AppbarLeadingImage(
imagePath: ImageConstant.imgArrowLeft,
margin: EdgeInsets.only(left: 16.h, top: 15.v, bottom: 15.v),
onTap: () {
onTapArrowLeft(context);
}),
onTap: onTapBack(context)),
centerTitle: true,
title: AppbarTitleImage(imagePath: ImageConstant.imgLogoEatvio));
}
Function onTapBack(BuildContext context) {
return () {
onTapArrowLeft(context);
};
}
/// Navigates to the previous screen.
onTapArrowLeft(BuildContext context) {
NavigatorService.goBack();

View File

@@ -107,55 +107,62 @@ class CredentialsScreen extends StatelessWidget {
margin:
EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () {
List<String> errors = validateFields(
state.emailController!.text,
state.nameController!.text,
state.passwordController!.text,
state.passwordConfirmationController!.text);
if (errors.isEmpty) {
UserRegistrationSheet sheet =
state.credentialsModelObj!.userRegistrationSheet;
sheet.email = state.emailController!.text;
sheet.name = state.nameController!.text;
sheet.password = state.passwordController!.text;
sheet.passwordConfirmation =
state.passwordConfirmationController!.text;
Navigator.pushNamed(
context,
AppRoutes.nameScreen,
arguments: sheet,
);
} 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'),
),
],
),
);
}
});
onPressed: onTapContinue(state, context));
}),
])));
}
onTapContinue(CredentialsState state, BuildContext context) {
return () {
List<String> errors = validateFields(
state.emailController!.text,
state.nameController!.text,
state.passwordController!.text,
state.passwordConfirmationController!.text);
if (errors.isEmpty) {
UserRegistrationSheet sheet =
state.credentialsModelObj!.userRegistrationSheet;
sheet.email = state.emailController!.text;
sheet.name = state.nameController!.text;
sheet.password = state.passwordController!.text;
sheet.passwordConfirmation = state.passwordConfirmationController!.text;
Navigator.pushNamed(
context,
AppRoutes.nameScreen,
arguments: sheet,
);
} 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: onTapOk(context),
child: Text('OK'),
),
],
),
);
}
};
}
onTapOk(BuildContext context) {
return () {
Navigator.pop(context);
};
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
_buildAppBar(BuildContext context) {
return CustomAppBar(
leadingWidth: 29.h,
centerTitle: true,

View File

@@ -69,37 +69,45 @@ class _DateOfBirthScreenState extends State<DateOfBirthScreen> {
margin: EdgeInsets.only(
left: 16.h, right: 16.h, bottom: 50.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () {
UserRegistrationSheet sheet = state
.dateOfBirthModelObj!.userRegistrationSheet;
sheet.birthdate = selectedDate.toString();
Navigator.pushNamed(
context,
AppRoutes.cityOfLivingScreen,
arguments: sheet,
);
});
onPressed: onTapContinue(state, context));
}),
]))));
});
}
onTapContinue(DateOfBirthState state, BuildContext context) {
return () {
UserRegistrationSheet sheet =
state.dateOfBirthModelObj!.userRegistrationSheet;
sheet.birthdate = selectedDate.toString();
Navigator.pushNamed(
context,
AppRoutes.cityOfLivingScreen,
arguments: sheet,
);
};
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
_buildAppBar(BuildContext context) {
return CustomAppBar(
leadingWidth: 29.h,
leading: AppbarLeadingImage(
imagePath: ImageConstant.imgArrowLeft,
margin: EdgeInsets.only(left: 16.h, top: 15.v, bottom: 15.v),
onTap: () {
onTapArrowLeft(context);
}),
onTap: onTapBack(context)),
centerTitle: true,
title: AppbarTitleImage(imagePath: ImageConstant.imgLogoEatvio));
}
Function onTapBack(BuildContext context) {
return () {
onTapArrowLeft(context);
};
}
/// Section Widget
Widget _buildDateInput(BuildContext context) {
_buildDateInput(BuildContext context) {
return Container(
width: 343.h,
padding: EdgeInsets.symmetric(horizontal: 4.h, vertical: 1.v),
@@ -119,9 +127,7 @@ class _DateOfBirthScreenState extends State<DateOfBirthScreen> {
Align(
alignment: Alignment.center,
child: GestureDetector(
onTap: () {
openDatePickerDialog(context);
},
onTap: onTapCalendar(context),
child: Text(
'${selectedDate.day}.${selectedDate.month}.${selectedDate.year}',
style: theme.textTheme.titleMedium))),
@@ -129,6 +135,12 @@ class _DateOfBirthScreenState extends State<DateOfBirthScreen> {
]));
}
onTapCalendar(BuildContext context) {
return () {
openDatePickerDialog(context);
};
}
/// Navigates to the previous screen.
onTapArrowLeft(BuildContext context) {
NavigatorService.goBack();
@@ -139,15 +151,21 @@ class _DateOfBirthScreenState extends State<DateOfBirthScreen> {
/// 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));
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1924),
lastDate: DateTime(
DateTime.now().year,
DateTime.now().month,
DateTime.now().day,
),
);
if (dateTime != null) {
setState(() {
selectedDate = dateTime;
});
if (mounted) {
setState(() {
selectedDate = dateTime;
});
}
}
}
}

View File

@@ -76,54 +76,64 @@ class _FavoriteFoodsScreenState extends State<FavoriteFoodsScreen> {
margin: EdgeInsets.only(
left: 16.h, right: 16.h, bottom: 40.v),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
onPressed: () {
UserRegistrationSheet sheet = state
.favoriteFoodsModelObj!.userRegistrationSheet;
sheet.favoriteFoods =
state.favoriteFoodsController!.text;
sheet.unfavoriteFoods =
state.unfavoriteFoodsController!.text;
Navigator.pushNamed(
context,
AppRoutes.additionalInformationScreen,
arguments: sheet,
);
});
onPressed: onTapContinue(state, context));
}),
BlocBuilder<FavoriteFoodsBloc, FavoriteFoodsState>(
builder: (context, state) {
return GestureDetector(
onTap: () {
UserRegistrationSheet sheet =
state.favoriteFoodsModelObj!.userRegistrationSheet;
Navigator.pushNamed(
context,
AppRoutes.additionalInformationScreen,
arguments: sheet,
);
},
onTap: onTapSkip(state, context),
child: Text("skip".tr, style: theme.textTheme.bodyLarge),
);
}),
]))));
}
onTapSkip(FavoriteFoodsState state, BuildContext context) {
return () {
UserRegistrationSheet sheet =
state.favoriteFoodsModelObj!.userRegistrationSheet;
Navigator.pushNamed(
context,
AppRoutes.additionalInformationScreen,
arguments: sheet,
);
};
}
onTapContinue(FavoriteFoodsState state, BuildContext context) {
return () {
UserRegistrationSheet sheet =
state.favoriteFoodsModelObj!.userRegistrationSheet;
sheet.favoriteFoods = state.favoriteFoodsController!.text;
sheet.unfavoriteFoods = state.unfavoriteFoodsController!.text;
Navigator.pushNamed(
context,
AppRoutes.additionalInformationScreen,
arguments: sheet,
);
};
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
_buildAppBar(BuildContext context) {
return CustomAppBar(
leadingWidth: 31.h,
leading: AppbarLeadingImage(
imagePath: ImageConstant.imgArrowLeft,
margin: EdgeInsets.only(left: 18.h, top: 15.v, bottom: 15.v),
onTap: () {
onTapArrowLeft(context);
}),
onTap: onTapBack(context)),
centerTitle: true,
title: AppbarTitleImage(imagePath: ImageConstant.imgLogoEatvio));
}
Function onTapBack(BuildContext context) {
return () {
onTapArrowLeft(context);
};
}
/// Section Widget
Widget _buildTextFormFieldFav(BuildContext context) {
_buildTextFormFieldFav(BuildContext context) {
return Column(children: [
Text("favorite_foods".tr, style: CustomTextStyles.titleMediumSegoeUI),
SizedBox(height: 2.v),
@@ -140,7 +150,7 @@ class _FavoriteFoodsScreenState extends State<FavoriteFoodsScreen> {
}
/// Section Widget
Widget _buildTextFormFieldUnfav(BuildContext context) {
_buildTextFormFieldUnfav(BuildContext context) {
return Column(children: [
Text("unfavorite_foods".tr, style: CustomTextStyles.titleMediumSegoeUI),
SizedBox(height: 2.v),