calorimeter/lib/utils/scan_food_floating_button.dart

32 lines
910 B
Dart

import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:calorimeter/food_scan/food_fact_lookup.dart';
import 'package:calorimeter/perdate/perdate_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class ScanFoodFloatingButton extends StatelessWidget {
const ScanFoodFloatingButton({
super.key,
});
@override
Widget build(BuildContext context) {
return FloatingActionButton(
heroTag: "scanFoodFAB",
child: const Icon(Icons.barcode_reader),
onPressed: () async {
var client = FoodFactLookupClient();
var scanResult = await BarcodeScanner.scan();
var food = await client.retrieveFoodInfo(scanResult.rawContent);
if (!context.mounted) return;
context
.read<EnterFoodController>()
.set(food.name, food.kcalPer100g.toString());
},
);
}
}