From 34937905982c74f97dceb642b9858e3c041c2e16 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 6 Sep 2024 18:51:24 +0200 Subject: [PATCH] wip introduce scanner --- lib/{ => food_entry}/enter_food_widget.dart | 2 +- lib/food_entry/food_entry_widget.dart | 2 +- lib/food_scan/food_fact_lookup.dart | 33 ++++++++++++++++++++ lib/perdate/entry_list.dart | 2 +- lib/perdate/perdate_widget.dart | 30 ++++++++++++++---- lib/{ => utils}/app_drawer.dart | 0 lib/utils/rectangular_notch_shape.dart | 31 ++++++++++++++++++ lib/{ => utils}/row_with_spacers_widget.dart | 0 lib/utils/settings.dart | 2 +- lib/utils/sum_widget.dart | 2 +- 10 files changed, 93 insertions(+), 11 deletions(-) rename lib/{ => food_entry}/enter_food_widget.dart (98%) create mode 100644 lib/food_scan/food_fact_lookup.dart rename lib/{ => utils}/app_drawer.dart (100%) create mode 100644 lib/utils/rectangular_notch_shape.dart rename lib/{ => utils}/row_with_spacers_widget.dart (100%) diff --git a/lib/enter_food_widget.dart b/lib/food_entry/enter_food_widget.dart similarity index 98% rename from lib/enter_food_widget.dart rename to lib/food_entry/enter_food_widget.dart index e0ed597..5360c42 100644 --- a/lib/enter_food_widget.dart +++ b/lib/food_entry/enter_food_widget.dart @@ -1,7 +1,7 @@ import 'package:calorimeter/storage/storage.dart'; import 'package:flutter/material.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart'; -import 'package:calorimeter/row_with_spacers_widget.dart'; +import 'package:calorimeter/utils/row_with_spacers_widget.dart'; class EnterFoodWidget extends StatefulWidget { final Function(BuildContext context, FoodEntry entry) onAdd; diff --git a/lib/food_entry/food_entry_widget.dart b/lib/food_entry/food_entry_widget.dart index 28dc931..88e64b8 100644 --- a/lib/food_entry/food_entry_widget.dart +++ b/lib/food_entry/food_entry_widget.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart'; -import 'package:calorimeter/row_with_spacers_widget.dart'; +import 'package:calorimeter/utils/row_with_spacers_widget.dart'; class FoodEntryWidget extends StatefulWidget { final FoodEntry entry; diff --git a/lib/food_scan/food_fact_lookup.dart b/lib/food_scan/food_fact_lookup.dart new file mode 100644 index 0000000..db4e0b2 --- /dev/null +++ b/lib/food_scan/food_fact_lookup.dart @@ -0,0 +1,33 @@ +import 'dart:convert'; +import 'dart:developer'; +import 'dart:io'; + +class FoodFactLookupClient { + FoodFactLookupClient(); + + static const String host = "world.openfoodfacts.org"; + static const String url = "https://world.openfoodfacts.org/api/v3/product/"; + + String getProductUrl(String ean) { + return "$url$ean.json"; + } + + Future retrieveFoodInfo(String ean) async { + HttpClient client = HttpClient(); + var request = await client.getUrl(Uri.parse(getProductUrl(ean))); + var response = await request.close(); + var asString = await response.transform(utf8.decoder).join(); + return FoodFactModel.fromJson(jsonDecode(asString)); + } +} + +class FoodFactModel { + final String name; + + FoodFactModel({required this.name}); + + factory FoodFactModel.fromJson(Map json) { + log("key product.product_name: ${json['product']['product_name']}"); + return FoodFactModel(name: json['product']['product_name']); + } +} diff --git a/lib/perdate/entry_list.dart b/lib/perdate/entry_list.dart index bb23ba2..756f9fd 100644 --- a/lib/perdate/entry_list.dart +++ b/lib/perdate/entry_list.dart @@ -1,4 +1,4 @@ -import 'package:calorimeter/enter_food_widget.dart'; +import 'package:calorimeter/food_entry/enter_food_widget.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart'; import 'package:calorimeter/food_entry/food_entry_widget.dart'; import 'package:flutter/material.dart'; diff --git a/lib/perdate/perdate_widget.dart b/lib/perdate/perdate_widget.dart index dbb54c6..b5a7d45 100644 --- a/lib/perdate/perdate_widget.dart +++ b/lib/perdate/perdate_widget.dart @@ -1,11 +1,16 @@ -import 'package:calorimeter/app_drawer.dart'; -import 'package:calorimeter/utils/calendar_floating_button.dart'; -import 'package:calorimeter/perdate/entry_list.dart'; +import 'dart:developer'; + +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'; +import 'package:calorimeter/perdate/entry_list.dart'; import 'package:calorimeter/storage/storage.dart'; +import 'package:calorimeter/utils/calendar_floating_button.dart'; +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'; @@ -22,6 +27,7 @@ class _PerDateWidgetState extends State { late Future> entriesFuture; List entries = []; String formattedDate = ''; + FoodFactLookupClient client = FoodFactLookupClient(); @override void initState() { @@ -56,12 +62,24 @@ class _PerDateWidgetState extends State { ), body: FoodEntryList(entries: state.foodEntries), bottomNavigationBar: BottomAppBar( - shape: const CircularNotchedRectangle(), + shape: const RectangularNotchShape(), color: Theme.of(context).colorScheme.secondary, child: SumWidget(foodEntries: state.foodEntries)), drawer: const AppDrawer(), - floatingActionButton: - CalendarFloatingButton(date: widget.date), + floatingActionButton: OverflowBar(children: [ + 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); + }); + }, + ), + const SizedBox(width: 8), + CalendarFloatingButton(date: widget.date), + ]), floatingActionButtonLocation: FloatingActionButtonLocation.endDocked); }), diff --git a/lib/app_drawer.dart b/lib/utils/app_drawer.dart similarity index 100% rename from lib/app_drawer.dart rename to lib/utils/app_drawer.dart diff --git a/lib/utils/rectangular_notch_shape.dart b/lib/utils/rectangular_notch_shape.dart new file mode 100644 index 0000000..ef9f0a3 --- /dev/null +++ b/lib/utils/rectangular_notch_shape.dart @@ -0,0 +1,31 @@ +import 'package:flutter/material.dart'; + +class RectangularNotchShape extends NotchedShape { + const RectangularNotchShape(); + + @override + Path getOuterPath(Rect host, Rect? guest) { + if (guest == null || !host.overlaps(guest)) { + return Path()..addRect(host); + } + + var margin = 20; + + return Path() + ..moveTo(host.left, host.top) + ..lineTo(guest.left - margin, host.top) + ..quadraticBezierTo(guest.left, host.top, guest.left, host.top + margin) + ..lineTo(guest.left, guest.bottom - margin) + ..quadraticBezierTo( + guest.left, guest.bottom, guest.left + margin, guest.bottom) + ..lineTo(guest.right - margin, guest.bottom) + ..quadraticBezierTo( + guest.right, guest.bottom, guest.right, guest.bottom - margin) + ..lineTo(guest.right, host.top + margin) + ..quadraticBezierTo(guest.right, host.top, guest.right + margin, host.top) + ..lineTo(host.right, host.top) + ..lineTo(host.right, host.bottom) + ..lineTo(host.left, host.bottom) + ..close(); + } +} diff --git a/lib/row_with_spacers_widget.dart b/lib/utils/row_with_spacers_widget.dart similarity index 100% rename from lib/row_with_spacers_widget.dart rename to lib/utils/row_with_spacers_widget.dart diff --git a/lib/utils/settings.dart b/lib/utils/settings.dart index 09ae3b4..4409f6b 100644 --- a/lib/utils/settings.dart +++ b/lib/utils/settings.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.dart'; -import 'package:calorimeter/app_drawer.dart'; +import 'package:calorimeter/utils/app_drawer.dart'; import 'package:calorimeter/utils/calendar_floating_button.dart'; import 'package:calorimeter/utils/settings_bloc.dart'; import 'package:settings_ui/settings_ui.dart'; diff --git a/lib/utils/sum_widget.dart b/lib/utils/sum_widget.dart index baf3d64..668c03a 100644 --- a/lib/utils/sum_widget.dart +++ b/lib/utils/sum_widget.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart'; import 'package:calorimeter/utils/settings_bloc.dart'; -import 'package:calorimeter/row_with_spacers_widget.dart'; +import 'package:calorimeter/utils/row_with_spacers_widget.dart'; class SumWidget extends StatefulWidget { final List foodEntries;