feat: add swipe navigation between main tabs
This commit is contained in:
parent
f03f1b18e4
commit
fa738b9a96
1 changed files with 23 additions and 3 deletions
|
|
@ -12,6 +12,8 @@ class HomeShell extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _HomeShellState extends State<HomeShell> {
|
||||
static const double _swipeVelocityThreshold = 300;
|
||||
|
||||
int _currentIndex = 0;
|
||||
final _collectionsKey = GlobalKey<CollectionsScreenState>();
|
||||
final _scanKey = GlobalKey<ScanTabState>();
|
||||
|
|
@ -26,13 +28,31 @@ class _HomeShellState extends State<HomeShell> {
|
|||
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
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: IndexedStack(
|
||||
body: GestureDetector(
|
||||
behavior: HitTestBehavior.translucent,
|
||||
onHorizontalDragEnd: _handleHorizontalSwipe,
|
||||
child: IndexedStack(
|
||||
index: _currentIndex,
|
||||
children: _pages,
|
||||
),
|
||||
),
|
||||
bottomNavigationBar: NavigationBar(
|
||||
selectedIndex: _currentIndex,
|
||||
onDestinationSelected: _onTabSelected,
|
||||
|
|
|
|||
Loading…
Reference in a new issue