import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:quiver/core.dart'; class FoodEntryBloc extends Bloc { FoodEntryBloc(super.initialState) { on(addFoodEntryToState); } void addFoodEntryToState(FoodEntryEvent event, Emitter emit) { FoodEntryState newState = FoodEntryState.from(state); newState.addEntry(event.entry); emit(newState); } } class FoodEntryEvent { final FoodEntry entry; FoodEntryEvent({required this.entry}); } class FoodEntryState { final List foodEntries; FoodEntryState({required this.foodEntries}); factory FoodEntryState.init() { return FoodEntryState(foodEntries: []); } static from(FoodEntryState state) { List newList = List.from(state.foodEntries); return FoodEntryState(foodEntries: newList); } void addEntry(FoodEntry entry) { foodEntries.add(entry); } @override int get hashCode { return hashObjects(foodEntries); } @override operator ==(other) { if (other.runtimeType != FoodEntryState) return false; other = other as FoodEntryState; if (foodEntries.length != other.foodEntries.length) return false; for (var i = 0; i < foodEntries.length; i += 1) { if (foodEntries[i].name != other.foodEntries[i].name || foodEntries[i].mass != other.foodEntries[i].mass || foodEntries[i].kcalPerMass != other.foodEntries[i].kcalPerMass || foodEntries[i].kcal != other.foodEntries[i].kcal) { return false; } } return true; } } class FoodEntry { final String name; final double mass; final double kcalPerMass; final double kcal; FoodEntry({ required this.name, required this.mass, required this.kcalPerMass, required this.kcal, }); }