From 8e49d056c586c4032cb880fff6f2bbb1e997d126 Mon Sep 17 00:00:00 2001 From: maxxkim Date: Thu, 15 Feb 2024 15:11:38 +0300 Subject: [PATCH] feat(diary) Made an event for satieety and mood change --- .../bloc/diary_edit_bloc.dart | 25 +++++-- .../bloc/diary_edit_event.dart | 11 +++- .../bloc/diary_edit_state.dart | 12 ++-- .../diary_edit_screen/diary_edit_screen.dart | 66 +++++-------------- 4 files changed, 50 insertions(+), 64 deletions(-) diff --git a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart index 4b879c67..bd114b8e 100644 --- a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart +++ b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:equatable/equatable.dart'; import 'package:flutter/material.dart'; import '/core/app_export.dart'; @@ -14,6 +16,8 @@ part 'diary_edit_state.dart'; class DiaryEditBloc extends Bloc { DiaryEditBloc(DiaryEditState initialState) : super(initialState) { on(_onInitialize); + on(_changeSatiety); + on(_changeMood); } Future _onInitialize( @@ -88,13 +92,22 @@ class DiaryEditBloc extends Bloc { ToggleWakeupExpandedEvent event, Emitter emit) { emit(state.copyWith(wakeupExpanded: !state.wakeupExpanded)); }*/ -} -class ScaleBloc extends Bloc { - ScaleBloc() : super(0); + Future _changeSatiety( + SatietyEvent event, Emitter emit) async { + if (state is DiaryEditLoadedState) { + var currentState = state as DiaryEditLoadedState; + emit(currentState.copyWith(selectedSatiety: event.selectedElement)); + currentState.selectedSatiety = event.selectedElement; + } + } - @override - Stream mapEventToState(ScaleEvent event) async* { - yield event.selectedElement; + Future _changeMood( + MoodEvent event, Emitter emit) async { + if (state is DiaryEditLoadedState) { + var currentState = state as DiaryEditLoadedState; + emit(currentState.copyWith(selectedMood: event.selectedElement)); + currentState.selectedMood = event.selectedElement; + } } } diff --git a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_event.dart b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_event.dart index a95e6037..d88fead6 100644 --- a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_event.dart +++ b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_event.dart @@ -15,10 +15,17 @@ class DiaryEditInitialEvent extends DiaryEditEvent { List get props => []; } -class ScaleEvent extends Equatable { +class SatietyEvent extends DiaryEditEvent { + SatietyEvent(this.selectedElement); final int selectedElement; - ScaleEvent(this.selectedElement); + @override + List get props => [selectedElement]; +} + +class MoodEvent extends DiaryEditEvent { + MoodEvent(this.selectedElement); + final int selectedElement; @override List get props => [selectedElement]; diff --git a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_state.dart b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_state.dart index 91570c27..71ca214f 100644 --- a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_state.dart +++ b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_state.dart @@ -58,9 +58,9 @@ class DiaryEditLoadedState extends DiaryEditState { TextEditingController? productNameController, TextEditingController? productQuantityController, int? selectedMood, - required int selectedSatiety, - required bool wakeupExpanded, - required bool moodExpanded, + int? selectedSatiety, + bool? wakeupExpanded, + bool? moodExpanded, }) { return DiaryEditLoadedState( diaryEditModelObj: diaryEditModelObj ?? this.diaryEditModelObj, @@ -74,9 +74,9 @@ class DiaryEditLoadedState extends DiaryEditState { productQuantityController: productQuantityController ?? this.productQuantityController, selectedMood: selectedMood ?? this.selectedMood, - selectedSatiety: selectedSatiety, - wakeupExpanded: wakeupExpanded, - moodExpanded: moodExpanded); + selectedSatiety: selectedSatiety ?? this.selectedSatiety, + wakeupExpanded: wakeupExpanded ?? this.wakeupExpanded, + moodExpanded: moodExpanded ?? this.moodExpanded); } } diff --git a/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart b/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart index 6924fe91..795a4a63 100644 --- a/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart +++ b/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart @@ -25,24 +25,6 @@ class DiaryEditScreen extends StatelessWidget { "Прием пищи 6", ]; - List satietyList = [ - "Ужасное", - "Плохое", - "Удовлетворительное", - "Нормальное", - "Хорошее", - "Прекрасное", - ]; - - List moodList = [ - "Ужасное", - "Плохое", - "Удовлетворительное", - "Нормальное", - "Хорошее", - "Прекрасное", - ]; - static Widget builder(BuildContext context) { return BlocProvider( create: (context) => @@ -99,7 +81,7 @@ class DiaryEditScreen extends StatelessWidget { //_buildImageStack(context), _buildMeal(context, state), SizedBox(height: 20.v), - _buildMood(moodList, context, state), + _buildMood(context, state), SizedBox(height: 30.v), _buildHeaderColumn(context), SizedBox(height: 30.v), @@ -198,7 +180,6 @@ class DiaryEditScreen extends StatelessWidget { _buildMeal(BuildContext context, DiaryEditLoadedState state) { final digitsOnly = FilteringTextInputFormatter.digitsOnly; - final ScaleBloc scaleBloc = ScaleBloc(); return Padding( padding: EdgeInsets.symmetric(horizontal: 16.h), child: Container( @@ -307,48 +288,48 @@ class DiaryEditScreen extends StatelessWidget { color: Colors.red, isSelected: state.selectedSatiety == 0, onTap: () { - scaleBloc.add(ScaleEvent(0)); + context.read().add(SatietyEvent(0)); }, ), ScaleElement( color: Colors.orange, isSelected: state.selectedSatiety == 1, onTap: () { - scaleBloc.add(ScaleEvent(1)); + context.read().add(SatietyEvent(1)); }, ), ScaleElement( color: Colors.yellow, isSelected: state.selectedSatiety == 2, onTap: () { - scaleBloc.add(ScaleEvent(2)); + context.read().add(SatietyEvent(2)); }, ), ScaleElement( color: Colors.lime, isSelected: state.selectedSatiety == 3, onTap: () { - scaleBloc.add(ScaleEvent(3)); + context.read().add(SatietyEvent(3)); }, ), ScaleElement( color: Colors.lightGreen, isSelected: state.selectedSatiety == 4, onTap: () { - scaleBloc.add(ScaleEvent(4)); + context.read().add(SatietyEvent(4)); }, ), ScaleElement( color: Colors.green, isSelected: state.selectedSatiety == 5, onTap: () { - scaleBloc.add(ScaleEvent(5)); + context.read().add(SatietyEvent(5)); }, ), Text("😊"), // Остальные элементы шкалы ], - ) + ), ], ), )); @@ -793,10 +774,7 @@ class DiaryEditScreen extends StatelessWidget { ); } - Widget _buildMood( - List moodList, BuildContext context, DiaryEditLoadedState state) { - final ScaleBloc scaleBloc = ScaleBloc(); - + Widget _buildMood(BuildContext context, DiaryEditLoadedState state) { return Padding( padding: EdgeInsets.symmetric(horizontal: 16.h), child: Container( @@ -829,7 +807,7 @@ class DiaryEditScreen extends StatelessWidget { "Ваше настроение после приема пищи", style: CustomTextStyles.bodyMediumBlack900, ), - Icon(state.wakeupExpanded + Icon(state.moodExpanded ? Icons.arrow_drop_up : Icons.arrow_drop_down), ], @@ -843,44 +821,32 @@ class DiaryEditScreen extends StatelessWidget { ScaleElement( color: Colors.red, isSelected: state.selectedMood == 0, - onTap: () { - scaleBloc.add(ScaleEvent(0)); - }, + onTap: () {}, ), ScaleElement( color: Colors.orange, isSelected: state.selectedMood == 1, - onTap: () { - scaleBloc.add(ScaleEvent(1)); - }, + onTap: () {}, ), ScaleElement( color: Colors.yellow, isSelected: state.selectedMood == 2, - onTap: () { - scaleBloc.add(ScaleEvent(2)); - }, + onTap: () {}, ), ScaleElement( color: Colors.lime, isSelected: state.selectedMood == 3, - onTap: () { - scaleBloc.add(ScaleEvent(3)); - }, + onTap: () {}, ), ScaleElement( color: Colors.lightGreen, isSelected: state.selectedMood == 4, - onTap: () { - scaleBloc.add(ScaleEvent(4)); - }, + onTap: () {}, ), ScaleElement( color: Colors.green, isSelected: state.selectedMood == 5, - onTap: () { - scaleBloc.add(ScaleEvent(5)); - }, + onTap: () {}, ), Text("😊"), // Остальные элементы шкалы