Make it work with relying the data for 'EnterFoodWidget' onto the InheritedWidget

This commit is contained in:
Marco 2024-09-07 00:02:01 +02:00
parent 3493790598
commit 4afb3ca319
13 changed files with 181 additions and 136 deletions

View File

@ -6,7 +6,7 @@ plugins {
} }
android { android {
namespace = "com.example.calorimeter" namespace = "de.swgross.calorimeter"
compileSdk = flutter.compileSdkVersion compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion ndkVersion = flutter.ndkVersion
@ -21,7 +21,7 @@ android {
defaultConfig { defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "com.example.calorimeter" applicationId = "de.swgross.calorimeter"
// You can update the following values to match your application needs. // You can update the following values to match your application needs.
// For more information, see: https://flutter.dev/to/review-gradle-config. // For more information, see: https://flutter.dev/to/review-gradle-config.
minSdk = flutter.minSdkVersion minSdk = flutter.minSdkVersion

View File

@ -1,6 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA" />
<application <application
android:label="calorimeter" android:label="Calorimeter"
android:name="${applicationName}" android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"> android:icon="@mipmap/ic_launcher">
<activity <activity

View File

@ -1,4 +1,4 @@
package com.example.calorimeter package de.swgross.calorimeter
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity

View File

@ -368,7 +368,7 @@
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
); );
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter; PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;
@ -384,7 +384,7 @@
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests; PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_OPTIMIZATION_LEVEL = "-Onone";
@ -401,7 +401,7 @@
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests; PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Runner"; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Runner";
@ -416,7 +416,7 @@
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests; PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Runner"; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Runner";
@ -547,7 +547,7 @@
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
); );
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter; PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_OPTIMIZATION_LEVEL = "-Onone";
@ -569,7 +569,7 @@
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
); );
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter; PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;

View File

@ -1,7 +1,11 @@
import 'dart:developer';
import 'package:calorimeter/perdate/perdate_widget.dart';
import 'package:calorimeter/storage/storage.dart'; import 'package:calorimeter/storage/storage.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:calorimeter/food_entry/food_entry_bloc.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart';
import 'package:calorimeter/utils/row_with_spacers_widget.dart'; import 'package:calorimeter/utils/row_with_spacers_widget.dart';
import 'package:provider/provider.dart';
class EnterFoodWidget extends StatefulWidget { class EnterFoodWidget extends StatefulWidget {
final Function(BuildContext context, FoodEntry entry) onAdd; final Function(BuildContext context, FoodEntry entry) onAdd;
@ -13,23 +17,35 @@ class EnterFoodWidget extends StatefulWidget {
} }
class _EnterFoodWidgetState extends State<EnterFoodWidget> { class _EnterFoodWidgetState extends State<EnterFoodWidget> {
String perFoodresult = "dings"; late TextEditingController nameController;
TextEditingController nameController = TextEditingController(); late TextEditingController massController;
TextEditingController massController = TextEditingController(); late TextEditingController kcalPerMassController;
TextEditingController kcalPerMassController = TextEditingController(); late Map<String, double> suggestions;
Map<String, double> suggestions = {};
@override @override
void initState() { void initState() {
nameController = TextEditingController();
massController = TextEditingController();
kcalPerMassController = TextEditingController();
suggestions = FoodStorage.getInstance().getFoodEntryLookupDatabase; suggestions = FoodStorage.getInstance().getFoodEntryLookupDatabase;
super.initState(); super.initState();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var nameWidget = Autocomplete<String>( return Consumer<ScannedFood>(
builder: (context, food, child) {
nameController.text = food.name;
kcalPerMassController.text = food.kcalPer100g;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: RowWidget(
Autocomplete<String>(
optionsViewOpenDirection: OptionsViewOpenDirection.down, optionsViewOpenDirection: OptionsViewOpenDirection.down,
fieldViewBuilder: (context, controller, focusNode, onSubmitted) { fieldViewBuilder:
(context, controller, focusNode, onSubmitted) {
nameController = controller; nameController = controller;
return TextFormField( return TextFormField(
controller: controller, controller: controller,
@ -53,51 +69,45 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
); );
}, },
onSelected: (selectedFood) { onSelected: (selectedFood) {
double kcalPerMassForSelectedFood = suggestions[selectedFood]!; double kcalPerMassForSelectedFood =
setState(() { suggestions[selectedFood]!;
kcalPerMassController.text = kcalPerMassForSelectedFood.toString(); context
}); .read<ScannedFood>()
}); .set(selectedFood, kcalPerMassForSelectedFood.toString());
}),
var massWidget = TextField( TextField(
textAlign: TextAlign.end, textAlign: TextAlign.end,
decoration: const InputDecoration( decoration: const InputDecoration(
label: Align(alignment: Alignment.centerRight, child: Text("Menge")), label: Align(
alignment: Alignment.centerRight, child: Text("Menge")),
), ),
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
controller: massController, controller: massController,
onSubmitted: (value) => onSubmitAction(), onSubmitted: (value) => onSubmitAction(),
); ),
TextField(
var kcalPerMassWidget = TextField(
textAlign: TextAlign.end, textAlign: TextAlign.end,
decoration: const InputDecoration( decoration: const InputDecoration(
label: label: Align(
Align(alignment: Alignment.centerRight, child: Text("kcal pro"))), alignment: Alignment.centerRight,
child: Text("kcal pro"))),
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
controller: kcalPerMassController, controller: kcalPerMassController,
onSubmitted: (value) => onSubmitAction(), onSubmitted: (value) => onSubmitAction(),
); ),
Padding(
var enterButton = ElevatedButton( padding: const EdgeInsets.only(left: 16.0),
child: ElevatedButton(
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
), ),
onPressed: () => onSubmitAction(), onPressed: () => onSubmitAction(),
child: const Icon(Icons.add)); child: const Icon(Icons.add)),
return Padding(
padding: const EdgeInsets.only(left: 8.0, right: 4.0),
child: RowWidget(
nameWidget,
massWidget,
kcalPerMassWidget,
Padding(
padding: const EdgeInsets.only(left: 16.0),
child: enterButton,
), ),
), ),
); );
},
);
} }
void onSubmitAction() { void onSubmitAction() {
@ -123,14 +133,6 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
ScaffoldMessenger.of(context).showSnackBar(snackbar); ScaffoldMessenger.of(context).showSnackBar(snackbar);
return; return;
} }
try {
massAsNumber = double.parse(massController.text.replaceAll(",", "."));
} catch (e) {
var snackbar = const SnackBar(content: Text("Menge muss eine Zahl sein"));
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackbar);
return;
}
var entry = FoodEntry( var entry = FoodEntry(
name: nameController.text, name: nameController.text,
@ -138,5 +140,6 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
kcalPerMass: kcalPerMassAsNumber); kcalPerMass: kcalPerMassAsNumber);
widget.onAdd(context, entry); widget.onAdd(context, entry);
context.read<ScannedFood>().set("", "");
} }
} }

