fix(diary) Fix

This commit is contained in:
2024-02-09 19:54:59 +03:00
parent d3c00cc99f
commit 7b989076cc
7 changed files with 206 additions and 0 deletions

View File

@@ -1,62 +0,0 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import '/core/app_export.dart';
import 'package:eatvio_app/presentation/diary/diary_edit_screen/models/diary_edit_model.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:hive/hive.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
part 'diary_edit_event.dart';
part 'diary_edit_state.dart';
/// A bloc that manages the state of a DiaryEdit according to the event that is dispatched to it.
class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
DiaryEditBloc(DiaryEditState initialState) : super(initialState) {
on<DiaryEditInitialEvent>(_onInitialize);
}
Future<void> _onInitialize(
DiaryEditInitialEvent event,
Emitter<DiaryEditState> emit,
) async {
try {
final box = await Hive.openBox('token');
final token = box.get('token');
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/diary/index'),
headers: {
'Authorization': 'Bearer $token',
},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
// Проверка наличия ключевых полей в JSON
if (data is Map<String, dynamic> && data.containsKey('id')) {
// Все ключевые поля присутствуют, продолжаем обработку
if (state is DiaryEditLoadedState) {
// Если текущее состояние уже загружено, обновляем его
var currentState = state as DiaryEditLoadedState;
// Обновляем значения контроллеров только если они уже были созданы
emit((currentState).copyWith());
} else {
// Если текущее состояние - начальное, создаем новое загруженное состояние
emit(DiaryEditLoadedState());
}
} else {
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
emit(
DiaryEditErrorState('Некоторые необходимые данные отсутствуют.'));
}
} else {
// Статус ответа не равен 200, обрабатываем ошибку
emit(DiaryEditErrorState('Ошибка сервера: ${response.statusCode}'));
}
} catch (e) {
// Ошибка при декодировании JSON или другая ошибка выполнения
emit(DiaryEditErrorState('Ошибка при получении данных профиля: $e'));
}
}
}

View File

@@ -1,16 +0,0 @@
// ignore_for_file: must_be_immutable
part of 'diary_edit_bloc.dart';
/// Abstract class for all events that can be dispatched from the
///DiaryEdit widget.
///
/// Events must be immutable and implement the [Equatable] interface.
@immutable
abstract class DiaryEditEvent extends Equatable {}
/// Event that is dispatched when the DiaryEdit widget is first created.
class DiaryEditInitialEvent extends DiaryEditEvent {
@override
List<Object?> get props => [];
}

View File

@@ -1,45 +0,0 @@
// ignore_for_file: must_be_immutable
part of 'diary_edit_bloc.dart';
class DiaryEditState extends Equatable {
const DiaryEditState({this.diaryEditModelObj});
final DiaryEditModel? diaryEditModelObj;
@override
List<Object?> get props => [diaryEditModelObj];
}
class DiaryEditInitialState extends DiaryEditState {
DiaryEditInitialState({required DiaryEditModel diaryEditModelObj})
: super(diaryEditModelObj: diaryEditModelObj);
}
/// Represents the state of DiaryEdit in the application.
class DiaryEditLoadedState extends DiaryEditState {
DiaryEditLoadedState({this.diaryEditModelObj})
: super(diaryEditModelObj: null);
DiaryEditModel? diaryEditModelObj;
@override
List<Object?> get props => [
diaryEditModelObj,
];
DiaryEditLoadedState copyWith({
DiaryEditModel? diaryEditModelObj,
}) {
return DiaryEditLoadedState(
diaryEditModelObj: diaryEditModelObj ?? this.diaryEditModelObj,
);
}
}
class DiaryEditErrorState extends DiaryEditState {
final String errorMessage;
const DiaryEditErrorState(this.errorMessage);
@override
List<Object> get props => [errorMessage];
}

View File

