118 lines
3.2 KiB
Dart
118 lines
3.2 KiB
Dart
/// Bottom navigation: products, lists, add, and shop log.
|
|
library;
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'products_screen.dart';
|
|
import 'scan_screen.dart';
|
|
import 'services/app_settings.dart';
|
|
import 'services/product_repository.dart';
|
|
import 'shop_log_screen.dart';
|
|
import 'shopping_lists_screen.dart';
|
|
|
|
class HomeShell extends StatefulWidget {
|
|
const HomeShell({super.key, required this.repository});
|
|
|
|
final ProductRepository repository;
|
|
|
|
@override
|
|
State<HomeShell> createState() => _HomeShellState();
|
|
}
|
|
|
|
class _HomeShellState extends State<HomeShell> {
|
|
static const _addIndex = 2;
|
|
static const _shopLogIndex = 3;
|
|
|
|
int _index = _addIndex;
|
|
int _revision = 0;
|
|
AppSettings? _settings;
|
|
int _seenGeneration = 0;
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
final settings = SettingsScope.maybeOf(context);
|
|
if (settings != _settings) {
|
|
_settings?.removeListener(_onSettings);
|
|
_settings = settings;
|
|
_seenGeneration = settings?.catalogGeneration ?? 0;
|
|
_settings?.addListener(_onSettings);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_settings?.removeListener(_onSettings);
|
|
super.dispose();
|
|
}
|
|
|
|
void _onSettings() {
|
|
final generation = _settings?.catalogGeneration ?? 0;
|
|
if (generation == _seenGeneration) return;
|
|
_seenGeneration = generation;
|
|
if (mounted) setState(() => _revision++);
|
|
}
|
|
|
|
void _onTripSaved() {
|
|
setState(() {
|
|
_index = _shopLogIndex;
|
|
_revision++;
|
|
});
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context)
|
|
.showSnackBar(const SnackBar(content: Text('Shopping trip saved.')));
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: switch (_index) {
|
|
0 => ProductsScreen(
|
|
key: ValueKey('products-$_revision'),
|
|
repository: widget.repository,
|
|
),
|
|
1 => ShoppingListsScreen(
|
|
key: ValueKey('lists-$_revision'),
|
|
repository: widget.repository,
|
|
),
|
|
2 => ScanScreen(
|
|
repository: widget.repository,
|
|
onTripSaved: _onTripSaved,
|
|
),
|
|
_ => ShopLogScreen(
|
|
key: ValueKey('shops-$_revision'),
|
|
repository: widget.repository,
|
|
),
|
|
},
|
|
bottomNavigationBar: NavigationBar(
|
|
selectedIndex: _index,
|
|
onDestinationSelected: (index) => setState(() => _index = index),
|
|
destinations: const [
|
|
NavigationDestination(
|
|
icon: Icon(Icons.inventory_2_outlined),
|
|
selectedIcon: Icon(Icons.inventory_2),
|
|
label: 'Products',
|
|
),
|
|
NavigationDestination(
|
|
icon: Icon(Icons.checklist_outlined),
|
|
selectedIcon: Icon(Icons.checklist),
|
|
label: 'Lists',
|
|
),
|
|
NavigationDestination(
|
|
icon: Icon(Icons.add),
|
|
selectedIcon: Icon(Icons.add_circle),
|
|
label: 'Add',
|
|
),
|
|
NavigationDestination(
|
|
icon: Icon(Icons.receipt_long_outlined),
|
|
selectedIcon: Icon(Icons.receipt_long),
|
|
label: 'Shop log',
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|