internet connection handling

This commit is contained in:
Username1548
2025-02-03 15:32:12 +07:00
parent 4242265830
commit 589122581b
30 changed files with 1384 additions and 666 deletions

View File

@@ -1,3 +1,4 @@
import 'dart:developer';
import 'dart:io';
import 'package:eatvio_app/features/recipes/entities/recipe_create_entity.dart';
@@ -11,10 +12,10 @@ class CreateRecipeCubit extends Cubit<CreateRecipeState> {
CreateRecipeCubit({required this.recipeRepository})
: super(
CreateRecipeState(
RecipeCreateEntity(
entity: RecipeCreateEntity(
typeOfRecipe: TypeOfRecipe.first_meals,
ingredients: [],
steps: [],
steps: [StepEntity()],
tags: [],
),
),
@@ -22,13 +23,11 @@ class CreateRecipeCubit extends Cubit<CreateRecipeState> {
addStep() {
emit(
CreateRecipeState(
state.entity.copyWith(
steps: [
...state.entity.steps,
StepEntity(),
],
),
state.copyWith.entity(
steps: [
...state.entity.steps,
StepEntity(),
],
),
);
}
@@ -36,21 +35,16 @@ class CreateRecipeCubit extends Cubit<CreateRecipeState> {
removeStep(StepEntity step) {
List<StepEntity> steps = [...state.entity.steps];
steps.remove(step);
emit(
CreateRecipeState(state.entity.copyWith(steps: steps)),
);
emit(state.copyWith.entity(steps: steps));
}
addIngredient(Ingredient entity) {
emit(
CreateRecipeState(
state.entity.copyWith(
ingredients: [
...state.entity.ingredients,
entity,
],
),
state.copyWith.entity(
ingredients: [
...state.entity.ingredients,
entity,
],
),
);
}
@@ -59,67 +53,35 @@ class CreateRecipeCubit extends Cubit<CreateRecipeState> {
List<Ingredient> ingredients = [...state.entity.ingredients];
ingredients.removeAt(index);
emit(
CreateRecipeState(
state.entity.copyWith(
ingredients: ingredients,
),
),
);
emit(state.copyWith.entity(ingredients: ingredients));
}
replaceIngredientPosition(int oldIndex, int newIndex) {
List<Ingredient> ingredients = [...state.entity.ingredients];
final item = ingredients.removeAt(oldIndex);
ingredients.insert(newIndex, item);
emit(
CreateRecipeState(
state.entity.copyWith(
ingredients: ingredients,
),
),
);
emit(state.copyWith.entity(ingredients: ingredients));
}
replaceTitle(String title) {
emit(
CreateRecipeState(
state.entity.copyWith(
title: title,
),
),
);
emit(state.copyWith.entity(title: title == '' ? null : title));
}
replaceDescription(String description) {
emit(
CreateRecipeState(
state.entity.copyWith(
description: description,
),
),
);
emit(state.copyWith.entity(description: description == '' ? null : description));
}
changeType(TypeOfRecipe typeOfRecipe) {
emit(
CreateRecipeState(
state.entity.copyWith(
typeOfRecipe: typeOfRecipe,
),
),
);
emit(state.copyWith.entity(typeOfRecipe: typeOfRecipe));
}
addTag(String tag) {
emit(
CreateRecipeState(
state.entity.copyWith(
tags: [
...state.entity.tags,
tag,
],
),
state.copyWith.entity(
tags: [
...state.entity.tags,
tag,
],
),
);
}
@@ -128,104 +90,145 @@ class CreateRecipeCubit extends Cubit<CreateRecipeState> {
List<String> tags = [...state.entity.tags];
tags.remove(tag);
emit(
CreateRecipeState(
state.entity.copyWith(
tags: tags,
),
),
);
emit(state.copyWith.entity(tags: tags));
}
replacePortionsCount(int count) {
emit(
CreateRecipeState(
state.entity.copyWith(
countOfPortions: count,
),
),
);
emit(state.copyWith.entity(countOfPortions: count));
}
replaceEnegryInPortion(double energy) {
emit(
CreateRecipeState(
state.entity.copyWith(
energyInOnePortion: energy,
),
),
);
emit(state.copyWith.entity(energyInOnePortion: energy));
}
replaceTotalTime(String time) {
emit(
CreateRecipeState(
state.entity.copyWith(
totalCookTime: time,
),
),
);
emit(state.copyWith.entity(totalCookTime: time));
}
replaceActiveTime(String time) {
emit(
CreateRecipeState(
state.entity.copyWith(
activeCookTime: time,
),
),
);
emit(state.copyWith.entity(activeCookTime: time));
}
replaceVideoUrl(String videoUrl) {
emit(
CreateRecipeState(
state.entity.copyWith(
videoUrl: videoUrl,
),
),
);
emit(state.copyWith.entity(videoUrl: videoUrl));
}
replaceImage(File? image) {
emit(
CreateRecipeState(
state.entity.copyWith(
image: image,
),
),
);
emit(state.copyWith.entity(image: image));
}
replaceDescriptionInStep(String description, int index) {
List<StepEntity> steps = [...state.entity.steps];
steps[index] = steps[index].copyWith(description: description);
emit(
CreateRecipeState(
state.entity.copyWith(
steps: steps,
),
),
);
emit(state.copyWith.entity(steps: steps));
}
replaceImageInStep(File? image, int index) {
List<StepEntity> steps = [...state.entity.steps];
steps[index] = steps[index].copyWith(image: image);
emit(
CreateRecipeState(
state.entity.copyWith(
steps: steps,
),
),
);
emit(state.copyWith.entity(steps: steps));
}
Future<void> publishRecipe() async {
Future<bool> publishRecipe() async {
try {
await recipeRepository.publishRecipe(state.entity.toModel());
} catch (e) {
rethrow;
final model = state.entity.toModel();
await recipeRepository.publishRecipe(model);
return true;
} catch (e, st) {
log(e.toString(), stackTrace: st);
return false;
}
}
bool validateAll() {
final isTitleValid = validateTitle();
final isDescValid = validateDescription();
final isImageValid = validateImage();
final areServingsValid = validateServings();
final isCookTimeValid = validateTotalCookTime();
final isActiveCookTimeValid = validateActiveCookTime();
final isEnergyPerServingsValid = validateEnergyPerServing();
return isTitleValid && isDescValid && isImageValid && areServingsValid && isCookTimeValid && isActiveCookTimeValid && isEnergyPerServingsValid;
}
bool validateTitle() {
if (state.entity.title == null || (state.entity.title?.isEmpty ?? true)) {
emit(state.copyWith(titleValidationError: 'Поле не заполнено'));
return false;
} else if (state.entity.title!.length < 3) {
emit(state.copyWith(titleValidationError: 'Минимальная длина 3 символа '));
return false;
} else {
emit(state.copyWith(titleValidationError: null));
return true;
}
}
bool validateServings() {
if (state.entity.countOfPortions == null) {
emit(state.copyWith(servingsValidationError: 'Поле не заполнено'));
return false;
} else {
emit(state.copyWith(servingsValidationError: null));
return true;
}
}
bool validateEnergyPerServing() {
if (state.entity.energyInOnePortion == null) {
emit(state.copyWith(energyPerServingValidationError: 'Поле не заполнено'));
return false;
} else {
emit(state.copyWith(energyPerServingValidationError: null));
return true;
}
}
bool validateActiveCookTime() {
if (state.entity.activeCookTime == null || state.entity.activeCookTime == '00:00') {
emit(state.copyWith(activeTimeValidationError: 'Поле не заполнено'));
return false;
} else {
emit(state.copyWith(activeTimeValidationError: null));
return true;
}
}
bool validateTotalCookTime() {
if (state.entity.totalCookTime == null || state.entity.totalCookTime == '00:00') {
emit(state.copyWith(cookingTimeValidationError: 'Поле не заполнено'));
return false;
} else {
emit(state.copyWith(cookingTimeValidationError: null));
return true;
}
}
bool validateDescription() {
if (state.entity.description == null || (state.entity.description?.isEmpty ?? true)) {
emit(state.copyWith(descriptionValidationError: 'Поле не заполнено'));
return false;
} else if (state.entity.description!.length < 3) {
emit(state.copyWith(descriptionValidationError: 'Минимальная длина 3 символа '));
return false;
} else {
emit(state.copyWith(descriptionValidationError: null));
return true;
}
}
bool validateImage() {
if (state.entity.image == null) {
emit(state.copyWith(imageValidationError: 'Поле не заполнено'));
return false;
} else {
emit(state.copyWith(imageValidationError: null));
return true;
}
}
}

View File

@@ -4,8 +4,16 @@ import 'package:freezed_annotation/freezed_annotation.dart';
part 'create_recipe_state.freezed.dart';
@freezed
class CreateRecipeState with _$CreateRecipeState {
const factory CreateRecipeState(
RecipeCreateEntity entity,
) = _CreateRecipeState;
class CreateRecipeState with _$CreateRecipeState {
const factory CreateRecipeState({
required RecipeCreateEntity entity,
String? titleValidationError,
String? descriptionValidationError,
String? cookingTimeValidationError,
String? activeTimeValidationError,
String? servingsValidationError,
String? energyPerServingValidationError,
String? imageValidationError,
}) = _CreateRecipeState;
}

View File

@@ -17,6 +17,14 @@ final _privateConstructorUsedError = UnsupportedError(
/// @nodoc
mixin _$CreateRecipeState {
RecipeCreateEntity get entity => throw _privateConstructorUsedError;
String? get titleValidationError => throw _privateConstructorUsedError;
String? get descriptionValidationError => throw _privateConstructorUsedError;
String? get cookingTimeValidationError => throw _privateConstructorUsedError;
String? get activeTimeValidationError => throw _privateConstructorUsedError;
String? get servingsValidationError => throw _privateConstructorUsedError;
String? get energyPerServingValidationError =>
throw _privateConstructorUsedError;
String? get imageValidationError => throw _privateConstructorUsedError;
/// Create a copy of CreateRecipeState
/// with the given fields replaced by the non-null parameter values.
@@ -31,7 +39,15 @@ abstract class $CreateRecipeStateCopyWith<$Res> {
CreateRecipeState value, $Res Function(CreateRecipeState) then) =
_$CreateRecipeStateCopyWithImpl<$Res, CreateRecipeState>;
@useResult
$Res call({RecipeCreateEntity entity});
$Res call(
{RecipeCreateEntity entity,
String? titleValidationError,
String? descriptionValidationError,
String? cookingTimeValidationError,
String? activeTimeValidationError,
String? servingsValidationError,
String? energyPerServingValidationError,
String? imageValidationError});
$RecipeCreateEntityCopyWith<$Res> get entity;
}
@@ -52,12 +68,48 @@ class _$CreateRecipeStateCopyWithImpl<$Res, $Val extends CreateRecipeState>
@override
$Res call({
Object? entity = null,
Object? titleValidationError = freezed,
Object? descriptionValidationError = freezed,
Object? cookingTimeValidationError = freezed,
Object? activeTimeValidationError = freezed,
Object? servingsValidationError = freezed,
Object? energyPerServingValidationError = freezed,
Object? imageValidationError = freezed,
}) {
return _then(_value.copyWith(
entity: null == entity
? _value.entity
: entity // ignore: cast_nullable_to_non_nullable
as RecipeCreateEntity,
titleValidationError: freezed == titleValidationError
? _value.titleValidationError
: titleValidationError // ignore: cast_nullable_to_non_nullable
as String?,
descriptionValidationError: freezed == descriptionValidationError
? _value.descriptionValidationError
: descriptionValidationError // ignore: cast_nullable_to_non_nullable
as String?,
cookingTimeValidationError: freezed == cookingTimeValidationError
? _value.cookingTimeValidationError
: cookingTimeValidationError // ignore: cast_nullable_to_non_nullable
as String?,
activeTimeValidationError: freezed == activeTimeValidationError
? _value.activeTimeValidationError
: activeTimeValidationError // ignore: cast_nullable_to_non_nullable
as String?,
servingsValidationError: freezed == servingsValidationError
? _value.servingsValidationError
: servingsValidationError // ignore: cast_nullable_to_non_nullable
as String?,
energyPerServingValidationError: freezed ==
energyPerServingValidationError
? _value.energyPerServingValidationError
: energyPerServingValidationError // ignore: cast_nullable_to_non_nullable
as String?,
imageValidationError: freezed == imageValidationError
? _value.imageValidationError
: imageValidationError // ignore: cast_nullable_to_non_nullable
as String?,
) as $Val);
}
@@ -80,7 +132,15 @@ abstract class _$$CreateRecipeStateImplCopyWith<$Res>
__$$CreateRecipeStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({RecipeCreateEntity entity});
$Res call(
{RecipeCreateEntity entity,
String? titleValidationError,
String? descriptionValidationError,
String? cookingTimeValidationError,
String? activeTimeValidationError,
String? servingsValidationError,
String? energyPerServingValidationError,
String? imageValidationError});
@override
$RecipeCreateEntityCopyWith<$Res> get entity;
@@ -100,12 +160,48 @@ class __$$CreateRecipeStateImplCopyWithImpl<$Res>
@override
$Res call({
Object? entity = null,
Object? titleValidationError = freezed,
Object? descriptionValidationError = freezed,
Object? cookingTimeValidationError = freezed,
Object? activeTimeValidationError = freezed,
Object? servingsValidationError = freezed,
Object? energyPerServingValidationError = freezed,
Object? imageValidationError = freezed,
}) {
return _then(_$CreateRecipeStateImpl(
null == entity
entity: null == entity
? _value.entity
: entity // ignore: cast_nullable_to_non_nullable
as RecipeCreateEntity,
titleValidationError: freezed == titleValidationError
? _value.titleValidationError
: titleValidationError // ignore: cast_nullable_to_non_nullable
as String?,
descriptionValidationError: freezed == descriptionValidationError
? _value.descriptionValidationError
: descriptionValidationError // ignore: cast_nullable_to_non_nullable
as String?,
cookingTimeValidationError: freezed == cookingTimeValidationError
? _value.cookingTimeValidationError
: cookingTimeValidationError // ignore: cast_nullable_to_non_nullable
as String?,
activeTimeValidationError: freezed == activeTimeValidationError
? _value.activeTimeValidationError
: activeTimeValidationError // ignore: cast_nullable_to_non_nullable
as String?,
servingsValidationError: freezed == servingsValidationError
? _value.servingsValidationError
: servingsValidationError // ignore: cast_nullable_to_non_nullable
as String?,
energyPerServingValidationError: freezed ==
energyPerServingValidationError
? _value.energyPerServingValidationError
: energyPerServingValidationError // ignore: cast_nullable_to_non_nullable
as String?,
imageValidationError: freezed == imageValidationError
? _value.imageValidationError
: imageValidationError // ignore: cast_nullable_to_non_nullable
as String?,
));
}
}
@@ -113,14 +209,36 @@ class __$$CreateRecipeStateImplCopyWithImpl<$Res>
/// @nodoc
class _$CreateRecipeStateImpl implements _CreateRecipeState {
const _$CreateRecipeStateImpl(this.entity);
const _$CreateRecipeStateImpl(
{required this.entity,
this.titleValidationError,
this.descriptionValidationError,
this.cookingTimeValidationError,
this.activeTimeValidationError,
this.servingsValidationError,
this.energyPerServingValidationError,
this.imageValidationError});
@override
final RecipeCreateEntity entity;
@override
final String? titleValidationError;
@override
final String? descriptionValidationError;
@override
final String? cookingTimeValidationError;
@override
final String? activeTimeValidationError;
@override
final String? servingsValidationError;
@override
final String? energyPerServingValidationError;
@override
final String? imageValidationError;
@override
String toString() {
return 'CreateRecipeState(entity: $entity)';
return 'CreateRecipeState(entity: $entity, titleValidationError: $titleValidationError, descriptionValidationError: $descriptionValidationError, cookingTimeValidationError: $cookingTimeValidationError, activeTimeValidationError: $activeTimeValidationError, servingsValidationError: $servingsValidationError, energyPerServingValidationError: $energyPerServingValidationError, imageValidationError: $imageValidationError)';
}
@override
@@ -128,11 +246,42 @@ class _$CreateRecipeStateImpl implements _CreateRecipeState {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$CreateRecipeStateImpl &&
(identical(other.entity, entity) || other.entity == entity));
(identical(other.entity, entity) || other.entity == entity) &&
(identical(other.titleValidationError, titleValidationError) ||
other.titleValidationError == titleValidationError) &&
(identical(other.descriptionValidationError,
descriptionValidationError) ||
other.descriptionValidationError ==
descriptionValidationError) &&
(identical(other.cookingTimeValidationError,
cookingTimeValidationError) ||
other.cookingTimeValidationError ==
cookingTimeValidationError) &&
(identical(other.activeTimeValidationError,
activeTimeValidationError) ||
other.activeTimeValidationError == activeTimeValidationError) &&
(identical(
other.servingsValidationError, servingsValidationError) ||
other.servingsValidationError == servingsValidationError) &&
(identical(other.energyPerServingValidationError,
energyPerServingValidationError) ||
other.energyPerServingValidationError ==
energyPerServingValidationError) &&
(identical(other.imageValidationError, imageValidationError) ||
other.imageValidationError == imageValidationError));
}
@override
int get hashCode => Object.hash(runtimeType, entity);
int get hashCode => Object.hash(
runtimeType,
entity,
titleValidationError,
descriptionValidationError,
cookingTimeValidationError,
activeTimeValidationError,
servingsValidationError,
energyPerServingValidationError,
imageValidationError);
/// Create a copy of CreateRecipeState
/// with the given fields replaced by the non-null parameter values.
@@ -145,11 +294,32 @@ class _$CreateRecipeStateImpl implements _CreateRecipeState {
}
abstract class _CreateRecipeState implements CreateRecipeState {
const factory _CreateRecipeState(final RecipeCreateEntity entity) =
_$CreateRecipeStateImpl;
const factory _CreateRecipeState(
{required final RecipeCreateEntity entity,
final String? titleValidationError,
final String? descriptionValidationError,
final String? cookingTimeValidationError,
final String? activeTimeValidationError,
final String? servingsValidationError,
final String? energyPerServingValidationError,
final String? imageValidationError}) = _$CreateRecipeStateImpl;
@override
RecipeCreateEntity get entity;
@override
String? get titleValidationError;
@override
String? get descriptionValidationError;
@override
String? get cookingTimeValidationError;
@override
String? get activeTimeValidationError;
@override
String? get servingsValidationError;
@override
String? get energyPerServingValidationError;
@override
String? get imageValidationError;
/// Create a copy of CreateRecipeState
/// with the given fields replaced by the non-null parameter values.

View File

@@ -212,7 +212,7 @@ class _AboutRecipesScreenState extends State<AboutRecipesScreen> {
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
widget.recipes.title,
widget.recipes.title ?? '',
style: TextStyle(
color: Colors.black,
fontSize: 24,
@@ -241,7 +241,7 @@ class _AboutRecipesScreenState extends State<AboutRecipesScreen> {
Icon(Icons.fast_forward_outlined),
SizedBox(width: 4),
Text(
widget.recipes.steps.length.toString() + ' этапов',
'${widget.recipes.steps?.length ?? 0}' + ' этапов',
style: TextStyle(
color: Colors.black,
fontSize: 12,
@@ -286,9 +286,14 @@ class _AboutRecipesScreenState extends State<AboutRecipesScreen> {
padding: const EdgeInsets.symmetric(horizontal: 16),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, index) => _ingredient(widget.recipes.ingredients[index]),
itemBuilder: (context, index) {
if (widget.recipes.ingredients != null) {
return _ingredient(widget.recipes.ingredients![index]);
}
return SizedBox.shrink();
},
separatorBuilder: (context, index) => SizedBox(height: 4),
itemCount: widget.recipes.ingredients.length,
itemCount: widget.recipes.ingredients?.length ?? 0,
),
SizedBox(height: 12),
Padding(
@@ -307,7 +312,10 @@ class _AboutRecipesScreenState extends State<AboutRecipesScreen> {
ListView.separated(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, index) => _step(widget.recipes.steps[index], index + 1),
itemBuilder: (context, index) {
if (widget.recipes.steps == null) return SizedBox.shrink();
return _step(widget.recipes.steps![index], index + 1);
},
separatorBuilder: (context, index) => Padding(
padding: const EdgeInsets.all(16),
child: Container(
@@ -318,7 +326,7 @@ class _AboutRecipesScreenState extends State<AboutRecipesScreen> {
),
),
),
itemCount: widget.recipes.steps.length,
itemCount: widget.recipes.steps?.length ?? 0,
),
SizedBox(height: 8),
Padding(

View File

@@ -1,6 +1,8 @@
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/utils/toasts.dart';
import 'package:eatvio_app/features/recipes/presentation/bloc/create_recipe_cubit/create_recipe_cubit.dart';
import 'package:eatvio_app/features/recipes/presentation/bloc/create_recipe_cubit/create_recipe_state.dart';
import 'package:eatvio_app/features/recipes/presentation/widgets/info_about_recipe_widget.dart';
import 'package:eatvio_app/features/recipes/presentation/widgets/ingredients_widget.dart';
import 'package:eatvio_app/features/recipes/presentation/widgets/steps_widget.dart';
@@ -42,32 +44,44 @@ class _RecipeCreateScreenState extends State<RecipeCreateScreen> {
InfoAboutRecipeWidget(),
IngredientsWidget(),
StepsWidget(),
Builder(
builder: (context) {
return InkWell(
onTap: () {
context.read<CreateRecipeCubit>().publishRecipe();
},
borderRadius: BorderRadius.circular(20),
child: Ink(
width: MediaQuery.of(context).size.width - 32,
decoration: BoxDecoration(
color: appTheme.blueA400,
BlocBuilder<CreateRecipeCubit, CreateRecipeState>(
builder: (context, state) {
return Builder(
builder: (context) {
return InkWell(
onTap: () async {
if (context.read<CreateRecipeCubit>().validateAll()) {
final isSuccess = await context.read<CreateRecipeCubit>().publishRecipe();
if (isSuccess) {
showSuccessToast('Рецепт успешно опубликован');
Navigator.pop(context);
} else {
showErrorToast('Не удалось опубликовать рецепт');
}
}
},
borderRadius: BorderRadius.circular(20),
),
child: Container(
padding: const EdgeInsets.all(16),
alignment: Alignment.center,
child: Text(
'Опубликовать',
style: TextStyle(
fontSize: 16,
color: Colors.white,
fontWeight: FontWeight.w600,
child: Ink(
width: MediaQuery.of(context).size.width - 32,
decoration: BoxDecoration(
color: appTheme.blueA400,
borderRadius: BorderRadius.circular(20),
),
child: Container(
padding: const EdgeInsets.all(16),
alignment: Alignment.center,
child: Text(
'Опубликовать',
style: TextStyle(
fontSize: 16,
color: Colors.white,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
);
},
);
},
),

View File

@@ -8,14 +8,13 @@ import 'package:flutter_svg/flutter_svg.dart';
import 'package:image_picker/image_picker.dart';
class ImageWidget extends StatefulWidget {
const ImageWidget({super.key});
const ImageWidget({super.key, required this.image});
final File? image;
@override
State<ImageWidget> createState() => _ImageWidgetState();
}
class _ImageWidgetState extends State<ImageWidget> {
XFile? image;
late final CreateRecipeCubit createRecipeCubit;
@override
@@ -30,7 +29,7 @@ class _ImageWidgetState extends State<ImageWidget> {
onTap: () {
_showImagePicker();
},
child: image == null
child: widget.image == null
? SvgPicture.asset(
width: MediaQuery.of(context).size.width - 32,
'assets/icons/upload_photo_of_portion.svg',
@@ -40,7 +39,7 @@ class _ImageWidgetState extends State<ImageWidget> {
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.file(
File(image!.path),
File(widget.image!.path),
),
),
Positioned(
@@ -48,9 +47,6 @@ class _ImageWidgetState extends State<ImageWidget> {
top: 16,
child: GestureDetector(
onTap: () {
setState(() {
image = null;
});
createRecipeCubit.replaceImage(null);
},
child: Container(
@@ -76,10 +72,11 @@ class _ImageWidgetState extends State<ImageWidget> {
CupertinoActionSheetAction(
onPressed: () async {
final picker = ImagePicker();
image = await picker.pickImage(source: ImageSource.camera);
final image = await picker.pickImage(source: ImageSource.camera);
setState(() {});
if (image != null) {
createRecipeCubit.replaceImage(File(image!.path));
createRecipeCubit.replaceImage(File(image.path));
createRecipeCubit.validateImage();
}
Navigator.of(context).pop();
},
@@ -94,10 +91,11 @@ class _ImageWidgetState extends State<ImageWidget> {
CupertinoActionSheetAction(
onPressed: () async {
final picker = ImagePicker();
image = await picker.pickImage(source: ImageSource.gallery);
final image = await picker.pickImage(source: ImageSource.gallery);
setState(() {});
if (image != null) {
createRecipeCubit.replaceImage(File(image!.path));
createRecipeCubit.replaceImage(File(image.path));
createRecipeCubit.validateImage();
}
Navigator.of(context).pop();
},

View File

@@ -1,8 +1,11 @@
import 'package:eatvio_app/core/data/selectionPopupModel/selection_popup_model.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/widgets/custom_drop_down.dart';
import 'package:eatvio_app/core/widgets/custom_text_form_field.dart';
import 'package:eatvio_app/features/create_diet/presentation/widgets/error_widget.dart';
import 'package:eatvio_app/features/recipes/entities/recipe_create_entity.dart';
import 'package:eatvio_app/features/recipes/presentation/bloc/create_recipe_cubit/create_recipe_cubit.dart';
import 'package:eatvio_app/features/recipes/presentation/bloc/create_recipe_cubit/create_recipe_state.dart';
import 'package:eatvio_app/features/recipes/presentation/widgets/image_widget.dart';
import 'package:eatvio_app/features/recipes/presentation/widgets/portion_widget.dart';
import 'package:eatvio_app/features/recipes/presentation/widgets/tags_widget.dart';
@@ -40,10 +43,29 @@ class _InfoAboutRecipeWidgetState extends State<InfoAboutRecipeWidget> {
children: [
Text('Название'),
SizedBox(height: 4),
CustomTextFormField(
hintText: 'Например: тар-тар из говядины',
onChanged: (value) {
createRecipeCubit.replaceTitle(value);
BlocBuilder<CreateRecipeCubit, CreateRecipeState>(
buildWhen: (previous, current) => previous.titleValidationError != current.titleValidationError,
builder: (context, state) {
return Column(
children: [
CustomTextFormField(
hintText: 'Например: тар-тар из говядины',
hasError: state.titleValidationError != null,
onChanged: (value) {
createRecipeCubit.replaceTitle(value);
if (state.titleValidationError != null) {
createRecipeCubit.validateTitle();
}
},
),
if (state.titleValidationError != null) ...[
SizedBox(height: 4),
CustomErrorWidget(
error: state.titleValidationError!,
),
],
],
);
},
),
const SizedBox(height: 12),
@@ -70,18 +92,57 @@ class _InfoAboutRecipeWidgetState extends State<InfoAboutRecipeWidget> {
const SizedBox(height: 12),
Text('Описание'),
SizedBox(height: 4),
CustomTextFormField(
hintText: 'Расскажите, почему вы выбрали именно этот рецепт? Опишите, каким будет готовое блюдо и в чём его достоинства.',
minLines: 5,
maxLines: 5,
onChanged: (value) {
createRecipeCubit.replaceDescription(value);
BlocBuilder<CreateRecipeCubit, CreateRecipeState>(
buildWhen: (previous, current) => previous.descriptionValidationError != current.descriptionValidationError,
builder: (context, state) {
return Column(
children: [
CustomTextFormField(
hintText: 'Расскажите, почему вы выбрали именно этот рецепт? Опишите, каким будет готовое блюдо и в чём его достоинства.',
minLines: 5,
maxLines: 5,
hasError: state.descriptionValidationError != null,
onChanged: (value) {
createRecipeCubit.replaceDescription(value);
if (state.descriptionValidationError != null) {
createRecipeCubit.validateDescription();
}
},
),
if (state.descriptionValidationError != null) ...[
SizedBox(height: 4),
CustomErrorWidget(
error: state.descriptionValidationError!,
),
],
],
);
},
),
const SizedBox(height: 12),
Text('Фото обложка'),
SizedBox(height: 4),
ImageWidget(),
BlocBuilder<CreateRecipeCubit, CreateRecipeState>(
buildWhen: (previous, current) => previous.imageValidationError != current.imageValidationError || previous.entity.image != current.entity.image,
builder: (context, state) {
return Column(
children: [
Container(
child: ImageWidget(image: state.entity.image),
decoration: state.imageValidationError != null
? BoxDecoration(border: Border.all(width: 1, color: appTheme.red), borderRadius: BorderRadius.circular(8))
: null,
),
if (state.imageValidationError != null) ...[
SizedBox(height: 4),
CustomErrorWidget(
error: state.imageValidationError!,
),
],
],
);
},
),
const SizedBox(height: 12),
Text('Ссылка на видео (если есть)'),
SizedBox(height: 4),

View File

@@ -69,7 +69,7 @@ class _IngredientsWidgetState extends State<IngredientsWidget> {
color: Colors.grey.shade600,
),
title: Text(state.entity.ingredients[index].name),
subtitle: Text('${state.entity.ingredients[index].unit} ${state.entity.ingredients[index].portion == 'gr' ? 'гр' : 'шт'}'),
subtitle: Text('${state.entity.ingredients[index].portion} ${state.entity.ingredients[index].unit == 'gr' ? 'гр' : 'шт'}'),
trailing: IconButton(
onPressed: () {
createRecipeCubit.removeIngredient(index);
@@ -163,8 +163,8 @@ class _IngredientsWidgetState extends State<IngredientsWidget> {
createRecipeCubit.addIngredient(
Ingredient(
name: nameController.text,
unit: gramsController.text.isEmpty ? pcsController.text : gramsController.text,
portion: gramsController.text.isEmpty ? 'pcs' : 'gr',
unit: gramsController.text.isEmpty ? 'pcs' : 'gr',
portion: gramsController.text.isEmpty ? pcsController.text : gramsController.text,
),
);
Navigator.of(context).pop();

View File

@@ -1,5 +1,7 @@
import 'package:eatvio_app/core/widgets/custom_text_form_field.dart';
import 'package:eatvio_app/features/create_diet/presentation/widgets/error_widget.dart';
import 'package:eatvio_app/features/recipes/presentation/bloc/create_recipe_cubit/create_recipe_cubit.dart';
import 'package:eatvio_app/features/recipes/presentation/bloc/create_recipe_cubit/create_recipe_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -12,7 +14,6 @@ class PortionWidget extends StatefulWidget {
class _PortionWidgetState extends State<PortionWidget> {
late final CreateRecipeCubit createRecipeCubit;
@override
void initState() {
super.initState();
@@ -21,49 +22,86 @@ class _PortionWidgetState extends State<PortionWidget> {
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
return BlocBuilder<CreateRecipeCubit, CreateRecipeState>(
buildWhen: (previous, current) =>
previous.servingsValidationError != current.servingsValidationError ||
previous.energyPerServingValidationError != current.energyPerServingValidationError,
builder: (context, state) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Кол-во порций'),
SizedBox(height: 4),
CustomTextFormField(
hintText: '1',
onChanged: (value) {
createRecipeCubit.replacePortionsCount(int.parse(value));
},
Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Кол-во порций'),
SizedBox(height: 4),
Column(
children: [
CustomTextFormField(
hintText: '1',
hasError: state.servingsValidationError != null,
textInputType: TextInputType.number,
onChanged: (value) {
createRecipeCubit.replacePortionsCount(int.parse(value));
if (state.servingsValidationError != null) {
createRecipeCubit.validateServings();
}
},
),
if (state.servingsValidationError != null) ...[
SizedBox(height: 4),
CustomErrorWidget(
error: state.servingsValidationError!,
),
],
],
),
],
),
],
),
),
const SizedBox(width: 8),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Ккал в одной порции'),
SizedBox(height: 4),
CustomTextFormField(
hintText: '100',
suffixText: 'Ккал',
onChanged: (value) {
createRecipeCubit.replaceEnegryInPortion(double.parse(value));
},
),
const SizedBox(width: 8),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Ккал в одной порции'),
SizedBox(height: 4),
Column(
children: [
CustomTextFormField(
hintText: '100',
hasError: state.energyPerServingValidationError != null,
suffixText: 'Ккал',
textInputType: TextInputType.numberWithOptions(decimal: true),
onChanged: (value) {
createRecipeCubit.replaceEnegryInPortion(double.parse(value));
if (state.energyPerServingValidationError != null) {
createRecipeCubit.validateEnergyPerServing();
}
},
),
if (state.energyPerServingValidationError != null) ...[
SizedBox(height: 4),
CustomErrorWidget(
error: state.energyPerServingValidationError!,
),
],
],
),
],
),
],
),
),
],
),
],
),
],
);
},
);
}
}

View File

@@ -1,6 +1,8 @@
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/widgets/custom_text_form_field.dart';
import 'package:eatvio_app/features/create_diet/presentation/widgets/error_widget.dart';
import 'package:eatvio_app/features/recipes/presentation/bloc/create_recipe_cubit/create_recipe_cubit.dart';
import 'package:eatvio_app/features/recipes/presentation/bloc/create_recipe_cubit/create_recipe_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -22,130 +24,198 @@ class _TimeOfCookWidgetState extends State<TimeOfCookWidget> {
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Text('Общее время приготовления'),
const SizedBox(width: 8),
GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text(
'Общее время приготовления - это период времени от начала приготовления блюда до подачи его на стол.',
style: TextStyle(
fontWeight: FontWeight.w500,
color: Color(0xFF323F4B),
height: 1.2,
),
),
),
);
},
child: Icon(
Icons.help_outline_rounded,
color: appTheme.blueA400,
size: 20,
),
),
],
),
SizedBox(height: 4),
Row(
return BlocBuilder<CreateRecipeCubit, CreateRecipeState>(
buildWhen: (previous, current) =>
previous.activeTimeValidationError != current.activeTimeValidationError || previous.cookingTimeValidationError != current.cookingTimeValidationError,
builder: (context, state) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: CustomTextFormField(
hintText: 'Часов',
onChanged: (value) {
if (createRecipeCubit.state.entity.totalCookTime == null) {
createRecipeCubit.replaceTotalTime('${value.padLeft(2, '0')}:00');
} else {
createRecipeCubit.replaceTotalTime('${value.padLeft(2, '0')}:${createRecipeCubit.state.entity.totalCookTime!.substring(3, 5)}');
}
},
),
),
const SizedBox(width: 8),
Expanded(
child: CustomTextFormField(
hintText: 'Минут',
onChanged: (value) {
if (createRecipeCubit.state.entity.totalCookTime == null) {
createRecipeCubit.replaceTotalTime('00:${value.padLeft(2, '0')}');
} else {
createRecipeCubit.replaceTotalTime('${createRecipeCubit.state.entity.totalCookTime!.substring(0, 2)}:${value.padLeft(2, '0')}');
}
},
),
),
],
),
const SizedBox(height: 12),
Row(
children: [
Text('Активное время приготовления'),
const SizedBox(width: 8),
GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text(
'Активное время приготовления - это период времени, требующий активного участия в приготовлении блюда. Оно включает мытье и очистку продуктов, нарезку, шинковку, взбивание, перемешивание, размешивание, перекладывание и т.п.',
style: TextStyle(
fontWeight: FontWeight.w500,
color: Color(0xFF323F4B),
height: 1.2,
Row(
children: [
Text('Общее время приготовления'),
const SizedBox(width: 8),
GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text(
'Общее время приготовления - это период времени от начала приготовления блюда до подачи его на стол.',
style: TextStyle(
fontWeight: FontWeight.w500,
color: Color(0xFF323F4B),
height: 1.2,
),
),
),
),
);
},
child: Icon(
Icons.help_outline_rounded,
color: appTheme.blueA400,
size: 20,
),
);
},
child: Icon(
Icons.help_outline_rounded,
color: appTheme.blueA400,
size: 20,
),
),
],
),
SizedBox(height: 4),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Column(
children: [
CustomTextFormField(
hintText: 'Часов',
hasError: state.cookingTimeValidationError != null,
textInputType: TextInputType.number,
onChanged: (value) {
if (createRecipeCubit.state.entity.totalCookTime == null) {
createRecipeCubit.replaceTotalTime('${value.padLeft(2, '0')}:00');
} else {
createRecipeCubit.replaceTotalTime('${value.padLeft(2, '0')}:${createRecipeCubit.state.entity.totalCookTime!.substring(3, 5)}');
}
if (state.cookingTimeValidationError != null) {
createRecipeCubit.validateTotalCookTime();
}
},
),
if (state.cookingTimeValidationError != null) ...[
SizedBox(height: 4),
CustomErrorWidget(
error: state.cookingTimeValidationError!,
),
],
],
),
),
const SizedBox(width: 8),
Expanded(
child: Column(
children: [
CustomTextFormField(
hintText: 'Минут',
hasError: state.cookingTimeValidationError != null,
textInputType: TextInputType.number,
onChanged: (value) {
if (createRecipeCubit.state.entity.totalCookTime == null) {
createRecipeCubit.replaceTotalTime('00:${value.padLeft(2, '0')}');
} else {
createRecipeCubit.replaceTotalTime('${createRecipeCubit.state.entity.totalCookTime!.substring(0, 2)}:${value.padLeft(2, '0')}');
}
if (state.cookingTimeValidationError != null) {
createRecipeCubit.validateTotalCookTime();
}
},
),
if (state.cookingTimeValidationError != null) ...[
SizedBox(height: 4),
CustomErrorWidget(
error: state.cookingTimeValidationError!,
),
],
],
),
),
],
),
const SizedBox(height: 12),
Row(
children: [
Text('Активное время приготовления'),
const SizedBox(width: 8),
GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text(
'Активное время приготовления - это период времени, требующий активного участия в приготовлении блюда. Оно включает мытье и очистку продуктов, нарезку, шинковку, взбивание, перемешивание, размешивание, перекладывание и т.п.',
style: TextStyle(
fontWeight: FontWeight.w500,
color: Color(0xFF323F4B),
height: 1.2,
),
),
),
);
},
child: Icon(
Icons.help_outline_rounded,
color: appTheme.blueA400,
size: 20,
),
),
],
),
SizedBox(height: 4),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Column(
children: [
CustomTextFormField(
hintText: 'Часов',
hasError: state.activeTimeValidationError != null,
textInputType: TextInputType.number,
onChanged: (value) {
if (createRecipeCubit.state.entity.activeCookTime == null) {
createRecipeCubit.replaceActiveTime('${value.padLeft(2, '0')}:00');
} else {
createRecipeCubit.replaceActiveTime('${value.padLeft(2, '0')}:${createRecipeCubit.state.entity.activeCookTime!.substring(3, 5)}');
}
if (state.activeTimeValidationError != null) {
createRecipeCubit.validateActiveCookTime();
}
},
),
if (state.activeTimeValidationError != null) ...[
SizedBox(height: 4),
CustomErrorWidget(
error: state.activeTimeValidationError!,
),
],
],
),
),
const SizedBox(width: 8),
Expanded(
child: Column(
children: [
CustomTextFormField(
hintText: 'Минут',
hasError: state.activeTimeValidationError != null,
textInputType: TextInputType.number,
onChanged: (value) {
if (createRecipeCubit.state.entity.activeCookTime == null) {
createRecipeCubit.replaceActiveTime('00:${value.padLeft(2, '0')}');
} else {
createRecipeCubit.replaceActiveTime('${createRecipeCubit.state.entity.activeCookTime!.substring(0, 2)}:${value.padLeft(2, '0')}');
}
if (state.activeTimeValidationError != null) {
createRecipeCubit.validateActiveCookTime();
}
},
),
if (state.activeTimeValidationError != null) ...[
SizedBox(height: 4),
CustomErrorWidget(
error: state.activeTimeValidationError!,
),
],
],
),
),
],
),
],
),
SizedBox(height: 4),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: CustomTextFormField(
hintText: 'Часов',
onChanged: (value) {
if (createRecipeCubit.state.entity.activeCookTime == null) {
createRecipeCubit.replaceActiveTime('${value.padLeft(2, '0')}:00');
} else {
createRecipeCubit.replaceActiveTime('${value.padLeft(2, '0')}:${createRecipeCubit.state.entity.activeCookTime!.substring(3, 5)}');
}
},
),
),
const SizedBox(width: 8),
Expanded(
child: CustomTextFormField(
hintText: 'Минут',
onChanged: (value) {
if (createRecipeCubit.state.entity.activeCookTime == null) {
createRecipeCubit.replaceActiveTime('00:${value.padLeft(2, '0')}');
} else {
createRecipeCubit.replaceActiveTime('${createRecipeCubit.state.entity.activeCookTime!.substring(0, 2)}:${value.padLeft(2, '0')}');
}
},
),
),
],
),
],
);
},
);
}
}