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'; class PortionWidget extends StatefulWidget { const PortionWidget({super.key}); @override State createState() => _PortionWidgetState(); } class _PortionWidgetState extends State { late final CreateRecipeCubit createRecipeCubit; @override void initState() { super.initState(); createRecipeCubit = context.read(); } @override Widget build(BuildContext context) { return BlocBuilder( buildWhen: (previous, current) => previous.servingsValidationError != current.servingsValidationError || previous.energyPerServingValidationError != current.energyPerServingValidationError, builder: (context, state) { return Column( mainAxisSize: MainAxisSize.min, children: [ 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), 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!, ), ], ], ), ], ), ), ], ), ], ); }, ); } }