Better navigation

This commit is contained in:
LittleSheep 2024-02-10 22:18:49 +08:00
parent 4caca49baf
commit 641b3b850a
2 changed files with 68 additions and 47 deletions

View File

@ -5,23 +5,27 @@ import 'package:goatagent/screens/dashboard.dart';
import 'package:goatagent/screens/notifications.dart'; import 'package:goatagent/screens/notifications.dart';
class AgentNavigation extends StatefulWidget { class AgentNavigation extends StatefulWidget {
const AgentNavigation({super.key}); AgentNavigation({super.key});
static const items = [ static const items = [
NavigationDestination( {'label': 'Dashboard', 'icon': Icon(Icons.home)},
icon: Icon(Icons.home), {'label': 'Notifications', 'icon': Icon(Icons.notifications)},
label: 'Dashboard', {'label': 'Account', 'icon': Icon(Icons.account_circle)},
),
NavigationDestination(
icon: Icon(Icons.notifications),
label: 'Notifications',
),
NavigationDestination(
icon: Icon(Icons.account_circle),
label: 'Account',
)
]; ];
final bottomDestinations = items
.map((element) => BottomNavigationBarItem(
icon: element["icon"] as Widget,
label: element["label"] as String,
))
.toList();
final railDestinations = items
.map((element) => NavigationRailDestination(
icon: element["icon"] as Widget,
label: Text(element["label"] as String),
))
.toList();
@override @override
State<AgentNavigation> createState() => _AgentNavigationState(); State<AgentNavigation> createState() => _AgentNavigationState();
} }
@ -37,7 +41,7 @@ class _AgentNavigationState extends State<AgentNavigation> {
} }
RemoteMessage? initialMessage = RemoteMessage? initialMessage =
await FirebaseMessaging.instance.getInitialMessage(); await FirebaseMessaging.instance.getInitialMessage();
if (initialMessage != null) { if (initialMessage != null) {
navigate(); navigate();
@ -56,33 +60,55 @@ class _AgentNavigationState extends State<AgentNavigation> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( final isLargeScreen = MediaQuery.of(context).size.width > 640;
body: Stack(
children: [ final content = Stack(
Offstage( children: [
offstage: _view != 0, Offstage(
child: const DashboardScreen(), offstage: _view != 0,
), child: const DashboardScreen(),
Offstage( ),
offstage: _view != 1, Offstage(
child: const NotificationScreen(), offstage: _view != 1,
), child: const NotificationScreen(),
Offstage( ),
offstage: _view != 2, Offstage(
child: const AccountScreen(), offstage: _view != 2,
) child: const AccountScreen(),
], )
), ],
bottomNavigationBar: NavigationBar(
selectedIndex: _view,
labelBehavior: NavigationDestinationLabelBehavior.onlyShowSelected,
destinations: AgentNavigation.items,
onDestinationSelected: (index) {
setState(() {
_view = index;
});
},
),
); );
if (isLargeScreen) {
return Row(children: <Widget>[
NavigationRail(
selectedIndex: _view,
groupAlignment: 0,
onDestinationSelected: (int index) {
setState(() {
_view = index;
});
},
labelType: NavigationRailLabelType.all,
destinations: widget.railDestinations,
),
const VerticalDivider(thickness: 1, width: 1),
Expanded(child: content),
]);
} else {
return Scaffold(
body: content,
bottomNavigationBar: BottomNavigationBar(
currentIndex: _view,
showUnselectedLabels: false,
items: widget.bottomDestinations,
onTap: (index) {
setState(() {
_view = index;
});
},
),
);
}
} }
} }

View File

@ -1,11 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:goatagent/auth.dart'; import 'package:goatagent/auth.dart';
import 'package:goatagent/firebase.dart'; import 'package:goatagent/firebase.dart';
import 'package:goatagent/screens/about.dart';
import 'package:goatagent/screens/account.dart';
import 'package:goatagent/screens/dashboard.dart';
import 'package:goatagent/screens/notifications.dart';
import 'layouts/navigation.dart'; import 'layouts/navigation.dart';
@ -50,7 +45,7 @@ class GoatAgent extends StatelessWidget {
), ),
useMaterial3: true, useMaterial3: true,
), ),
home: const AgentNavigation(), home: AgentNavigation(),
); );
} }
} }