mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-28 08:44:06 +02:00
scan products
This commit is contained in:
parent
8f4ca01f77
commit
385d962092
@ -42,4 +42,5 @@
|
|||||||
<data android:mimeType="text/plain"/>
|
<data android:mimeType="text/plain"/>
|
||||||
</intent>
|
</intent>
|
||||||
</queries>
|
</queries>
|
||||||
|
<uses-permission android:name="android.permission.CAMERA"/>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
|
import 'package:barcode_scan2/model/android_options.dart';
|
||||||
|
import 'package:barcode_scan2/model/scan_options.dart';
|
||||||
|
import 'package:barcode_scan2/platform_wrapper.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:mobile/pages/finish_shopping_page.dart';
|
import 'package:mobile/pages/finish_shopping_page.dart';
|
||||||
import 'package:mobile/repos/cart.dart';
|
import 'package:mobile/repos/cart.dart';
|
||||||
|
import 'package:mobile/repos/product.dart';
|
||||||
|
import 'package:mobile/results.dart';
|
||||||
import 'package:mobile/widgets/primary_button.dart';
|
import 'package:mobile/widgets/primary_button.dart';
|
||||||
import 'package:mobile/widgets/primary_card.dart';
|
import 'package:mobile/widgets/primary_card.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
@ -181,7 +186,68 @@ class CartPage extends StatelessWidget {
|
|||||||
child: Container(
|
child: Container(
|
||||||
margin: const EdgeInsets.only(left: 10),
|
margin: const EdgeInsets.only(left: 10),
|
||||||
child: PrimaryButton(
|
child: PrimaryButton(
|
||||||
onPressed: () {}, child: const Text("Skan vare")),
|
onPressed: () async {
|
||||||
|
final result = await BarcodeScanner.scan(
|
||||||
|
options: const ScanOptions(
|
||||||
|
android: AndroidOptions(
|
||||||
|
appBarTitle: "Skan varer"),
|
||||||
|
strings: {
|
||||||
|
"cancel": "Annullér",
|
||||||
|
"flash_on": "Lommelygte til",
|
||||||
|
"flash_off": "Lommelygte fra"
|
||||||
|
}));
|
||||||
|
switch (result.type.name) {
|
||||||
|
case "Cancelled":
|
||||||
|
final snackBar = const SnackBar(
|
||||||
|
content:
|
||||||
|
Text("Skanning af varer annulleret"));
|
||||||
|
if (context.mounted) {
|
||||||
|
ScaffoldMessenger.of(context)
|
||||||
|
.showSnackBar(snackBar);
|
||||||
|
}
|
||||||
|
case "Barcode":
|
||||||
|
if (!context.mounted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final CartRepo cartRepo =
|
||||||
|
context.read<CartRepo>();
|
||||||
|
final ProductRepo productRepo =
|
||||||
|
context.read<ProductRepo>();
|
||||||
|
final productResult = productRepo
|
||||||
|
.productWithBarcode(result.rawContent);
|
||||||
|
switch (productResult) {
|
||||||
|
case Ok<Product, String>():
|
||||||
|
{
|
||||||
|
cartRepo.addToCart(productResult.value);
|
||||||
|
final snackBar = SnackBar(
|
||||||
|
content: Text(
|
||||||
|
"Tilføjet ${productResult.value.name} til indkøbskurven"));
|
||||||
|
ScaffoldMessenger.of(context)
|
||||||
|
.showSnackBar(snackBar);
|
||||||
|
}
|
||||||
|
case Err<Product, String>():
|
||||||
|
final snackBar = const SnackBar(
|
||||||
|
content: Text(
|
||||||
|
"Varen du prøver at tilføje eksistere ikke"));
|
||||||
|
ScaffoldMessenger.of(context)
|
||||||
|
.showSnackBar(snackBar);
|
||||||
|
}
|
||||||
|
|
||||||
|
case "Error":
|
||||||
|
if (!context.mounted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final snackBar = const SnackBar(
|
||||||
|
content:
|
||||||
|
Text("Der skete en fejl, prøv igen"));
|
||||||
|
ScaffoldMessenger.of(context)
|
||||||
|
.showSnackBar(snackBar);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw Exception("Unreachable");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: const Text("Skan vare")),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:mobile/results.dart';
|
||||||
|
|
||||||
class ProductRepo extends ChangeNotifier {
|
class ProductRepo extends ChangeNotifier {
|
||||||
int _nextId = 0;
|
int _nextId = 0;
|
||||||
@ -33,6 +34,15 @@ class ProductRepo extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result<Product, String> productWithBarcode(String barcode) {
|
||||||
|
for (var i = 0; i < products.length; i++) {
|
||||||
|
if (products[i].barcode == barcode) {
|
||||||
|
return Ok(products[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Err("Product with barcode $barcode doesn't exist");
|
||||||
|
}
|
||||||
|
|
||||||
void _addAllProducts() {
|
void _addAllProducts() {
|
||||||
products = [
|
products = [
|
||||||
Product(
|
Product(
|
||||||
@ -76,6 +86,12 @@ class ProductRepo extends ChangeNotifier {
|
|||||||
name: "Jägermeister 750 ml",
|
name: "Jägermeister 750 ml",
|
||||||
price: 60,
|
price: 60,
|
||||||
description: ""),
|
description: ""),
|
||||||
|
Product(
|
||||||
|
id: _nextId++,
|
||||||
|
barcode: "5711953068881",
|
||||||
|
name: "Protein Chokoladedrik",
|
||||||
|
price: 15,
|
||||||
|
description: "Arla's protein chokolade drik der giver store muskler"),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -93,11 +109,13 @@ class Product {
|
|||||||
final String description;
|
final String description;
|
||||||
final int price;
|
final int price;
|
||||||
final Coordinate? location;
|
final Coordinate? location;
|
||||||
|
final String? barcode;
|
||||||
|
|
||||||
Product(
|
Product(
|
||||||
{required this.id,
|
{required this.id,
|
||||||
required this.name,
|
required this.name,
|
||||||
required this.price,
|
required this.price,
|
||||||
required this.description,
|
required this.description,
|
||||||
this.location});
|
this.location,
|
||||||
|
this.barcode});
|
||||||
}
|
}
|
||||||
|
@ -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: ee34fad7148248f4450652909f5188ad95c9b6f512e75088e96bc3e96ff2e9eb
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.4.0"
|
||||||
boolean_selector:
|
boolean_selector:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -57,6 +65,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.1"
|
version: "1.3.1"
|
||||||
|
fixnum:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: fixnum
|
||||||
|
sha256: b6dc7065e46c974bc7c5f143080a6764ec7a4be6da1285ececdc37be96de53be
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.1.1"
|
||||||
flutter:
|
flutter:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description: flutter
|
description: flutter
|
||||||
@ -147,6 +163,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.9.0"
|
version: "1.9.0"
|
||||||
|
protobuf:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: protobuf
|
||||||
|
sha256: "68645b24e0716782e58948f8467fd42a880f255096a821f9e7d0ec625b00c84d"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.1.0"
|
||||||
provider:
|
provider:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
@ -35,6 +35,7 @@ dependencies:
|
|||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
cupertino_icons: ^1.0.8
|
cupertino_icons: ^1.0.8
|
||||||
provider: ^6.1.2
|
provider: ^6.1.2
|
||||||
|
barcode_scan2: ^4.4.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user