Files
eatvio_app/lib/features/diary/presentation/widgets/meal_comment_widget.dart
2025-04-24 04:20:24 +07:00

75 lines
2.9 KiB
Dart

import 'package:easy_debounce/easy_debounce.dart';
import 'package:eatvio_app/core/theme/custom_text_style.dart';
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/diary/presentation/bloc/consultant_comments_cubit/consultant_comments_cubit.dart';
import 'package:eatvio_app/features/diary/presentation/bloc/consultant_comments_cubit/consultant_comments_state.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class MealCommentWidget extends StatefulWidget {
const MealCommentWidget({super.key, required this.selectedMeal});
final int selectedMeal;
@override
State<MealCommentWidget> createState() => _MealCommentWidgetState();
}
class _MealCommentWidgetState extends State<MealCommentWidget> {
final controller = TextEditingController();
@override
void didUpdateWidget(MealCommentWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.selectedMeal != oldWidget.selectedMeal) {
final state = context.read<ConsultantCommentsCubit>().state;
controller.text = state.commentsModel.mealsComments[widget.selectedMeal.toString()]?.comment ?? '';
}
}
@override
Widget build(BuildContext context) {
final s = S.of(context);
return BlocBuilder<ConsultantCommentsCubit, ConsultantCommentsState>(
builder: (context, state) {
final bool hasError;
if (state.commentsModel.mealsComments[widget.selectedMeal.toString()] != null &&
state.commentsModel.mealsComments[widget.selectedMeal.toString()]!.comment.length < 3) {
hasError = true;
} else {
hasError = false;
}
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'${s.commentForMeal} ${widget.selectedMeal}',
textAlign: TextAlign.center,
style: TextStyles.textLSemibold.copyWith(color: appTheme.black),
),
const SizedBox(height: 12),
CustomTextFormField(
controller: controller,
hasError: hasError,
onChanged: (value) {
EasyDebounce.debounce(
'client-typing',
const Duration(milliseconds: 500),
() => context.read<ConsultantCommentsCubit>().setMealComment(mealNumber: widget.selectedMeal, comment: value),
);
},
hintText: s.typeMealComment,
minLines: 4,
maxLines: 4,
readOnly: false,
),
if (hasError) ...[const SizedBox(height: 4), const ValidationErrorWidget(error: 'Минимум 3 символа')],
],
);
},
);
}
}