Make it work with relying the data for 'EnterFoodWidget' onto the InheritedWidget
This commit is contained in:
parent
3493790598
commit
4afb3ca319
@ -6,7 +6,7 @@ plugins {
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "com.example.calorimeter"
|
||||
namespace = "de.swgross.calorimeter"
|
||||
compileSdk = flutter.compileSdkVersion
|
||||
ndkVersion = flutter.ndkVersion
|
||||
|
||||
@ -21,7 +21,7 @@ android {
|
||||
|
||||
defaultConfig {
|
||||
// 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.
|
||||
// For more information, see: https://flutter.dev/to/review-gradle-config.
|
||||
minSdk = flutter.minSdkVersion
|
||||
|
@ -1,6 +1,8 @@
|
||||
<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
|
||||
android:label="calorimeter"
|
||||
android:label="Calorimeter"
|
||||
android:name="${applicationName}"
|
||||
android:icon="@mipmap/ic_launcher">
|
||||
<activity
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.example.calorimeter
|
||||
package de.swgross.calorimeter
|
||||
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
|
||||
|
@ -368,7 +368,7 @@
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
);
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
|
||||
SWIFT_VERSION = 5.0;
|
||||
@ -384,7 +384,7 @@
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
MARKETING_VERSION = 1.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
@ -401,7 +401,7 @@
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
MARKETING_VERSION = 1.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 5.0;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Runner";
|
||||
@ -416,7 +416,7 @@
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
MARKETING_VERSION = 1.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 5.0;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Runner";
|
||||
@ -547,7 +547,7 @@
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
);
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
@ -569,7 +569,7 @@
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
);
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
|
||||
SWIFT_VERSION = 5.0;
|
||||
|
@ -1,7 +1,11 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:calorimeter/perdate/perdate_widget.dart';
|
||||
import 'package:calorimeter/storage/storage.dart';
|
||||
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:provider/provider.dart';
|
||||
|
||||
class EnterFoodWidget extends StatefulWidget {
|
||||
final Function(BuildContext context, FoodEntry entry) onAdd;
|
||||
@ -13,23 +17,35 @@ class EnterFoodWidget extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
||||
String perFoodresult = "dings";
|
||||
TextEditingController nameController = TextEditingController();
|
||||
TextEditingController massController = TextEditingController();
|
||||
TextEditingController kcalPerMassController = TextEditingController();
|
||||
Map<String, double> suggestions = {};
|
||||
late TextEditingController nameController;
|
||||
late TextEditingController massController;
|
||||
late TextEditingController kcalPerMassController;
|
||||
late Map<String, double> suggestions;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
nameController = TextEditingController();
|
||||
massController = TextEditingController();
|
||||
kcalPerMassController = TextEditingController();
|
||||
suggestions = FoodStorage.getInstance().getFoodEntryLookupDatabase;
|
||||
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
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,
|
||||
fieldViewBuilder: (context, controller, focusNode, onSubmitted) {
|
||||
fieldViewBuilder:
|
||||
(context, controller, focusNode, onSubmitted) {
|
||||
nameController = controller;
|
||||
return TextFormField(
|
||||
controller: controller,
|
||||
@ -53,51 +69,45 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
||||
);
|
||||
},
|
||||
onSelected: (selectedFood) {
|
||||
double kcalPerMassForSelectedFood = suggestions[selectedFood]!;
|
||||
setState(() {
|
||||
kcalPerMassController.text = kcalPerMassForSelectedFood.toString();
|
||||
});
|
||||
});
|
||||
|
||||
var massWidget = TextField(
|
||||
double kcalPerMassForSelectedFood =
|
||||
suggestions[selectedFood]!;
|
||||
context
|
||||
.read<ScannedFood>()
|
||||
.set(selectedFood, kcalPerMassForSelectedFood.toString());
|
||||
}),
|
||||
TextField(
|
||||
textAlign: TextAlign.end,
|
||||
decoration: const InputDecoration(
|
||||
label: Align(alignment: Alignment.centerRight, child: Text("Menge")),
|
||||
label: Align(
|
||||
alignment: Alignment.centerRight, child: Text("Menge")),
|
||||
),
|
||||
keyboardType: TextInputType.number,
|
||||
controller: massController,
|
||||
onSubmitted: (value) => onSubmitAction(),
|
||||
);
|
||||
|
||||
var kcalPerMassWidget = TextField(
|
||||
),
|
||||
TextField(
|
||||
textAlign: TextAlign.end,
|
||||
decoration: const InputDecoration(
|
||||
label:
|
||||
Align(alignment: Alignment.centerRight, child: Text("kcal pro"))),
|
||||
label: Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Text("kcal pro"))),
|
||||
keyboardType: TextInputType.number,
|
||||
controller: kcalPerMassController,
|
||||
onSubmitted: (value) => onSubmitAction(),
|
||||
);
|
||||
|
||||
var enterButton = ElevatedButton(
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 16.0),
|
||||
child: ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: EdgeInsets.zero,
|
||||
),
|
||||
onPressed: () => onSubmitAction(),
|
||||
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,
|
||||
child: const Icon(Icons.add)),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void onSubmitAction() {
|
||||
@ -123,14 +133,6 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
||||
ScaffoldMessenger.of(context).showSnackBar(snackbar);
|
||||
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(
|
||||
name: nameController.text,
|
||||
@ -138,5 +140,6 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
||||
kcalPerMass: kcalPerMassAsNumber);
|
||||
|
||||
widget.onAdd(context, entry);
|
||||
context.read<ScannedFood>().set("", "");
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ class FoodEntryWidget extends StatefulWidget {
|
||||
|
||||
class _FoodEntryWidgetState extends State<FoodEntryWidget> {
|
||||
late bool showCancelAndDelete;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
showCancelAndDelete = false;
|
||||
|
@ -23,11 +23,13 @@ class FoodFactLookupClient {
|
||||
|
||||
class FoodFactModel {
|
||||
final String name;
|
||||
final int kcalPer100g;
|
||||
|
||||
FoodFactModel({required this.name});
|
||||
FoodFactModel({required this.name, required this.kcalPer100g});
|
||||
|
||||
factory FoodFactModel.fromJson(Map<String, dynamic> json) {
|
||||
log("key product.product_name: ${json['product']['product_name']}");
|
||||
return FoodFactModel(name: json['product']['product_name']);
|
||||
return FoodFactModel(
|
||||
name: json['product']['product_name'],
|
||||
kcalPer100g: json['product']['nutriments']['energy-kcal_100g']);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:barcode_scan2/barcode_scan2.dart';
|
||||
import 'package:calorimeter/food_scan/food_fact_lookup.dart';
|
||||
import 'package:calorimeter/utils/app_drawer.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/theme_switcher_button.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class PerDateWidget extends StatefulWidget {
|
||||
final DateTime date;
|
||||
@ -48,7 +49,9 @@ class _PerDateWidgetState extends State<PerDateWidget> {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
} else {
|
||||
return BlocProvider(
|
||||
return ChangeNotifierProvider(
|
||||
create: (context) => ScannedFood(),
|
||||
child: BlocProvider(
|
||||
create: (context) => FoodEntryBloc(
|
||||
initialState: FoodEntryState(foodEntries: entries),
|
||||
storage: storage,
|
||||
@ -70,11 +73,15 @@ class _PerDateWidgetState extends State<PerDateWidget> {
|
||||
FloatingActionButton(
|
||||
child: const Icon(Icons.barcode_reader),
|
||||
onPressed: () async {
|
||||
String ean = await FlutterBarcodeScanner.scanBarcode(
|
||||
"#ffffff", "hallo", true, ScanMode.BARCODE);
|
||||
client.retrieveFoodInfo(ean).then((val) {
|
||||
log(val.name);
|
||||
});
|
||||
var scanResult = await BarcodeScanner.scan();
|
||||
var food = await client
|
||||
.retrieveFoodInfo(scanResult.rawContent);
|
||||
|
||||
if (!context.mounted) return;
|
||||
|
||||
context
|
||||
.read<ScannedFood>()
|
||||
.set(food.name, food.kcalPer100g.toString());
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
@ -83,8 +90,20 @@ class _PerDateWidgetState extends State<PerDateWidget> {
|
||||
floatingActionButtonLocation:
|
||||
FloatingActionButtonLocation.endDocked);
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class ScannedFood extends ChangeNotifier {
|
||||
String name = "";
|
||||
String kcalPer100g = "";
|
||||
|
||||
void set(String newName, String newKcal) {
|
||||
name = newName;
|
||||
kcalPer100g = newKcal;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ project(runner LANGUAGES CXX)
|
||||
set(BINARY_NAME "calorimeter")
|
||||
# The unique GTK application identifier for this application. See:
|
||||
# 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
|
||||
# versions of CMake.
|
||||
|
@ -385,7 +385,7 @@
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
MARKETING_VERSION = 1.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 5.0;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/calorimeter.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/calorimeter";
|
||||
@ -399,7 +399,7 @@
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
MARKETING_VERSION = 1.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 5.0;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/calorimeter.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/calorimeter";
|
||||
@ -413,7 +413,7 @@
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
MARKETING_VERSION = 1.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter.RunnerTests;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter.RunnerTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 5.0;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/calorimeter.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/calorimeter";
|
||||
|
@ -8,7 +8,7 @@
|
||||
PRODUCT_NAME = calorimeter
|
||||
|
||||
// The application's bundle identifier
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.example.calorimeter
|
||||
PRODUCT_BUNDLE_IDENTIFIER = de.swgross.calorimeter
|
||||
|
||||
// The copyright displayed in application information
|
||||
PRODUCT_COPYRIGHT = Copyright © 2024 com.example. All rights reserved.
|
||||
|
18
pubspec.lock
18
pubspec.lock
@ -9,6 +9,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
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:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -277,8 +285,16 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.1.8"
|
||||
provider:
|
||||
protobuf:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: protobuf
|
||||
sha256: "68645b24e0716782e58948f8467fd42a880f255096a821f9e7d0ec625b00c84d"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.1.0"
|
||||
provider:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: provider
|
||||
sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c
|
||||
|
@ -18,6 +18,8 @@ dependencies:
|
||||
settings_ui: ^2.0.2
|
||||
universal_platform: ^1.1.0
|
||||
uuid: ^4.5.0
|
||||
barcode_scan2: ^4.3.3
|
||||
provider: ^6.1.2
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
Loading…
Reference in New Issue
Block a user