/* SPDX-License-Identifier: GPL-3.0-or-later */ /* Copyright (C) 2024 Marco Groß */ import 'package:flutter/material.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart'; import 'package:calorimeter/utils/row_with_spacers_widget.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; class EnterFoodWidget extends StatefulWidget { final Function(BuildContext context, FoodEntryState entry) onAdd; final Map foodEntryLookupDatabase; const EnterFoodWidget( {super.key, required this.onAdd, required this.foodEntryLookupDatabase}); @override State createState() => _EnterFoodWidgetState(); } class _EnterFoodWidgetState extends State { late TextEditingController nameController; late TextEditingController massController; late TextEditingController kcalPerMassController; late bool open; @override void initState() { nameController = TextEditingController(); massController = TextEditingController(); kcalPerMassController = TextEditingController(); open = false; super.initState(); } @override Widget build(BuildContext context) { return Column( children: [ Stack( children: [ if (!open) RowWidget( showDividers: false, null, null, null, ElevatedButton( style: ElevatedButton.styleFrom( padding: EdgeInsets.zero, ), onPressed: () => setState(() => open = true), child: Icon(Icons.add)), ), Offstage( offstage: !open, child: AnimatedOpacity( duration: Duration(milliseconds: 250), opacity: open ? 1.0 : 0.0, child: RowWidget( showDividers: true, Autocomplete( optionsViewOpenDirection: OptionsViewOpenDirection.down, fieldViewBuilder: (context, controller, focusNode, onSubmitted) { nameController = controller; return TextFormField( scrollPadding: EdgeInsets.only(bottom: 100), controller: controller, focusNode: focusNode, decoration: InputDecoration( label: Text(AppLocalizations.of(context)!.name), ), ); }, optionsBuilder: (TextEditingValue textEditingValue) { if (textEditingValue.text == '') { return const Iterable.empty(); } return widget.foodEntryLookupDatabase.keys.where( (name) { return name .toLowerCase() .contains(textEditingValue.text.toLowerCase()); }, ); }, onSelected: (selectedFood) { int kcalPerMassForSelectedFood = widget.foodEntryLookupDatabase[selectedFood]!; setState(() { nameController.text = selectedFood; kcalPerMassController.text = kcalPerMassForSelectedFood.toString(); }); }), TextField( scrollPadding: EdgeInsets.only(bottom: 100), textAlign: TextAlign.end, decoration: InputDecoration( label: Directionality( textDirection: TextDirection.rtl, child: Text(AppLocalizations.of(context)!.amount), ), ), keyboardType: TextInputType.number, controller: massController, onSubmitted: (value) => onSubmitAction(), ), TextField( scrollPadding: EdgeInsets.only(bottom: 100), textAlign: TextAlign.end, decoration: InputDecoration( label: Directionality( textDirection: TextDirection.rtl, child: Text(AppLocalizations.of(context)!.kcal))), keyboardType: TextInputType.number, controller: kcalPerMassController, onSubmitted: (value) => onSubmitAction(), ), ElevatedButton( style: ElevatedButton.styleFrom( padding: EdgeInsets.zero, ), onPressed: () => onSubmitAction(), child: const Icon(Icons.check)), ), ), ), ], ), SizedBox( height: 200, child: GestureDetector( onTap: () => setState(() { open = false; }))), ], ); } void onSubmitAction() { int massAsNumber = 0; int kcalPerMassAsNumber = 0; try { massAsNumber = int.parse(massController.text.replaceAll(",", ".")); } catch (e) { var snackbar = SnackBar( content: Text(AppLocalizations.of(context)!.errAmountNotANumber)); ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).showSnackBar(snackbar); return; } try { kcalPerMassAsNumber = int.parse(kcalPerMassController.text.replaceAll(",", ".")); } catch (e) { var snackbar = SnackBar( content: Text(AppLocalizations.of(context)!.errKcalNotANumber)); ScaffoldMessenger.of(context).removeCurrentSnackBar(); ScaffoldMessenger.of(context).showSnackBar(snackbar); return; } var entry = FoodEntryState( name: nameController.text, mass: massAsNumber, kcalPer100: kcalPerMassAsNumber, waitingForNetwork: false, isSelected: false, ); widget.onAdd(context, entry); setState(() { nameController.text = ""; massController.text = ""; kcalPerMassController.text = ""; open = false; }); } }