import 'package:flutter/material.dart'; import 'package:calodiary/food_entry_bloc.dart'; import 'package:calodiary/row_with_spacers_widget.dart'; class FoodEntryWidget extends StatefulWidget { final FoodEntry entry; final Function(BuildContext context, String id) onDelete; const FoodEntryWidget( {super.key, required this.entry, required this.onDelete}); @override State createState() => _FoodEntryWidgetState(); } class _FoodEntryWidgetState extends State { late bool showCancelAndDelete; @override void initState() { showCancelAndDelete = false; super.initState(); } @override Widget build(BuildContext context) { return GestureDetector( onTap: () { setState(() { showCancelAndDelete = !showCancelAndDelete; }); }, child: Stack( children: [ Positioned.fill( child: Stack(children: [ Positioned.fill( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 8.0), child: RowWidget( Text(widget.entry.name), Text(widget.entry.mass.ceil().toString(), textAlign: TextAlign.end), Text(widget.entry.kcalPerMass.ceil().toString(), textAlign: TextAlign.end), Opacity( opacity: showCancelAndDelete ? 0.0 : 1.0, child: Text( (widget.entry.mass * widget.entry.kcalPerMass / 100) .ceil() .toString(), textAlign: TextAlign.end), ), ), ), ), Opacity( opacity: showCancelAndDelete ? 0.66 : 0.0, child: Container( color: Theme.of(context).colorScheme.secondary)), ]), ), Opacity( opacity: showCancelAndDelete ? 1.0 : 0.0, child: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ SizedBox( child: IconButton( padding: EdgeInsets.all(0.0), icon: const Icon(Icons.cancel), onPressed: showCancelAndDelete ? () => setState(() { showCancelAndDelete = false; }) : null, ), ), SizedBox( child: IconButton( padding: EdgeInsets.all(0.0), iconSize: 24, icon: const Icon(Icons.delete), color: Colors.redAccent, onPressed: showCancelAndDelete ? () => widget.onDelete(context, widget.entry.id) : null), ), ], ), ), ], ), ); } }