Fix bug that caused infinite loop for selecting FoodEntries

And also, the Cancel button is removed when the FoodEntry is selected.
This commit is contained in:
Marco 2024-09-25 18:20:50 +02:00
parent a70a7fd1e3
commit 37cceb3a9b
3 changed files with 17 additions and 27 deletions

View File

@ -123,15 +123,17 @@ class FoodEntryBloc extends Bloc<FoodEvent, PageState> {
void handleFoodEntryTapped( void handleFoodEntryTapped(
FoodEntryTapped event, Emitter<PageState> emit) async { FoodEntryTapped event, Emitter<PageState> emit) async {
var oldStateOfTappedEntry = event.entry.isSelected;
for (var entry in state.foodEntries) { for (var entry in state.foodEntries) {
entry.isSelected = false; entry.isSelected = false;
} }
var selectedEntry = state.foodEntries.firstWhere((entry) { var selectedEntry = state.foodEntries.firstWhere((entry) {
return entry.id == event.entryID; return entry.id == event.entry.id;
}); });
selectedEntry.selected = !selectedEntry.selected; selectedEntry.isSelected = !oldStateOfTappedEntry;
emit(PageState(foodEntries: state.foodEntries)); emit(PageState(foodEntries: state.foodEntries));
} }
@ -164,9 +166,9 @@ class BarcodeScanned extends FoodEvent {
} }
class FoodEntryTapped extends FoodEvent { class FoodEntryTapped extends FoodEvent {
final String entryID; final FoodEntryState entry;
FoodEntryTapped({required this.entryID}); FoodEntryTapped({required this.entry});
} }
/// This is the state for one date/page /// This is the state for one date/page
@ -223,9 +225,6 @@ class FoodEntryState {
required this.isSelected, required this.isSelected,
}); });
set selected(bool selected) => isSelected = selected;
bool get selected => isSelected;
@override @override
String toString() { String toString() {
//we use quotation marks around the name because the name might contain //we use quotation marks around the name because the name might contain

View File

@ -78,25 +78,6 @@ class _FoodEntryWidgetState extends State<FoodEntryWidget> {
child: Row( child: Row(
mainAxisAlignment: MainAxisAlignment.end, mainAxisAlignment: MainAxisAlignment.end,
children: [ children: [
SizedBox(
child: IconButton(
padding: const EdgeInsets.all(0.0),
icon: const Icon(Icons.cancel),
onPressed: widget.entry.isSelected
? widget.onTap(context, widget.entry)
: null,
),
),
SizedBox(
child: IconButton(
padding: const EdgeInsets.all(0.0),
iconSize: 24,
icon: const Icon(Icons.delete),
color: Colors.redAccent,
onPressed: widget.entry.isSelected
? () => widget.onDelete(context, widget.entry.id)
: null),
),
SizedBox( SizedBox(
child: IconButton( child: IconButton(
padding: const EdgeInsets.all(0.0), padding: const EdgeInsets.all(0.0),
@ -117,6 +98,16 @@ class _FoodEntryWidgetState extends State<FoodEntryWidget> {
} }
: null), : null),
), ),
SizedBox(
child: IconButton(
padding: const EdgeInsets.all(0.0),
iconSize: 24,
icon: const Icon(Icons.delete),
color: Colors.redAccent,
onPressed: widget.entry.isSelected
? () => widget.onDelete(context, widget.entry.id)
: null),
),
], ],
), ),
), ),

View File

@ -52,7 +52,7 @@ class FoodEntryList extends StatelessWidget {
onTap: (_, tappedEntry) { onTap: (_, tappedEntry) {
context context
.read<FoodEntryBloc>() .read<FoodEntryBloc>()
.add(FoodEntryTapped(entryID: tappedEntry.id)); .add(FoodEntryTapped(entry: tappedEntry));
}, },
), ),
const Divider(), const Divider(),