hwhub/lib/screens/home_shell.dart
copilot-swe-agent[bot] b9e298851b Add 30-second debounce to tab refresh in HomeShell
Co-authored-by: derkauzigekoala <79001016+derkauzigekoala@users.noreply.github.com>
2026-02-24 19:19:49 +00:00

70 lines
2 KiB
Dart

import 'package:flutter/material.dart';
import 'collections_screen.dart';
import 'scan_tab.dart';
import 'profile_screen.dart';
/// Bottom-navigation shell that hosts the three main tabs.
class HomeShell extends StatefulWidget {
const HomeShell({super.key});
@override
State<HomeShell> createState() => _HomeShellState();
}
class _HomeShellState extends State<HomeShell> {
int _currentIndex = 0;
final _collectionsKey = GlobalKey<CollectionsScreenState>();
final _scanKey = GlobalKey<ScanTabState>();
final Map<int, DateTime> _lastRefreshed = {};
static const _refreshDebounce = Duration(seconds: 30);
late final List<Widget> _pages = <Widget>[
CollectionsScreen(key: _collectionsKey),
ScanTab(key: _scanKey),
const ProfileScreen(),
];
void _onTabSelected(int i) {
setState(() => _currentIndex = i);
final now = DateTime.now();
final last = _lastRefreshed[i];
if (last != null && now.difference(last) < _refreshDebounce) return;
_lastRefreshed[i] = now;
if (i == 0) {
_collectionsKey.currentState?.refresh();
} else if (i == 1) {
_scanKey.currentState?.refresh();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: _pages,
),
bottomNavigationBar: NavigationBar(
selectedIndex: _currentIndex,
onDestinationSelected: _onTabSelected,
destinations: const [
NavigationDestination(
icon: Icon(Icons.collections_bookmark_outlined),
selectedIcon: Icon(Icons.collections_bookmark),
label: 'Collections',
),
NavigationDestination(
icon: Icon(Icons.qr_code_scanner_outlined),
selectedIcon: Icon(Icons.qr_code_scanner),
label: 'Scan',
),
NavigationDestination(
icon: Icon(Icons.person_outline),
selectedIcon: Icon(Icons.person),
label: 'Profile',
),
],
),
);
}
}