init
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:kooltab2/theme.dart';
|
||||
|
||||
import 'router.dart';
|
||||
|
||||
class KoolTabApp extends StatelessWidget {
|
||||
const KoolTabApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp.router(
|
||||
title: 'KoolTab',
|
||||
debugShowCheckedModeBanner: false,
|
||||
routerConfig: appRouter,
|
||||
// theme: ThemeData(
|
||||
// useMaterial3: false,
|
||||
// colorSchemeSeed: Colors.redAccent,
|
||||
// brightness: Brightness.dark,
|
||||
// ),
|
||||
theme: neoBrutalDarkTheme
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../viewmodels/bar_screen_view_model.dart';
|
||||
import '../viewmodels/product_list_view_model.dart';
|
||||
|
||||
class AppBootstrap extends StatefulWidget {
|
||||
final Widget child;
|
||||
|
||||
const AppBootstrap({
|
||||
super.key,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
@override
|
||||
State<AppBootstrap> createState() => _AppBootstrapState();
|
||||
}
|
||||
|
||||
class _AppBootstrapState extends State<AppBootstrap> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!mounted) return;
|
||||
|
||||
context.read<ProductListViewModel>().ensureLoaded();
|
||||
context.read<BarScreenViewModel>().ensureLoaded();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return widget.child;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../views/bar_screen_view.dart';
|
||||
import '../views/product_form_view.dart';
|
||||
import '../views/product_list_view.dart';
|
||||
|
||||
final appRouter = GoRouter(
|
||||
initialLocation: '/bar',
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/bar',
|
||||
builder: (context, state) => const BarScreenView(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/products',
|
||||
builder: (context, state) => const ProductListView(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: 'new',
|
||||
builder: (context, state) => const ProductFormView(),
|
||||
),
|
||||
GoRoute(
|
||||
path: ':id/edit',
|
||||
builder: (context, state) {
|
||||
final productId = state.pathParameters['id']!;
|
||||
|
||||
return ProductFormView(productId: productId);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
@@ -0,0 +1,112 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift_flutter/drift_flutter.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
part 'app_database.g.dart';
|
||||
|
||||
@DataClassName('ProductRow')
|
||||
class Products extends Table {
|
||||
TextColumn get id => text()();
|
||||
|
||||
TextColumn get name => text()();
|
||||
|
||||
TextColumn get category => text()();
|
||||
|
||||
IntColumn get stockQuantity => integer()();
|
||||
|
||||
IntColumn get lowStockThreshold => integer()();
|
||||
|
||||
IntColumn get priceInCents => integer()();
|
||||
|
||||
TextColumn get imagePath => text().nullable()();
|
||||
|
||||
BoolColumn get active => boolean().withDefault(const Constant(true))();
|
||||
|
||||
@override
|
||||
Set<Column> get primaryKey => {id};
|
||||
}
|
||||
|
||||
@DataClassName('BarTabRow')
|
||||
class BarTabs extends Table {
|
||||
TextColumn get id => text()();
|
||||
|
||||
TextColumn get customerName => text()();
|
||||
|
||||
TextColumn get status => text().withDefault(const Constant('open'))();
|
||||
|
||||
DateTimeColumn get openedAt => dateTime()();
|
||||
|
||||
DateTimeColumn get closedAt => dateTime().nullable()();
|
||||
|
||||
@override
|
||||
Set<Column> get primaryKey => {id};
|
||||
}
|
||||
|
||||
@DataClassName('TabItemRow')
|
||||
class TabItems extends Table {
|
||||
TextColumn get id => text()();
|
||||
|
||||
TextColumn get tabId => text().references(
|
||||
BarTabs,
|
||||
#id,
|
||||
onDelete: KeyAction.cascade,
|
||||
)();
|
||||
|
||||
TextColumn get productId => text().references(
|
||||
Products,
|
||||
#id,
|
||||
)();
|
||||
|
||||
TextColumn get productName => text()();
|
||||
|
||||
IntColumn get quantity => integer()();
|
||||
|
||||
IntColumn get unitPriceInCents => integer()();
|
||||
|
||||
DateTimeColumn get createdAt => dateTime()();
|
||||
|
||||
@override
|
||||
Set<Column> get primaryKey => {id};
|
||||
}
|
||||
|
||||
@DriftDatabase(
|
||||
tables: [
|
||||
Products,
|
||||
BarTabs,
|
||||
TabItems,
|
||||
],
|
||||
)
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
AppDatabase([QueryExecutor? executor]) : super(executor ?? _openConnection());
|
||||
|
||||
@override
|
||||
int get schemaVersion => 3;
|
||||
|
||||
@override
|
||||
MigrationStrategy get migration {
|
||||
return MigrationStrategy(
|
||||
onCreate: (migrator) async {
|
||||
await migrator.createAll();
|
||||
},
|
||||
onUpgrade: (migrator, from, to) async {
|
||||
if (from < 2) {
|
||||
await migrator.createTable(barTabs);
|
||||
await migrator.createTable(tabItems);
|
||||
}
|
||||
|
||||
if (from < 3) {
|
||||
await migrator.addColumn(products, products.imagePath);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static QueryExecutor _openConnection() {
|
||||
return driftDatabase(
|
||||
name: 'kooltab',
|
||||
native: const DriftNativeOptions(
|
||||
databaseDirectory: getApplicationSupportDirectory,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,54 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'app/app.dart';
|
||||
import 'app/app_bootstrap.dart';
|
||||
import 'database/app_database.dart';
|
||||
import 'services/bar_tab_service.dart';
|
||||
import 'services/product_service.dart';
|
||||
import 'viewmodels/bar_screen_view_model.dart';
|
||||
import 'viewmodels/product_list_view_model.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
await SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.landscapeLeft,
|
||||
DeviceOrientation.landscapeRight,
|
||||
]);
|
||||
|
||||
runApp(
|
||||
MultiProvider(
|
||||
providers: [
|
||||
Provider<AppDatabase>(
|
||||
create: (_) => AppDatabase(),
|
||||
dispose: (_, database) => database.close(),
|
||||
),
|
||||
Provider<ProductService>(
|
||||
create: (context) => DriftProductService(
|
||||
database: context.read<AppDatabase>(),
|
||||
),
|
||||
),
|
||||
Provider<BarTabService>(
|
||||
create: (context) => DriftBarTabService(
|
||||
database: context.read<AppDatabase>(),
|
||||
),
|
||||
),
|
||||
ChangeNotifierProvider<ProductListViewModel>(
|
||||
create: (context) => ProductListViewModel(
|
||||
productService: context.read<ProductService>(),
|
||||
),
|
||||
),
|
||||
ChangeNotifierProvider<BarScreenViewModel>(
|
||||
create: (context) => BarScreenViewModel(
|
||||
barTabService: context.read<BarTabService>(),
|
||||
),
|
||||
),
|
||||
],
|
||||
child: const AppBootstrap(
|
||||
child: KoolTabApp(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import 'tab_item.dart';
|
||||
|
||||
class BarTab {
|
||||
final String id;
|
||||
final String customerName;
|
||||
final String status;
|
||||
final DateTime openedAt;
|
||||
final DateTime? closedAt;
|
||||
final List<TabItem> items;
|
||||
|
||||
const BarTab({
|
||||
required this.id,
|
||||
required this.customerName,
|
||||
required this.status,
|
||||
required this.openedAt,
|
||||
required this.items,
|
||||
this.closedAt,
|
||||
});
|
||||
|
||||
int get totalInCents {
|
||||
return items.fold<int>(
|
||||
0,
|
||||
(total, item) => total + item.lineTotalInCents,
|
||||
);
|
||||
}
|
||||
|
||||
int get itemCount {
|
||||
return items.fold<int>(
|
||||
0,
|
||||
(total, item) => total + item.quantity,
|
||||
);
|
||||
}
|
||||
|
||||
String get formattedTotal {
|
||||
return '€${(totalInCents / 100).toStringAsFixed(2)}';
|
||||
}
|
||||
|
||||
bool get isOpen => status == 'open';
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
class Product {
|
||||
final String id;
|
||||
final String name;
|
||||
final String category;
|
||||
final int stockQuantity;
|
||||
final int lowStockThreshold;
|
||||
final int priceInCents;
|
||||
final String? imagePath;
|
||||
final bool active;
|
||||
|
||||
const Product({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.category,
|
||||
required this.stockQuantity,
|
||||
required this.lowStockThreshold,
|
||||
required this.priceInCents,
|
||||
this.imagePath,
|
||||
this.active = true,
|
||||
});
|
||||
|
||||
bool get isLowStock => stockQuantity <= lowStockThreshold;
|
||||
|
||||
String get formattedPrice {
|
||||
final euros = priceInCents / 100;
|
||||
|
||||
return '€${euros.toStringAsFixed(2)}';
|
||||
}
|
||||
|
||||
Product copyWith({
|
||||
String? id,
|
||||
String? name,
|
||||
String? category,
|
||||
int? stockQuantity,
|
||||
int? lowStockThreshold,
|
||||
int? priceInCents,
|
||||
String? imagePath,
|
||||
bool? active,
|
||||
}) {
|
||||
return Product(
|
||||
id: id ?? this.id,
|
||||
name: name ?? this.name,
|
||||
category: category ?? this.category,
|
||||
stockQuantity: stockQuantity ?? this.stockQuantity,
|
||||
lowStockThreshold: lowStockThreshold ?? this.lowStockThreshold,
|
||||
priceInCents: priceInCents ?? this.priceInCents,
|
||||
imagePath: imagePath ?? this.imagePath,
|
||||
active: active ?? this.active,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
class TabItem {
|
||||
final String id;
|
||||
final String tabId;
|
||||
final String productId;
|
||||
final String productName;
|
||||
final int quantity;
|
||||
final int unitPriceInCents;
|
||||
|
||||
const TabItem({
|
||||
required this.id,
|
||||
required this.tabId,
|
||||
required this.productId,
|
||||
required this.productName,
|
||||
required this.quantity,
|
||||
required this.unitPriceInCents,
|
||||
});
|
||||
|
||||
int get lineTotalInCents => quantity * unitPriceInCents;
|
||||
|
||||
String get formattedLineTotal {
|
||||
return '€${(lineTotalInCents / 100).toStringAsFixed(2)}';
|
||||
}
|
||||
|
||||
String get formattedUnitPrice {
|
||||
return '€${(unitPriceInCents / 100).toStringAsFixed(2)}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../database/app_database.dart';
|
||||
import '../models/bar_tab.dart';
|
||||
import '../models/product.dart';
|
||||
import '../models/tab_item.dart';
|
||||
|
||||
abstract class BarTabService {
|
||||
Future<List<BarTab>> getOpenTabs();
|
||||
|
||||
Future<BarTab?> getTabById(String id);
|
||||
|
||||
Future<BarTab> createTab({
|
||||
required String customerName,
|
||||
});
|
||||
|
||||
Future<void> addProductToTab({
|
||||
required String tabId,
|
||||
required Product product,
|
||||
});
|
||||
|
||||
Future<void> updateTabItemQuantity({
|
||||
required String tabItemId,
|
||||
required int quantity,
|
||||
});
|
||||
|
||||
Future<void> closeTab(String tabId);
|
||||
}
|
||||
|
||||
class DriftBarTabService implements BarTabService {
|
||||
final AppDatabase database;
|
||||
final _uuid = const Uuid();
|
||||
|
||||
DriftBarTabService({
|
||||
required this.database,
|
||||
});
|
||||
|
||||
TabItem _mapItemRow(TabItemRow row) {
|
||||
return TabItem(
|
||||
id: row.id,
|
||||
tabId: row.tabId,
|
||||
productId: row.productId,
|
||||
productName: row.productName,
|
||||
quantity: row.quantity,
|
||||
unitPriceInCents: row.unitPriceInCents,
|
||||
);
|
||||
}
|
||||
|
||||
BarTab _mapTabRow(
|
||||
BarTabRow row,
|
||||
List<TabItem> items,
|
||||
) {
|
||||
return BarTab(
|
||||
id: row.id,
|
||||
customerName: row.customerName,
|
||||
status: row.status,
|
||||
openedAt: row.openedAt,
|
||||
closedAt: row.closedAt,
|
||||
items: items,
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<TabItem>> _getItemsForTab(String tabId) async {
|
||||
final query = database.select(database.tabItems)
|
||||
..where((item) => item.tabId.equals(tabId))
|
||||
..orderBy([
|
||||
(item) => OrderingTerm.asc(item.createdAt),
|
||||
]);
|
||||
|
||||
final rows = await query.get();
|
||||
|
||||
return rows.map(_mapItemRow).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<BarTab>> getOpenTabs() async {
|
||||
final query = database.select(database.barTabs)
|
||||
..where((tab) => tab.status.equals('open'))
|
||||
..orderBy([
|
||||
(tab) => OrderingTerm.desc(tab.openedAt),
|
||||
]);
|
||||
|
||||
final tabRows = await query.get();
|
||||
|
||||
final tabs = <BarTab>[];
|
||||
|
||||
for (final tabRow in tabRows) {
|
||||
final items = await _getItemsForTab(tabRow.id);
|
||||
|
||||
tabs.add(_mapTabRow(tabRow, items));
|
||||
}
|
||||
|
||||
return tabs;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<BarTab?> getTabById(String id) async {
|
||||
final query = database.select(database.barTabs)
|
||||
..where((tab) => tab.id.equals(id));
|
||||
|
||||
final tabRow = await query.getSingleOrNull();
|
||||
|
||||
if (tabRow == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final items = await _getItemsForTab(tabRow.id);
|
||||
|
||||
return _mapTabRow(tabRow, items);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<BarTab> createTab({
|
||||
required String customerName,
|
||||
}) async {
|
||||
final id = _uuid.v4();
|
||||
|
||||
await database.into(database.barTabs).insert(
|
||||
BarTabsCompanion.insert(
|
||||
id: id,
|
||||
customerName: customerName,
|
||||
status: const Value('open'),
|
||||
openedAt: DateTime.now(),
|
||||
),
|
||||
);
|
||||
|
||||
final tab = await getTabById(id);
|
||||
|
||||
if (tab == null) {
|
||||
throw Exception('Could not create tab.');
|
||||
}
|
||||
|
||||
return tab;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> addProductToTab({
|
||||
required String tabId,
|
||||
required Product product,
|
||||
}) async {
|
||||
await database.transaction(() async {
|
||||
final existingItemQuery = database.select(database.tabItems)
|
||||
..where(
|
||||
(item) =>
|
||||
item.tabId.equals(tabId) &
|
||||
item.productId.equals(product.id),
|
||||
);
|
||||
|
||||
final existingItem = await existingItemQuery.getSingleOrNull();
|
||||
|
||||
if (existingItem != null) {
|
||||
final updateQuery = database.update(database.tabItems)
|
||||
..where((item) => item.id.equals(existingItem.id));
|
||||
|
||||
await updateQuery.write(
|
||||
TabItemsCompanion(
|
||||
quantity: Value(existingItem.quantity + 1),
|
||||
),
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await database.into(database.tabItems).insert(
|
||||
TabItemsCompanion.insert(
|
||||
id: _uuid.v4(),
|
||||
tabId: tabId,
|
||||
productId: product.id,
|
||||
productName: product.name,
|
||||
quantity: 1,
|
||||
unitPriceInCents: product.priceInCents,
|
||||
createdAt: DateTime.now(),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateTabItemQuantity({
|
||||
required String tabItemId,
|
||||
required int quantity,
|
||||
}) async {
|
||||
if (quantity <= 0) {
|
||||
final deleteQuery = database.delete(database.tabItems)
|
||||
..where((item) => item.id.equals(tabItemId));
|
||||
|
||||
await deleteQuery.go();
|
||||
return;
|
||||
}
|
||||
|
||||
final updateQuery = database.update(database.tabItems)
|
||||
..where((item) => item.id.equals(tabItemId));
|
||||
|
||||
await updateQuery.write(
|
||||
TabItemsCompanion(
|
||||
quantity: Value(quantity),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> closeTab(String tabId) async {
|
||||
final updateQuery = database.update(database.barTabs)
|
||||
..where((tab) => tab.id.equals(tabId));
|
||||
|
||||
await updateQuery.write(
|
||||
BarTabsCompanion(
|
||||
status: const Value('closed'),
|
||||
closedAt: Value(DateTime.now()),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../database/app_database.dart';
|
||||
import '../models/product.dart';
|
||||
|
||||
abstract class ProductService {
|
||||
Future<List<Product>> getProducts();
|
||||
|
||||
Future<Product?> getProductById(String id);
|
||||
|
||||
Future<void> createProduct({
|
||||
required String name,
|
||||
required String category,
|
||||
required int stockQuantity,
|
||||
required int lowStockThreshold,
|
||||
required int priceInCents,
|
||||
required String? imagePath,
|
||||
});
|
||||
|
||||
Future<void> updateProduct(Product product);
|
||||
|
||||
Future<void> deleteProduct(String id);
|
||||
}
|
||||
|
||||
class DriftProductService implements ProductService {
|
||||
final AppDatabase database;
|
||||
final _uuid = const Uuid();
|
||||
|
||||
DriftProductService({
|
||||
required this.database,
|
||||
});
|
||||
|
||||
Product _mapRowToProduct(ProductRow row) {
|
||||
return Product(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
category: row.category,
|
||||
stockQuantity: row.stockQuantity,
|
||||
lowStockThreshold: row.lowStockThreshold,
|
||||
priceInCents: row.priceInCents,
|
||||
imagePath: row.imagePath,
|
||||
active: row.active,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Product>> getProducts() async {
|
||||
final query = database.select(database.products)
|
||||
..where((product) => product.active.equals(true))
|
||||
..orderBy([
|
||||
(product) => OrderingTerm.asc(product.name),
|
||||
]);
|
||||
|
||||
final rows = await query.get();
|
||||
|
||||
return rows.map(_mapRowToProduct).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Product?> getProductById(String id) async {
|
||||
final query = database.select(database.products)
|
||||
..where((product) => product.id.equals(id));
|
||||
|
||||
final row = await query.getSingleOrNull();
|
||||
|
||||
if (row == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return _mapRowToProduct(row);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> createProduct({
|
||||
required String name,
|
||||
required String category,
|
||||
required int stockQuantity,
|
||||
required int lowStockThreshold,
|
||||
required int priceInCents,
|
||||
required String? imagePath,
|
||||
}) async {
|
||||
await database.into(database.products).insert(
|
||||
ProductsCompanion.insert(
|
||||
id: _uuid.v4(),
|
||||
name: name,
|
||||
category: category,
|
||||
stockQuantity: stockQuantity,
|
||||
lowStockThreshold: lowStockThreshold,
|
||||
priceInCents: priceInCents,
|
||||
imagePath: Value(imagePath),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateProduct(Product product) async {
|
||||
final query = database.update(database.products)
|
||||
..where((row) => row.id.equals(product.id));
|
||||
|
||||
await query.write(
|
||||
ProductsCompanion(
|
||||
name: Value(product.name),
|
||||
category: Value(product.category),
|
||||
stockQuantity: Value(product.stockQuantity),
|
||||
lowStockThreshold: Value(product.lowStockThreshold),
|
||||
priceInCents: Value(product.priceInCents),
|
||||
imagePath: Value(product.imagePath),
|
||||
active: Value(product.active),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteProduct(String id) async {
|
||||
final query = database.delete(database.products)
|
||||
..where((product) => product.id.equals(id));
|
||||
|
||||
await query.go();
|
||||
}
|
||||
}
|
||||
+327
@@ -0,0 +1,327 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
final ThemeData darkTheme = ThemeData(
|
||||
useMaterial3: true,
|
||||
brightness: Brightness.dark,
|
||||
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: const Color(0xFF7C4DFF),
|
||||
brightness: Brightness.dark,
|
||||
),
|
||||
|
||||
scaffoldBackgroundColor: const Color(0xFF121212),
|
||||
canvasColor: const Color(0xFF121212),
|
||||
|
||||
appBarTheme: const AppBarTheme(
|
||||
elevation: 0,
|
||||
centerTitle: true,
|
||||
backgroundColor: Colors.transparent,
|
||||
foregroundColor: Colors.white,
|
||||
surfaceTintColor: Colors.transparent,
|
||||
titleTextStyle: TextStyle(
|
||||
fontSize: 22,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
|
||||
cardTheme: CardThemeData(
|
||||
color: const Color(0xFF1E1E1E),
|
||||
elevation: 0,
|
||||
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(18),
|
||||
side: BorderSide(
|
||||
color: Colors.white.withOpacity(0.06),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
elevatedButtonTheme: ElevatedButtonThemeData(
|
||||
style: ElevatedButton.styleFrom(
|
||||
elevation: 0,
|
||||
backgroundColor: const Color(0xFF7C4DFF),
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 24,
|
||||
vertical: 16,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
),
|
||||
textStyle: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
outlinedButtonTheme: OutlinedButtonThemeData(
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: Colors.white,
|
||||
side: BorderSide(
|
||||
color: Colors.white.withOpacity(0.15),
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 24,
|
||||
vertical: 16,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
inputDecorationTheme: InputDecorationTheme(
|
||||
filled: true,
|
||||
fillColor: const Color(0xFF242424),
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 18,
|
||||
vertical: 16,
|
||||
),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
borderSide: BorderSide(
|
||||
color: Colors.white.withOpacity(0.08),
|
||||
),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
borderSide: const BorderSide(
|
||||
color: Color(0xFF7C4DFF),
|
||||
width: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
floatingActionButtonTheme: const FloatingActionButtonThemeData(
|
||||
backgroundColor: Color(0xFF7C4DFF),
|
||||
foregroundColor: Colors.white,
|
||||
elevation: 2,
|
||||
),
|
||||
|
||||
navigationBarTheme: NavigationBarThemeData(
|
||||
backgroundColor: const Color(0xFF1A1A1A),
|
||||
indicatorColor: const Color(0xFF7C4DFF).withOpacity(0.25),
|
||||
labelTextStyle: WidgetStatePropertyAll(
|
||||
TextStyle(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
listTileTheme: ListTileThemeData(
|
||||
tileColor: const Color(0xFF1E1E1E),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
),
|
||||
iconColor: const Color(0xFFB39DFF),
|
||||
textColor: Colors.white,
|
||||
),
|
||||
|
||||
snackBarTheme: SnackBarThemeData(
|
||||
backgroundColor: const Color(0xFF2A2A2A),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
|
||||
dividerTheme: DividerThemeData(
|
||||
color: Colors.white.withOpacity(0.08),
|
||||
thickness: 1,
|
||||
),
|
||||
|
||||
textTheme: const TextTheme(
|
||||
displayLarge: TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
headlineMedium: TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
titleLarge: TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
bodyLarge: TextStyle(
|
||||
color: Colors.white70,
|
||||
fontSize: 16,
|
||||
height: 1.5,
|
||||
),
|
||||
bodyMedium: TextStyle(
|
||||
color: Colors.white60,
|
||||
height: 1.4,
|
||||
),
|
||||
labelLarge: TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final ThemeData neoBrutalDarkTheme = ThemeData(
|
||||
useMaterial3: true,
|
||||
brightness: Brightness.dark,
|
||||
|
||||
colorScheme: const ColorScheme.dark(
|
||||
primary: Color(0xFFFFD60A), // Bright yellow
|
||||
secondary: Color(0xFF00E5FF), // Cyan
|
||||
surface: Color(0xFF1A1A1A),
|
||||
error: Color(0xFFFF5252),
|
||||
),
|
||||
|
||||
scaffoldBackgroundColor: const Color(0xFF0E0E0E),
|
||||
canvasColor: const Color(0xFF0E0E0E),
|
||||
|
||||
textTheme: const TextTheme(
|
||||
displayLarge: TextStyle(
|
||||
fontSize: 48,
|
||||
fontWeight: FontWeight.w900,
|
||||
color: Colors.white,
|
||||
letterSpacing: -1,
|
||||
),
|
||||
headlineMedium: TextStyle(
|
||||
fontSize: 32,
|
||||
fontWeight: FontWeight.w900,
|
||||
color: Colors.white,
|
||||
),
|
||||
titleLarge: TextStyle(
|
||||
fontSize: 22,
|
||||
fontWeight: FontWeight.w800,
|
||||
color: Colors.white,
|
||||
),
|
||||
bodyLarge: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Colors.white,
|
||||
),
|
||||
bodyMedium: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.white70,
|
||||
),
|
||||
),
|
||||
|
||||
appBarTheme: const AppBarTheme(
|
||||
backgroundColor: Color(0xFF0E0E0E),
|
||||
foregroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
centerTitle: false,
|
||||
titleTextStyle: TextStyle(
|
||||
fontSize: 26,
|
||||
fontWeight: FontWeight.w900,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
|
||||
cardTheme: CardThemeData(
|
||||
color: const Color(0xFF242424),
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
side: const BorderSide(
|
||||
color: Colors.white,
|
||||
width: 3,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
elevatedButtonTheme: ElevatedButtonThemeData(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFFFFD60A),
|
||||
foregroundColor: Colors.black,
|
||||
elevation: 0,
|
||||
shadowColor: Colors.transparent,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 24,
|
||||
vertical: 18,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
side: const BorderSide(
|
||||
color: Colors.black,
|
||||
width: 3,
|
||||
),
|
||||
),
|
||||
textStyle: const TextStyle(
|
||||
fontWeight: FontWeight.w900,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
inputDecorationTheme: InputDecorationTheme(
|
||||
filled: true,
|
||||
fillColor: const Color(0xFF242424),
|
||||
contentPadding: const EdgeInsets.all(18),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderSide: const BorderSide(
|
||||
color: Colors.white,
|
||||
width: 3,
|
||||
),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderSide: const BorderSide(
|
||||
color: Colors.white,
|
||||
width: 3,
|
||||
),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
borderSide: const BorderSide(
|
||||
color: Color(0xFFFFD60A),
|
||||
width: 4,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
floatingActionButtonTheme: const FloatingActionButtonThemeData(
|
||||
backgroundColor: Color(0xFFFF5252),
|
||||
foregroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(8)),
|
||||
side: BorderSide(
|
||||
color: Colors.white,
|
||||
width: 3,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
snackBarTheme: SnackBarThemeData(
|
||||
backgroundColor: const Color(0xFF242424),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
side: const BorderSide(
|
||||
color: Colors.white,
|
||||
width: 3,
|
||||
),
|
||||
),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
|
||||
dividerTheme: const DividerThemeData(
|
||||
color: Colors.white,
|
||||
thickness: 3,
|
||||
),
|
||||
|
||||
listTileTheme: ListTileThemeData(
|
||||
tileColor: const Color(0xFF242424),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
side: const BorderSide(
|
||||
color: Colors.white,
|
||||
width: 3,
|
||||
),
|
||||
),
|
||||
textColor: Colors.white,
|
||||
iconColor: const Color(0xFFFFD60A),
|
||||
),
|
||||
);
|
||||
@@ -0,0 +1,140 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../models/bar_tab.dart';
|
||||
import '../models/product.dart';
|
||||
import '../models/tab_item.dart';
|
||||
import '../services/bar_tab_service.dart';
|
||||
import '../services/product_service.dart';
|
||||
|
||||
class BarScreenViewModel extends ChangeNotifier {
|
||||
final BarTabService barTabService;
|
||||
|
||||
BarScreenViewModel({
|
||||
required this.barTabService,
|
||||
});
|
||||
|
||||
List<BarTab> _tabs = [];
|
||||
String? _selectedTabId;
|
||||
bool _isLoading = false;
|
||||
bool _hasLoaded = false;
|
||||
String? _errorMessage;
|
||||
|
||||
List<BarTab> get tabs => _tabs;
|
||||
|
||||
String? get selectedTabId => _selectedTabId;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
bool get hasLoaded => _hasLoaded;
|
||||
|
||||
String? get errorMessage => _errorMessage;
|
||||
|
||||
BarTab? get selectedTab {
|
||||
if (_selectedTabId == null) return null;
|
||||
|
||||
try {
|
||||
return _tabs.firstWhere((tab) => tab.id == _selectedTabId);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> ensureLoaded() async {
|
||||
if (_hasLoaded || _isLoading) return;
|
||||
|
||||
await load();
|
||||
}
|
||||
|
||||
Future<void> load() async {
|
||||
if (_isLoading) return;
|
||||
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_tabs = await barTabService.getOpenTabs();
|
||||
|
||||
if (_selectedTabId == null ||
|
||||
!_tabs.any((tab) => tab.id == _selectedTabId)) {
|
||||
_selectedTabId = _tabs.isEmpty ? null : _tabs.first.id;
|
||||
}
|
||||
} catch (_) {
|
||||
_errorMessage = 'Could not load bar screen.';
|
||||
} finally {
|
||||
_hasLoaded = true;
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void selectTab(String tabId) {
|
||||
_selectedTabId = tabId;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> createTab(String customerName) async {
|
||||
final tab = await barTabService.createTab(
|
||||
customerName: customerName,
|
||||
);
|
||||
|
||||
_selectedTabId = tab.id;
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
Future<void> addProductToSelectedTab(Product product) async {
|
||||
final tab = selectedTab;
|
||||
|
||||
if (tab == null) return;
|
||||
|
||||
await barTabService.addProductToTab(
|
||||
tabId: tab.id,
|
||||
product: product,
|
||||
);
|
||||
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
Future<void> changeItemQuantity(
|
||||
TabItem item,
|
||||
int quantity,
|
||||
) async {
|
||||
await barTabService.updateTabItemQuantity(
|
||||
tabItemId: item.id,
|
||||
quantity: quantity,
|
||||
);
|
||||
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
Future<void> closeSelectedTab() async {
|
||||
final tab = selectedTab;
|
||||
|
||||
if (tab == null) return;
|
||||
|
||||
await barTabService.closeTab(tab.id);
|
||||
|
||||
_selectedTabId = null;
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
Future<void> closeTab(String tabId) async {
|
||||
await barTabService.closeTab(tabId);
|
||||
|
||||
if (_selectedTabId == tabId) {
|
||||
_selectedTabId = null;
|
||||
}
|
||||
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
Future<void> _reloadTabs() async {
|
||||
_tabs = await barTabService.getOpenTabs();
|
||||
|
||||
if (_selectedTabId == null || !_tabs.any((tab) => tab.id == _selectedTabId)) {
|
||||
_selectedTabId = _tabs.isEmpty ? null : _tabs.first.id;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../models/product.dart';
|
||||
import '../services/product_service.dart';
|
||||
|
||||
class ProductListViewModel extends ChangeNotifier {
|
||||
final ProductService productService;
|
||||
|
||||
ProductListViewModel({
|
||||
required this.productService,
|
||||
});
|
||||
|
||||
static const List<String> _defaultCategories = [
|
||||
'Bier',
|
||||
'Wijn',
|
||||
'Frisdrank',
|
||||
'Cocktails',
|
||||
'Snacks',
|
||||
'Coffee',
|
||||
'Thee',
|
||||
'Other',
|
||||
];
|
||||
|
||||
List<Product> _products = [];
|
||||
bool _isLoading = false;
|
||||
bool _hasLoaded = false;
|
||||
String? _errorMessage;
|
||||
|
||||
List<Product> get products => _products;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
bool get hasLoaded => _hasLoaded;
|
||||
|
||||
String? get errorMessage => _errorMessage;
|
||||
|
||||
Future<void> ensureLoaded() async {
|
||||
if (_hasLoaded || _isLoading) return;
|
||||
|
||||
await loadProducts();
|
||||
}
|
||||
|
||||
Future<void> loadProducts() async {
|
||||
if (_isLoading) return;
|
||||
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_products = await productService.getProducts();
|
||||
} catch (_) {
|
||||
_errorMessage = 'Could not load products.';
|
||||
} finally {
|
||||
_hasLoaded = true;
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> addProduct({
|
||||
required String name,
|
||||
required String category,
|
||||
required int stockQuantity,
|
||||
required int lowStockThreshold,
|
||||
required int priceInCents,
|
||||
required String? imagePath,
|
||||
}) async {
|
||||
await productService.createProduct(
|
||||
name: name,
|
||||
category: category,
|
||||
stockQuantity: stockQuantity,
|
||||
lowStockThreshold: lowStockThreshold,
|
||||
priceInCents: priceInCents,
|
||||
imagePath: imagePath,
|
||||
);
|
||||
|
||||
await loadProducts();
|
||||
}
|
||||
|
||||
Future<void> updateProduct(Product product) async {
|
||||
await productService.updateProduct(product);
|
||||
await loadProducts();
|
||||
}
|
||||
|
||||
Future<void> deleteProduct(String id) async {
|
||||
await productService.deleteProduct(id);
|
||||
await loadProducts();
|
||||
}
|
||||
|
||||
Future<Product?> getProductById(String id) {
|
||||
return productService.getProductById(id);
|
||||
}
|
||||
|
||||
List<String> get categories {
|
||||
final categories = {
|
||||
..._defaultCategories,
|
||||
..._products
|
||||
.map((p) => p.category.trim())
|
||||
.where((c) => c.isNotEmpty),
|
||||
}.toList();
|
||||
|
||||
categories.sort();
|
||||
|
||||
return categories;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,578 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widget_previews.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:kooltab2/viewmodels/product_list_view_model.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'dart:io';
|
||||
import 'package:flutter_slidable/flutter_slidable.dart';
|
||||
|
||||
import '../models/bar_tab.dart';
|
||||
import '../models/product.dart';
|
||||
import '../models/tab_item.dart';
|
||||
import '../viewmodels/bar_screen_view_model.dart';
|
||||
|
||||
class BarScreenView extends StatelessWidget {
|
||||
const BarScreenView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final viewModel = context.watch<BarScreenViewModel>();
|
||||
final productsViewModel = context.watch<ProductListViewModel>();
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: 'Manage products',
|
||||
onPressed: () => context.go('/products'),
|
||||
icon: const Icon(Icons.inventory_2_outlined),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Refresh',
|
||||
onPressed: viewModel.load,
|
||||
icon: const Icon(Icons.refresh),
|
||||
),
|
||||
],
|
||||
),
|
||||
resizeToAvoidBottomInset: false,
|
||||
body: Builder(
|
||||
builder: (context) {
|
||||
if (viewModel.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (viewModel.errorMessage != null) {
|
||||
return Center(child: Text(viewModel.errorMessage!));
|
||||
}
|
||||
|
||||
return Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 3,
|
||||
child: _ProductGrid(
|
||||
products: productsViewModel.products,
|
||||
hasSelectedTab: viewModel.selectedTab != null,
|
||||
onProductTap: (product) async {
|
||||
if (viewModel.selectedTab == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Open or select a tab first.'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await viewModel.addProductToSelectedTab(product);
|
||||
},
|
||||
),
|
||||
),
|
||||
const VerticalDivider(width: 1),
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: _TabPanel(
|
||||
tabs: viewModel.tabs,
|
||||
selectedTab: viewModel.selectedTab,
|
||||
selectedTabId: viewModel.selectedTabId,
|
||||
onNewTabPressed: () => _showNewTabDialog(context),
|
||||
onTabSelected: viewModel.selectTab,
|
||||
onItemQuantityChanged: viewModel.changeItemQuantity,
|
||||
onCloseTabPressed: () => _confirmCloseTab(context),
|
||||
onTabClosed: viewModel.closeTab,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _showNewTabDialog(BuildContext context) async {
|
||||
final controller = TextEditingController();
|
||||
|
||||
final name = await showDialog<String>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
title: const Text('Open new tab'),
|
||||
content: TextField(
|
||||
controller: controller,
|
||||
autofocus: true,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Customer / group name',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
onSubmitted: (value) {
|
||||
Navigator.of(dialogContext).pop(value);
|
||||
},
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
Navigator.of(dialogContext).pop(controller.text);
|
||||
},
|
||||
child: const Text('Open tab'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (name == null || name.trim().isEmpty) return;
|
||||
|
||||
if (!context.mounted) return;
|
||||
|
||||
await context.read<BarScreenViewModel>().createTab(name.trim());
|
||||
}
|
||||
|
||||
Future<void> _confirmCloseTab(BuildContext context) async {
|
||||
final viewModel = context.read<BarScreenViewModel>();
|
||||
final tab = viewModel.selectedTab;
|
||||
|
||||
if (tab == null) return;
|
||||
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
title: Text('Close ${tab.customerName}ʼs tab?'),
|
||||
content: Text(
|
||||
'Current total: ${tab.formattedTotal}\n\n'
|
||||
'Payments are not handled yet, so this only marks the tab as closed.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||
child: const Text('Close tab'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (confirmed != true) return;
|
||||
|
||||
await viewModel.closeSelectedTab();
|
||||
}
|
||||
}
|
||||
|
||||
class _ProductGrid extends StatelessWidget {
|
||||
final List<Product> products;
|
||||
final bool hasSelectedTab;
|
||||
final ValueChanged<Product> onProductTap;
|
||||
|
||||
const _ProductGrid({
|
||||
required this.products,
|
||||
required this.hasSelectedTab,
|
||||
required this.onProductTap,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (products.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(Icons.inventory_2_outlined, size: 48),
|
||||
const SizedBox(height: 12),
|
||||
const Text('No products yet.'),
|
||||
const SizedBox(height: 12),
|
||||
FilledButton.icon(
|
||||
onPressed: () => context.go('/products/new'),
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('Add product'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final columns = ((constraints.maxWidth / 170).floor())
|
||||
.clamp(3, 6)
|
||||
.toInt();
|
||||
|
||||
return GridView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: products.length,
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: columns,
|
||||
crossAxisSpacing: 12,
|
||||
mainAxisSpacing: 12,
|
||||
childAspectRatio: 1,
|
||||
),
|
||||
itemBuilder: (context, index) {
|
||||
final product = products[index];
|
||||
|
||||
return _ProductTile(
|
||||
product: product,
|
||||
enabled: hasSelectedTab,
|
||||
onTap: () => onProductTap(product),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ProductTile extends StatelessWidget {
|
||||
final Product product;
|
||||
final bool enabled;
|
||||
final VoidCallback onTap;
|
||||
|
||||
const _ProductTile({
|
||||
required this.product,
|
||||
required this.enabled,
|
||||
required this.onTap,
|
||||
});
|
||||
|
||||
bool get _hasImage {
|
||||
final imagePath = product.imagePath;
|
||||
|
||||
if (imagePath == null || imagePath.isEmpty) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return File(imagePath).existsSync();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
clipBehavior: Clip.antiAlias,
|
||||
margin: EdgeInsets.zero,
|
||||
child: InkWell(
|
||||
onTap: enabled ? onTap : null,
|
||||
child: Opacity(
|
||||
opacity: enabled ? 1 : 0.45,
|
||||
child: _hasImage
|
||||
? Image.file(
|
||||
File(product.imagePath!),
|
||||
fit: BoxFit.scaleDown,
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
)
|
||||
: const Center(
|
||||
child: Stack(
|
||||
children: [
|
||||
Icon(Icons.image_not_supported_outlined, size: 42),
|
||||
Text("No Image found!"),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TabPanel extends StatefulWidget {
|
||||
final List<BarTab> tabs;
|
||||
final BarTab? selectedTab;
|
||||
final String? selectedTabId;
|
||||
final VoidCallback onNewTabPressed;
|
||||
final ValueChanged<String> onTabSelected;
|
||||
final Future<void> Function(TabItem item, int quantity) onItemQuantityChanged;
|
||||
final VoidCallback onCloseTabPressed;
|
||||
final ValueChanged<String> onTabClosed;
|
||||
|
||||
const _TabPanel({
|
||||
required this.tabs,
|
||||
required this.selectedTab,
|
||||
required this.selectedTabId,
|
||||
required this.onNewTabPressed,
|
||||
required this.onTabSelected,
|
||||
required this.onItemQuantityChanged,
|
||||
required this.onCloseTabPressed,
|
||||
required this.onTabClosed,
|
||||
});
|
||||
|
||||
@override
|
||||
State<_TabPanel> createState() => _TabPanelState();
|
||||
}
|
||||
|
||||
class _TabPanelState extends State<_TabPanel> {
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
String _searchQuery = '';
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final filteredTabs = widget.tabs.where((tab) {
|
||||
return tab.customerName.toLowerCase().contains(
|
||||
_searchQuery.toLowerCase(),
|
||||
);
|
||||
}).toList();
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: FilledButton.icon(
|
||||
onPressed: widget.onNewTabPressed,
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('Open tab'),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
TextField(
|
||||
controller: _searchController,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_searchQuery = value;
|
||||
});
|
||||
},
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Search name...',
|
||||
prefixIcon: Icon(Icons.search),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Text(
|
||||
'Open tabs',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
|
||||
SizedBox(
|
||||
height: 200,
|
||||
child: _OpenTabsList(
|
||||
tabs: filteredTabs,
|
||||
selectedTabId: widget.selectedTabId,
|
||||
onTabSelected: widget.onTabSelected,
|
||||
onTabClosed: widget.onTabClosed,
|
||||
),
|
||||
),
|
||||
|
||||
const Divider(height: 24),
|
||||
|
||||
Expanded(
|
||||
child: widget.selectedTab == null
|
||||
? const Center(child: Text('Select or open a tab.'))
|
||||
: _SelectedTabDetails(
|
||||
tab: widget.selectedTab!,
|
||||
onItemQuantityChanged: widget.onItemQuantityChanged,
|
||||
onCloseTabPressed: widget.onCloseTabPressed,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _OpenTabsList extends StatelessWidget {
|
||||
final List<BarTab> tabs;
|
||||
final String? selectedTabId;
|
||||
final ValueChanged<String> onTabSelected;
|
||||
final ValueChanged<String> onTabClosed;
|
||||
|
||||
const _OpenTabsList({
|
||||
required this.tabs,
|
||||
required this.selectedTabId,
|
||||
required this.onTabSelected,
|
||||
required this.onTabClosed,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (tabs.isEmpty) {
|
||||
return const Center(child: Text('No open tabs.'));
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
itemCount: tabs.length,
|
||||
separatorBuilder: (_, __) => const SizedBox(height: 6),
|
||||
itemBuilder: (context, index) {
|
||||
final tab = tabs[index];
|
||||
final selected = tab.id == selectedTabId;
|
||||
|
||||
return ClipRRect(
|
||||
child: Slidable(
|
||||
key: ValueKey(tab.id),
|
||||
endActionPane: ActionPane(
|
||||
motion: const DrawerMotion(),
|
||||
extentRatio: 0.65,
|
||||
children: [
|
||||
SlidableAction(
|
||||
onPressed: (_) {
|
||||
onTabSelected(tab.id);
|
||||
// TODO: rename/edit tab
|
||||
},
|
||||
icon: Icons.edit_outlined,
|
||||
label: 'Edit',
|
||||
backgroundColor: Theme.of(context).colorScheme.secondary,
|
||||
foregroundColor: Theme.of(context).colorScheme.onSecondary,
|
||||
),
|
||||
SlidableAction(
|
||||
onPressed: (_) => onTabClosed(tab.id),
|
||||
icon: Icons.close,
|
||||
label: 'Close',
|
||||
backgroundColor: Theme.of(context).colorScheme.error,
|
||||
foregroundColor: Theme.of(context).colorScheme.onError,
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Card(
|
||||
margin: EdgeInsets.zero,
|
||||
color: selected
|
||||
? Theme.of(context).colorScheme.primaryContainer
|
||||
: null,
|
||||
child: ListTile(
|
||||
dense: true,
|
||||
selected: selected,
|
||||
title: Text(
|
||||
tab.customerName,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
subtitle: Text('${tab.itemCount} items • ${tab.formattedTotal}'),
|
||||
onTap: () => onTabSelected(tab.id),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SelectedTabDetails extends StatelessWidget {
|
||||
final BarTab tab;
|
||||
final Future<void> Function(TabItem item, int quantity) onItemQuantityChanged;
|
||||
final VoidCallback onCloseTabPressed;
|
||||
|
||||
const _SelectedTabDetails({
|
||||
required this.tab,
|
||||
required this.onItemQuantityChanged,
|
||||
required this.onCloseTabPressed,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
tab.customerName,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'Total: ${tab.formattedTotal}',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Expanded(
|
||||
child: tab.items.isEmpty
|
||||
? const Center(child: Text('Tap products to add them.'))
|
||||
: ListView.separated(
|
||||
itemCount: tab.items.length,
|
||||
separatorBuilder: (_, __) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final item = tab.items[index];
|
||||
|
||||
return _TabItemRow(
|
||||
item: item,
|
||||
onQuantityChanged: onItemQuantityChanged,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const Divider(height: 24),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
tab.formattedTotal,
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: onCloseTabPressed,
|
||||
child: const Text('Close'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TabItemRow extends StatelessWidget {
|
||||
final TabItem item;
|
||||
final Future<void> Function(TabItem item, int quantity) onQuantityChanged;
|
||||
|
||||
const _TabItemRow({required this.item, required this.onQuantityChanged});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
item.productName,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
Text(
|
||||
'${item.quantity} × ${item.formattedUnitPrice}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
onPressed: () => onQuantityChanged(item, item.quantity - 1),
|
||||
icon: const Icon(Icons.remove_circle_outline),
|
||||
),
|
||||
Text('${item.quantity}'),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
onPressed: () => onQuantityChanged(item, item.quantity + 1),
|
||||
icon: const Icon(Icons.add_circle_outline),
|
||||
),
|
||||
SizedBox(
|
||||
width: 72,
|
||||
child: Text(item.formattedLineTotal, textAlign: TextAlign.end),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,451 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../models/product.dart';
|
||||
import '../viewmodels/product_list_view_model.dart';
|
||||
|
||||
class ProductFormView extends StatefulWidget {
|
||||
final String? productId;
|
||||
|
||||
const ProductFormView({
|
||||
super.key,
|
||||
this.productId,
|
||||
});
|
||||
|
||||
bool get isEditing => productId != null;
|
||||
|
||||
@override
|
||||
State<ProductFormView> createState() => _ProductFormViewState();
|
||||
}
|
||||
|
||||
class _ProductFormViewState extends State<ProductFormView> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
final _nameController = TextEditingController();
|
||||
final _priceController = TextEditingController();
|
||||
final _stockController = TextEditingController();
|
||||
final _lowStockController = TextEditingController();
|
||||
|
||||
String? _selectedCategory;
|
||||
final _categoryController = TextEditingController();
|
||||
|
||||
final _imagePicker = ImagePicker();
|
||||
|
||||
Product? _existingProduct;
|
||||
String? _imagePath;
|
||||
bool _isLoading = true;
|
||||
bool _isSaving = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadProductIfNeeded();
|
||||
}
|
||||
|
||||
Future<void> _loadProductIfNeeded() async {
|
||||
if (!widget.isEditing) {
|
||||
setState(() => _isLoading = false);
|
||||
return;
|
||||
}
|
||||
|
||||
final viewModel = context.read<ProductListViewModel>();
|
||||
final product = await viewModel.getProductById(widget.productId!);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
if (product == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Product not found.'),
|
||||
),
|
||||
);
|
||||
|
||||
context.go('/products');
|
||||
return;
|
||||
}
|
||||
|
||||
_existingProduct = product;
|
||||
_nameController.text = product.name;
|
||||
_selectedCategory = product.category;
|
||||
_priceController.text = (product.priceInCents / 100).toStringAsFixed(2);
|
||||
_stockController.text = product.stockQuantity.toString();
|
||||
_lowStockController.text = product.lowStockThreshold.toString();
|
||||
_imagePath = product.imagePath;
|
||||
|
||||
setState(() => _isLoading = false);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameController.dispose();
|
||||
_priceController.dispose();
|
||||
_stockController.dispose();
|
||||
_lowStockController.dispose();
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<String> _copyImageToAppFolder(XFile pickedFile) async {
|
||||
final appDirectory = await getApplicationSupportDirectory();
|
||||
final imagesDirectory = Directory(
|
||||
path.join(appDirectory.path, 'product_images'),
|
||||
);
|
||||
|
||||
if (!await imagesDirectory.exists()) {
|
||||
await imagesDirectory.create(recursive: true);
|
||||
}
|
||||
|
||||
final extension = path.extension(pickedFile.path);
|
||||
final fileName = 'product_${DateTime.now().millisecondsSinceEpoch}$extension';
|
||||
final newPath = path.join(imagesDirectory.path, fileName);
|
||||
|
||||
final copiedFile = await File(pickedFile.path).copy(newPath);
|
||||
|
||||
return copiedFile.path;
|
||||
}
|
||||
|
||||
Future<void> _pickImage() async {
|
||||
final pickedFile = await _imagePicker.pickImage(
|
||||
source: ImageSource.gallery,
|
||||
imageQuality: 85,
|
||||
maxWidth: 1000,
|
||||
);
|
||||
|
||||
if (pickedFile == null) return;
|
||||
|
||||
final copiedImagePath = await _copyImageToAppFolder(pickedFile);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
setState(() {
|
||||
_imagePath = copiedImagePath;
|
||||
});
|
||||
}
|
||||
|
||||
int _parsePriceToCents(String value) {
|
||||
final normalized = value.replaceAll(',', '.');
|
||||
final euros = double.tryParse(normalized) ?? 0;
|
||||
|
||||
return (euros * 100).round();
|
||||
}
|
||||
|
||||
Future<void> _save() async {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
|
||||
if (_imagePath == null || _imagePath!.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Choose a product image.'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_selectedCategory == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Please select a category.'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => _isSaving = true);
|
||||
|
||||
final viewModel = context.read<ProductListViewModel>();
|
||||
|
||||
final name = _nameController.text.trim();
|
||||
final category = _selectedCategory!;
|
||||
final priceInCents = _parsePriceToCents(_priceController.text);
|
||||
final stockQuantity = int.parse(_stockController.text);
|
||||
final lowStockThreshold = int.parse(_lowStockController.text);
|
||||
|
||||
if (widget.isEditing) {
|
||||
final updatedProduct = _existingProduct!.copyWith(
|
||||
name: name,
|
||||
category: category,
|
||||
priceInCents: priceInCents,
|
||||
stockQuantity: stockQuantity,
|
||||
lowStockThreshold: lowStockThreshold,
|
||||
imagePath: _imagePath,
|
||||
);
|
||||
|
||||
await viewModel.updateProduct(updatedProduct);
|
||||
} else {
|
||||
await viewModel.addProduct(
|
||||
name: name,
|
||||
category: category,
|
||||
priceInCents: priceInCents,
|
||||
stockQuantity: stockQuantity,
|
||||
lowStockThreshold: lowStockThreshold,
|
||||
imagePath: _imagePath,
|
||||
);
|
||||
}
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
setState(() => _isSaving = false);
|
||||
context.go('/products');
|
||||
}
|
||||
|
||||
Future<void> _delete() async {
|
||||
if (!widget.isEditing) return;
|
||||
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
title: const Text('Delete product?'),
|
||||
content: const Text(
|
||||
'This will remove the product from the product list.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||
child: const Text('Delete'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (confirmed != true) return;
|
||||
|
||||
final viewModel = context.read<ProductListViewModel>();
|
||||
await viewModel.deleteProduct(widget.productId!);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
context.go('/products');
|
||||
}
|
||||
|
||||
Widget _buildImagePicker(BuildContext context) {
|
||||
final hasImage = _imagePath != null && File(_imagePath!).existsSync();
|
||||
|
||||
return InkWell(
|
||||
onTap: _pickImage,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: Container(
|
||||
height: 220,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline,
|
||||
),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: hasImage
|
||||
? Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
Image.file(
|
||||
File(_imagePath!),
|
||||
fit: BoxFit.fitHeight,
|
||||
),
|
||||
Positioned(
|
||||
right: 12,
|
||||
bottom: 12,
|
||||
child: FilledButton.icon(
|
||||
onPressed: _pickImage,
|
||||
icon: const Icon(Icons.image),
|
||||
label: const Text('Change image'),
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(Icons.add_photo_alternate_outlined, size: 48),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'Choose product image',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final title = widget.isEditing ? 'Edit product' : 'Add product';
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(title),
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () => context.go('/products'),
|
||||
),
|
||||
actions: [
|
||||
if (widget.isEditing)
|
||||
IconButton(
|
||||
onPressed: _delete,
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
),
|
||||
],
|
||||
),
|
||||
resizeToAvoidBottomInset: false,
|
||||
body: _isLoading
|
||||
? const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
)
|
||||
: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 720),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
children: [
|
||||
_buildImagePicker(context),
|
||||
const SizedBox(height: 24),
|
||||
TextFormField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Product name',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
textInputAction: TextInputAction.next,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Enter a product name.';
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Consumer<ProductListViewModel>(
|
||||
builder: (context, viewModel, child) {
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
return DropdownMenu<String>(
|
||||
width: constraints.maxWidth,
|
||||
initialSelection: _selectedCategory,
|
||||
enableFilter: true,
|
||||
enableSearch: true,
|
||||
controller: _categoryController,
|
||||
requestFocusOnTap: true,
|
||||
label: const Text('Category'),
|
||||
hintText: 'Select a category',
|
||||
dropdownMenuEntries: viewModel.categories
|
||||
.map(
|
||||
(category) => DropdownMenuEntry<String>(
|
||||
value: category,
|
||||
label: category,
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
onSelected: (value) {
|
||||
setState(() {
|
||||
_selectedCategory = value;
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _priceController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Price',
|
||||
prefixText: '€ ',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: const TextInputType.numberWithOptions(
|
||||
decimal: true,
|
||||
),
|
||||
textInputAction: TextInputAction.next,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Enter a price.';
|
||||
}
|
||||
|
||||
final normalized = value.replaceAll(',', '.');
|
||||
final price = double.tryParse(normalized);
|
||||
|
||||
if (price == null || price < 0) {
|
||||
return 'Enter a valid price.';
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _stockController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Current stock',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.number,
|
||||
textInputAction: TextInputAction.next,
|
||||
validator: (value) {
|
||||
final number = int.tryParse(value ?? '');
|
||||
|
||||
if (number == null || number < 0) {
|
||||
return 'Enter a valid stock amount.';
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _lowStockController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Low stock warning threshold',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (value) {
|
||||
final number = int.tryParse(value ?? '');
|
||||
|
||||
if (number == null || number < 0) {
|
||||
return 'Enter a valid threshold.';
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
FilledButton.icon(
|
||||
onPressed: _isSaving ? null : _save,
|
||||
icon: _isSaving
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
),
|
||||
)
|
||||
: const Icon(Icons.save),
|
||||
label: Text(
|
||||
widget.isEditing ? 'Save changes' : 'Add product',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../viewmodels/product_list_view_model.dart';
|
||||
|
||||
class ProductListView extends StatelessWidget {
|
||||
const ProductListView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final viewModel = context.watch<ProductListViewModel>();
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Products'),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: 'Back to bar',
|
||||
onPressed: () => context.go('/bar'),
|
||||
icon: const Icon(Icons.point_of_sale),
|
||||
),
|
||||
],
|
||||
),
|
||||
resizeToAvoidBottomInset: false,
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: () => context.go('/products/new'),
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('Add product'),
|
||||
),
|
||||
body: Builder(
|
||||
builder: (context) {
|
||||
if (viewModel.isLoading) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
}
|
||||
|
||||
if (viewModel.errorMessage != null) {
|
||||
return Center(
|
||||
child: Text(viewModel.errorMessage!),
|
||||
);
|
||||
}
|
||||
|
||||
if (viewModel.products.isEmpty) {
|
||||
return const Center(
|
||||
child: Text('No products yet.'),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: viewModel.products.length,
|
||||
separatorBuilder: (_, __) => const SizedBox(height: 8),
|
||||
itemBuilder: (context, index) {
|
||||
final product = viewModel.products[index];
|
||||
|
||||
return Card(
|
||||
child: ListTile(
|
||||
leading: SizedBox(
|
||||
width: 56,
|
||||
height: 56,
|
||||
child: Center(
|
||||
child: Image.file(
|
||||
File(product.imagePath!),
|
||||
fit: BoxFit.contain,
|
||||
),
|
||||
),
|
||||
),
|
||||
title: Text(product.name),
|
||||
subtitle: Text(
|
||||
'${product.category} • ${product
|
||||
.formattedPrice} • Stock: ${product.stockQuantity}',
|
||||
),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () => context.go('/products/${product.id}/edit'),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user