View File

@ -15,6 +15,7 @@ class FoodEntryWidget extends StatefulWidget {
class _FoodEntryWidgetState extends State<FoodEntryWidget> { class _FoodEntryWidgetState extends State<FoodEntryWidget> {
late bool showCancelAndDelete; late bool showCancelAndDelete;
@override @override
void initState() { void initState() {
showCancelAndDelete = false; showCancelAndDelete = false;

View File

@ -23,11 +23,13 @@ class FoodFactLookupClient {
class FoodFactModel { class FoodFactModel {
final String name; final String name;
final int kcalPer100g;
FoodFactModel({required this.name}); FoodFactModel({required this.name, required this.kcalPer100g});
factory FoodFactModel.fromJson(Map<String, dynamic> json) { factory FoodFactModel.fromJson(Map<String, dynamic> json) {
log("key product.product_name: ${json['product']['product_name']}"); return FoodFactModel(
return FoodFactModel(name: json['product']['product_name']); name: json['product']['product_name'],
kcalPer100g: json['product']['nutriments']['energy-kcal_100g']);
} }
} }

View File

@ -1,5 +1,6 @@
import 'dart:developer'; import 'dart:developer';
import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:calorimeter/food_scan/food_fact_lookup.dart'; import 'package:calorimeter/food_scan/food_fact_lookup.dart';
import 'package:calorimeter/utils/app_drawer.dart'; import 'package:calorimeter/utils/app_drawer.dart';
import 'package:calorimeter/food_entry/food_entry_bloc.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart';
@ -10,9 +11,9 @@ import 'package:calorimeter/utils/rectangular_notch_shape.dart';
import 'package:calorimeter/utils/sum_widget.dart'; import 'package:calorimeter/utils/sum_widget.dart';
import 'package:calorimeter/utils/theme_switcher_button.dart'; import 'package:calorimeter/utils/theme_switcher_button.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
class PerDateWidget extends StatefulWidget { class PerDateWidget extends StatefulWidget {
final DateTime date; final DateTime date;
@ -48,7 +49,9 @@ class _PerDateWidgetState extends State<PerDateWidget> {
if (snapshot.connectionState != ConnectionState.done) { if (snapshot.connectionState != ConnectionState.done) {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} else { } else {
return BlocProvider( return ChangeNotifierProvider(
create: (context) => ScannedFood(),
child: BlocProvider(
create: (context) => FoodEntryBloc( create: (context) => FoodEntryBloc(
initialState: FoodEntryState(foodEntries: entries), initialState: FoodEntryState(foodEntries: entries),
storage: storage, storage: storage,
@ -70,11 +73,15 @@ class _PerDateWidgetState extends State<PerDateWidget> {
FloatingActionButton( FloatingActionButton(
child: const Icon(Icons.barcode_reader), child: const Icon(Icons.barcode_reader),
onPressed: () async { onPressed: () async {
String ean = await FlutterBarcodeScanner.scanBarcode( var scanResult = await BarcodeScanner.scan();
"#ffffff", "hallo", true, ScanMode.BARCODE); var food = await client
client.retrieveFoodInfo(ean).then((val) { .retrieveFoodInfo(scanResult.rawContent);
log(val.name);
}); if (!context.mounted) return;
context
.read<ScannedFood>()
.set(food.name, food.kcalPer100g.toString());
}, },
), ),
const SizedBox(width: 8), const SizedBox(width: 8),
@ -83,8 +90,20 @@ class _PerDateWidgetState extends State<PerDateWidget> {
floatingActionButtonLocation: floatingActionButtonLocation:
FloatingActionButtonLocation.endDocked); FloatingActionButtonLocation.endDocked);
}), }),
),
); );
} }
}); });
} }
} }
class ScannedFood extends ChangeNotifier {
String name = "";
String kcalPer100g = "";
void set(String newName, String newKcal) {
name = newName;
kcalPer100g = newKcal;
notifyListeners();
}
}

