102 lines
2.7 KiB
Dart
102 lines
2.7 KiB
Dart
/// Confirmed products stored on device.
|
|
library;
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'models/product.dart';
|
|
import 'services/product_repository.dart';
|
|
import 'utils/money.dart';
|
|
|
|
class ProductsScreen extends StatefulWidget {
|
|
const ProductsScreen({
|
|
super.key,
|
|
required this.repository,
|
|
this.savedCount,
|
|
});
|
|
|
|
final ProductRepository repository;
|
|
final int? savedCount;
|
|
|
|
@override
|
|
State<ProductsScreen> createState() => _ProductsScreenState();
|
|
}
|
|
|
|
class _ProductsScreenState extends State<ProductsScreen> {
|
|
late Future<List<Product>> _future;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_future = widget.repository.getAll();
|
|
final saved = widget.savedCount;
|
|
if (saved != null) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
saved == 1
|
|
? 'Saved 1 product.'
|
|
: 'Saved $saved products.',
|
|
),
|
|
),
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Products')),
|
|
body: FutureBuilder<List<Product>>(
|
|
future: _future,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasError) {
|
|
return Center(
|
|
child: Text(
|
|
'Could not load products.',
|
|
style: theme.textTheme.bodyLarge,
|
|
),
|
|
);
|
|
}
|
|
if (!snapshot.hasData) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
final products = snapshot.data!;
|
|
if (products.isEmpty) {
|
|
return Center(
|
|
child: Text(
|
|
'Confirmed receipt items will show up here.',
|
|
textAlign: TextAlign.center,
|
|
style: theme.textTheme.bodyLarge,
|
|
),
|
|
);
|
|
}
|
|
|
|
return ListView.separated(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
itemCount: products.length,
|
|
separatorBuilder: (_, _) => const Divider(indent: 16, endIndent: 16),
|
|
itemBuilder: (context, index) {
|
|
final product = products[index];
|
|
final seen = product.timesSeen;
|
|
return ListTile(
|
|
title: Text(product.name),
|
|
subtitle: Text(seen == 1 ? 'Seen once' : 'Seen $seen times'),
|
|
trailing: Text(
|
|
formatEuro(product.lastPrice),
|
|
style: theme.textTheme.titleMedium,
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|