@@ -1,817 +0,0 @@
import 'bloc/diary_edit_bloc.dart';
import 'models/diary_edit_model.dart';
import 'package:flutter/material.dart';
import 'package:eatvio_app/core/app_export.dart';
import 'package:eatvio_app/widgets/custom_text_form_field.dart';
import 'package:eatvio_app/widgets/custom_edit_drop_down.dart';
import 'package:eatvio_app/widgets/custom_outlined_button.dart';
import 'package:eatvio_app/widgets/custom_icon_button.dart';
// ignore: must_be_immutable
class DiaryEditScreen extends StatelessWidget {
DiaryEditScreen({Key? key})
: super(
key: key,
);
List<String> dropdownItemList = [
"Item One",
"Item Two",
"Item Three",
];
List<String> dropdownItemList1 = [
"Item One",
"Item Two",
"Item Three",
];
TextEditingController enterProductNameController = TextEditingController();
TextEditingController quantityController = TextEditingController();
List<String> dropdownItemList2 = [
"Item One",
"Item Two",
"Item Three",
];
List<String> dropdownItemList3 = [
"Item One",
"Item Two",
"Item Three",
];
static Widget builder(BuildContext context) {
return BlocProvider<DiaryEditBloc>(
create: (context) =>
DiaryEditBloc(DiaryEditState(diaryEditModelObj: DiaryEditModel()))
..add(DiaryEditInitialEvent()),
child: DiaryEditScreen());
}
Widget build(BuildContext context) {
return BlocBuilder<DiaryEditBloc, DiaryEditState>(
builder: (context, state) {
if (state is DiaryEditInitialState) {
// Это начальное состояние, данные ещё не загружены.
return CircularProgressIndicator(
color: Colors.white,
backgroundColor: Colors.white,
);
} else if (state is DiaryEditLoadedState) {
// Данные загружены, можно строить экран.
return _buildDiaryEditScreen(context, state);
} else if (state is DiaryEditErrorState) {
// Произошла ошибка при загрузке данных.
return Center(child: Text(state.errorMessage));
} else {
// Другие состояния, если они есть.
return Container(); // Пустой контейнер для необработанных состояний.
}
},
);
}
Widget _buildDiaryEditScreen(
BuildContext context, DiaryEditLoadedState state) {
mediaQueryData = MediaQuery.of(context);
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
body: SizedBox(
width: double.maxFinite,
child: Column(
children: [
Expanded(
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.only(bottom: 5.v),
child: Column(
children: [
_buildHeaderRow(context),
SizedBox(height: 30.v),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16.h),
child: CustomDropDown(
icon: Container(
margin:
EdgeInsets.fromLTRB(30.h, 24.v, 20.h, 22.v),
child: CustomImageView(
imagePath: ImageConstant.imgArrowLeft,
height: 10.v,
width: 18.h,
),
),
hintText: "Подъём \\ уход ко сну",
items: dropdownItemList,
onChanged: (value) {},
),
),
SizedBox(height: 30.v),
_buildImageStack(context),
SizedBox(height: 20.v),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16.h),
child: CustomDropDown(
icon: Container(
margin:
EdgeInsets.fromLTRB(30.h, 30.v, 20.h, 30.v),
child: CustomImageView(
imagePath: ImageConstant.imgArrowLeft,
height: 10.v,
width: 18.h,
),
),
hintText: "Ваше настроение после приема пищи",
items: dropdownItemList3,
onChanged: (value) {},
),
),
SizedBox(height: 30.v),
_buildHeaderColumn(context),
SizedBox(height: 30.v),
_buildHeaderStack(context),
],
),
),
),
),
],
),
),
bottomNavigationBar: _buildEndDay(context),
),
);
}
/// Section Widget
Widget _buildHeaderRow(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 16.h),
padding: EdgeInsets.symmetric(
horizontal: 24.h,
vertical: 12.v,
),
decoration: AppDecoration.outlinePrimary.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder12,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
CustomImageView(
imagePath: ImageConstant.imgArrowLeft,
height: 2.v,
width: 20.h,
margin: EdgeInsets.only(
top: 19.v,
bottom: 17.v,
),
),
Padding(
padding: EdgeInsets.only(top: 1.v),
child: Column(
children: [
Align(
alignment: Alignment.centerRight,
child: Text(
"10 день",
style: CustomTextStyles.titleMediumBlack,
),
),
SizedBox(height: 2.v),
Text(
"03-08-2023",
style: CustomTextStyles.bodyMediumBluegray800,
),
],
),
),
],
),
);
}
/// Section Widget
Widget _buildHeaderButton1(BuildContext context) {
return CustomOutlinedButton(
width: 73.h,
text: "14:52",
);
}
/// Section Widget
Widget _buildEnterProductName(BuildContext context) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 10.h),
child: CustomTextFormField(
controller: enterProductNameController,
hintText: "Введите названия продукта",
),
);
}
/// Section Widget
Widget _buildQuantity(BuildContext context) {
return CustomTextFormField(
width: 146.h,
controller: quantityController,
hintText: "Кол-во",
textInputAction: TextInputAction.done,
);
}
/// Section Widget
Widget _buildAddProduct(BuildContext context) {
return CustomOutlinedButton(
text: "Добавить продукт",
margin: EdgeInsets.symmetric(horizontal: 10.h),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
);
}
/// Section Widget
Widget _buildAddPhoto(BuildContext context) {
return CustomOutlinedButton(
width: 195.h,
text: "Добавить фото",
rightIcon: Container(
margin: EdgeInsets.only(left: 14.h),
child: CustomImageView(
imagePath: ImageConstant.imgVector,
height: 30.v,
width: 34.h,
),
),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
buttonTextStyle: theme.textTheme.bodyMedium!,
);
}
/// Section Widget
Widget _buildImageStack(BuildContext context) {
return SizedBox(
height: 790.v,
width: 374.h,
child: Stack(
alignment: Alignment.bottomCenter,
children: [
Align(
alignment: Alignment.center,
child: Container(
margin: EdgeInsets.symmetric(horizontal: 15.h),
padding: EdgeInsets.symmetric(
horizontal: 10.h,
vertical: 13.v,
),
decoration: AppDecoration.outlinePrimary.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder12,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 10.h),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: EdgeInsets.only(
top: 11.v,
bottom: 7.v,
),
child: Text(
"Время приема пищи",
style: CustomTextStyles.bodyMediumBlack900,
),
),
_buildHeaderButton1(context),
],
),
),
SizedBox(height: 20.v),
Padding(
padding: EdgeInsets.symmetric(horizontal: 10.h),
child: CustomDropDown(
hintText: "Прием пищи",
items: dropdownItemList1,
contentPadding: EdgeInsets.symmetric(
horizontal: 15.h,
vertical: 9.v,
),
onChanged: (value) {},
),
),
SizedBox(height: 20.v),
_buildEnterProductName(context),
SizedBox(height: 20.v),
Padding(
padding: EdgeInsets.symmetric(horizontal: 10.h),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildQuantity(context),
Padding(
padding: EdgeInsets.only(left: 10.h),
child: CustomDropDown(
width: 146.h,
hintText: "Порция",
items: dropdownItemList2,
contentPadding: EdgeInsets.symmetric(
horizontal: 15.h,
vertical: 9.v,
),
onChanged: (value) {},
),
),
],
),
),
SizedBox(height: 30.v),
Container(
margin: EdgeInsets.symmetric(horizontal: 10.h),
padding: EdgeInsets.symmetric(
horizontal: 8.h,
vertical: 6.v,
),
decoration: AppDecoration.fillGray.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder5,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.only(
left: 7.h,
top: 5.v,
),
child: Text(
"Сырный суп",
style: CustomTextStyles.titleMedium16,
),
),
Padding(
padding: EdgeInsets.only(
left: 20.h,
top: 3.v,
bottom: 3.v,
),
child: Text(
"400мл",
style: CustomTextStyles.titleMedium16,
),
),
Spacer(),
CustomImageView(
imagePath: ImageConstant.imgSearch,
height: 24.adaptSize,
width: 24.adaptSize,
),
CustomImageView(
imagePath: ImageConstant.imgClose,
height: 28.adaptSize,
width: 28.adaptSize,
margin: EdgeInsets.only(left: 10.h),
),
],
),
),
SizedBox(height: 30.v),
_buildAddProduct(context),
SizedBox(height: 249.v),
_buildAddPhoto(context),
SizedBox(height: 29.v),
Text(
"Ваша сытость после приёма пищи",
style: CustomTextStyles.bodyMediumBlack900,
),
SizedBox(height: 15.v),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CustomImageView(
imagePath: ImageConstant.imgEdit,
height: 30.adaptSize,
width: 30.adaptSize,
),
CustomImageView(
height: 13.v,
width: 243.h,
margin: EdgeInsets.only(
left: 10.h,
top: 9.v,
bottom: 8.v,
),
),
CustomImageView(
height: 30.adaptSize,
width: 30.adaptSize,
margin: EdgeInsets.only(left: 10.h),
),
],
),
SizedBox(height: 7.v),
],
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: EdgeInsets.only(bottom: 342.v),
child: SizedBox(
width: 374.h,
child: Divider(),
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 203.v,
width: 303.h,
margin: EdgeInsets.only(bottom: 178.v),
child: Stack(
alignment: Alignment.topRight,
children: [
CustomImageView(
height: 203.v,
width: 303.h,
radius: BorderRadius.circular(
5.h,
),
alignment: Alignment.center,
),
Align(
alignment: Alignment.topRight,
child: Padding(
padding: EdgeInsets.only(
left: 265.h,
top: 10.v,
right: 10.h,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CustomIconButton(
height: 18.adaptSize,
width: 18.adaptSize,
padding: EdgeInsets.all(4.h),
alignment: Alignment.centerRight,
child: CustomImageView(
imagePath: ImageConstant.imgCloseBlueGray900,
),
),
SizedBox(height: 60.v),
CustomIconButton(
height: 28.adaptSize,
width: 28.adaptSize,
padding: EdgeInsets.all(9.h),
decoration: IconButtonStyleHelper.fillBlueA,
child: CustomImageView(
imagePath: ImageConstant.imageNotFound,
),
),
],
),
),
),
],
),
),
),
],
),
);
}
/// Section Widget
Widget _buildHeaderColumn(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 16.h),
padding: EdgeInsets.symmetric(
horizontal: 10.h,
vertical: 16.v,
),
decoration: AppDecoration.outlinePrimary.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder12,
),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 3.v),
Text(
"Ваш КБЖУ",
style: theme.textTheme.titleMedium,
),
SizedBox(height: 8.v),
Text(
"От дневной суточной нормы",
style: CustomTextStyles.bodyMediumBluegray800,
),
SizedBox(height: 19.v),
Align(
alignment: Alignment.centerRight,
child: Padding(
padding: EdgeInsets.only(
left: 30.h,
right: 12.h,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
margin: EdgeInsets.only(top: 7.v),
decoration: AppDecoration.outlinePrimary.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder1,
),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
children: [
SizedBox(height: 101.v),
Container(
height: 47.v,
width: 12.h,
decoration: BoxDecoration(
color: theme.colorScheme.errorContainer,
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(1.h),
),
),
),
],
),
),
Spacer(
flex: 33,
),
Container(
margin: EdgeInsets.only(top: 7.v),
decoration: AppDecoration.outlinePrimary.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder1,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 66.v),
Container(
height: 82.v,
width: 12.h,
decoration: BoxDecoration(
color: appTheme.lightGreenA700,
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(1.h),
),
),
),
],
),
),
Spacer(
flex: 33,
),
Container(
margin: EdgeInsets.only(top: 7.v),
decoration: AppDecoration.outlinePrimary.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder1,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 14.v),
Container(
height: 134.v,
width: 12.h,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(1.h),
),
),
),
],
),
),
Spacer(
flex: 33,
),
Container(
margin: EdgeInsets.only(top: 7.v),
decoration: AppDecoration.outlinePrimary.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder1,
),
child: Container(
height: 148.v,
width: 12.h,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(
1.h,
),
),
),
),
Padding(
padding: EdgeInsets.only(
left: 6.h,
bottom: 128.v,
),
child: Text(
"*",
style: theme.textTheme.titleLarge,
),
),
],
),
),
),
SizedBox(height: 8.v),
Padding(
padding: EdgeInsets.only(left: 4.h),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Калории",
style: theme.textTheme.titleSmall,
),
Padding(
padding: EdgeInsets.only(left: 28.h),
child: Text(
"Белки",
style: theme.textTheme.titleSmall,
),
),
Spacer(),
Text(
"Жиры",
style: theme.textTheme.titleSmall,
),
Padding(
padding: EdgeInsets.only(left: 25.h),
child: Text(
"Углеводы",
style: theme.textTheme.titleSmall,
),
),
],
),
),
SizedBox(height: 2.v),
Padding(
padding: EdgeInsets.only(
left: 10.h,
right: 2.h,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"255 из 250Ккал",
style: theme.textTheme.bodySmall,
),
Padding(
padding: EdgeInsets.only(
left: 25.h,
bottom: 11.v,
),
child: Text(
"50 из 250 гр",
style: theme.textTheme.bodySmall,
),
),
Padding(
padding: EdgeInsets.only(
left: 19.h,
bottom: 11.v,
),
child: Text(
"270 из 250 гр",
style: theme.textTheme.bodySmall,
),
),
Padding(
padding: EdgeInsets.only(
left: 16.h,
bottom: 11.v,
),
child: Text(
"400 из 250 гр",
style: theme.textTheme.bodySmall,
),
),
],
),
),
SizedBox(height: 19.v),
Text(
"*Превышенная суточная норма углеводов",
style: theme.textTheme.labelLarge,
),
],
),
);
}
/// Section Widget
Widget _buildHeaderColumn1(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Container(
margin: EdgeInsets.symmetric(horizontal: 15.h),
padding: EdgeInsets.symmetric(
horizontal: 50.h,
vertical: 28.v,
),
decoration: AppDecoration.outlinePrimary.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder12,
),
child: SizedBox(
width: 240.h,
child: Text(
"Дополнительная информация для консультанта",
maxLines: 2,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
style: CustomTextStyles.bodyMediumBlack900,
),
),
),
);
}
/// Section Widget
Widget _buildHeaderStack(BuildContext context) {
return SizedBox(
height: 235.v,
width: 374.h,
child: Stack(
alignment: Alignment.bottomCenter,
children: [
_buildHeaderColumn1(context),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: EdgeInsets.only(bottom: 103.v),
child: SizedBox(
width: 374.h,
child: Divider(),
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
margin: EdgeInsets.only(
left: 35.h,
right: 35.h,
bottom: 20.v,
),
padding: EdgeInsets.symmetric(
horizontal: 14.h,
vertical: 9.v,
),
decoration: AppDecoration.outlineBlueGray.copyWith(
borderRadius: BorderRadiusStyle.roundedBorder5,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 63.v),
Container(
width: 245.h,
margin: EdgeInsets.only(right: 27.h),
child: Text(
"Введите дополнитльеную информацию для консультанта",
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: theme.textTheme.bodyLarge,
),
),
],
),
),
),
],
),
);
}
/// Section Widget
Widget _buildEndDay(BuildContext context) {
return CustomOutlinedButton(
text: "Закончить день",
margin: EdgeInsets.only(
left: 16.h,
right: 16.h,
bottom: 41.v,
),
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
);
}
}

View File

@@ -1,16 +0,0 @@
// ignore_for_file: must_be_immutable
import 'package:equatable/equatable.dart';
/// This class defines the variables used in the [k0_page],
/// and is typically used to hold data that is passed between different parts of the application.
class DiaryEditModel extends Equatable {
DiaryEditModel();
DiaryEditModel copyWith() {
return DiaryEditModel();
}
@override
List<Object?> get props => [];
}