wip introduce scanner

This commit is contained in:
Marco 2024-09-06 18:51:24 +02:00
parent 1f06e7b936
commit 3493790598
10 changed files with 93 additions and 11 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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<FoodFactModel> 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<String, dynamic> json) {
log("key product.product_name: ${json['product']['product_name']}");
return FoodFactModel(name: json['product']['product_name']);
}
}

View File

@ -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';

View File

@ -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<PerDateWidget> {
late Future<List<FoodEntry>> entriesFuture;
List<FoodEntry> entries = [];
String formattedDate = '';
FoodFactLookupClient client = FoodFactLookupClient();
@override
void initState() {
@ -56,12 +62,24 @@ class _PerDateWidgetState extends State<PerDateWidget> {
),
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);
}),

View File

@ -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();
}
}

View File

@ -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';

View File

@ -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<FoodEntry> foodEntries;