View File

@ -7,7 +7,7 @@ project(runner LANGUAGES CXX)
set(BINARY_NAME "calorimeter") set(BINARY_NAME "calorimeter")
# The unique GTK application identifier for this application. See: # The unique GTK application identifier for this application. See:
# https://wiki.gnome.org/HowDoI/ChooseApplicationID # https://wiki.gnome.org/HowDoI/ChooseApplicationID
set(APPLICATION_ID "com.example.calorimeter") set(APPLICATION_ID "de.swgross.calorimeter")
# Explicitly opt in to modern CMake behaviors to avoid warnings with recent # Explicitly opt in to modern CMake behaviors to avoid warnings with recent
# versions of CMake. # versions of CMake.

View File

@ -385,7 +385,7 @@
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests; PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/calorimeter.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/calorimeter"; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/calorimeter.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/calorimeter";
@ -399,7 +399,7 @@
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests; PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/calorimeter.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/calorimeter"; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/calorimeter.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/calorimeter";
@ -413,7 +413,7 @@
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests; PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/calorimeter.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/calorimeter"; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/calorimeter.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/calorimeter";

View File

@ -8,7 +8,7 @@
PRODUCT_NAME = calorimeter PRODUCT_NAME = calorimeter
// The application's bundle identifier // The application's bundle identifier
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter
// The copyright displayed in application information // The copyright displayed in application information
PRODUCT_COPYRIGHT = Copyright © 2024 com.example. All rights reserved. PRODUCT_COPYRIGHT = Copyright © 2024 com.example. All rights reserved.

View File

@ -9,6 +9,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.11.0" version: "2.11.0"
barcode_scan2:
dependency: "direct main"
description:
name: barcode_scan2
sha256: a2ab566027cd57b2795ea42aa26835dbaa8fe70bcc1aff54942a14d3705dff97
url: "https://pub.dev"
source: hosted
version: "4.3.3"
bloc: bloc:
dependency: transitive dependency: transitive
description: description:
@ -277,8 +285,16 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.8" version: "2.1.8"
provider: protobuf:
dependency: transitive dependency: transitive
description:
name: protobuf
sha256: "68645b24e0716782e58948f8467fd42a880f255096a821f9e7d0ec625b00c84d"
url: "https://pub.dev"
source: hosted
version: "3.1.0"
provider:
dependency: "direct main"
description: description:
name: provider name: provider
sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c

View File

@ -18,6 +18,8 @@ dependencies:
settings_ui: ^2.0.2 settings_ui: ^2.0.2
universal_platform: ^1.1.0 universal_platform: ^1.1.0
uuid: ^4.5.0 uuid: ^4.5.0
barcode_scan2: ^4.3.3
provider: ^6.1.2
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: