feat: add swipe navigation between main tabs

This commit is contained in:
Lukas Müllner 2026-03-05 09:30:32 +01:00
parent f03f1b18e4
commit fa738b9a96

View file

@ -12,6 +12,8 @@ class HomeShell extends StatefulWidget {
} }
class _HomeShellState extends State<HomeShell> { class _HomeShellState extends State<HomeShell> {
static const double _swipeVelocityThreshold = 300;
int _currentIndex = 0; int _currentIndex = 0;
final _collectionsKey = GlobalKey<CollectionsScreenState>(); final _collectionsKey = GlobalKey<CollectionsScreenState>();
final _scanKey = GlobalKey<ScanTabState>(); final _scanKey = GlobalKey<ScanTabState>();
@ -26,13 +28,31 @@ class _HomeShellState extends State<HomeShell> {
setState(() => _currentIndex = i); setState(() => _currentIndex = i);
} }
void _handleHorizontalSwipe(DragEndDetails details) {
final velocity = details.primaryVelocity ?? 0;
if (velocity.abs() < _swipeVelocityThreshold) return;
if (velocity < 0 && _currentIndex < _pages.length - 1) {
_onTabSelected(_currentIndex + 1);
return;
}
if (velocity > 0 && _currentIndex > 0) {
_onTabSelected(_currentIndex - 1);
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
body: IndexedStack( body: GestureDetector(
behavior: HitTestBehavior.translucent,
onHorizontalDragEnd: _handleHorizontalSwipe,
child: IndexedStack(
index: _currentIndex, index: _currentIndex,
children: _pages, children: _pages,
), ),
),
bottomNavigationBar: NavigationBar( bottomNavigationBar: NavigationBar(
selectedIndex: _currentIndex, selectedIndex: _currentIndex,
onDestinationSelected: _onTabSelected, onDestinationSelected: _onTabSelected,