This repository has been archived on 2024-06-08. You can view files and clone it, but cannot push or open issues or pull requests.
SolarAgent/lib/layouts/navigation.dart

115 lines
2.9 KiB
Dart
Raw Normal View History

2024-02-08 07:19:37 +00:00
import 'package:firebase_messaging/firebase_messaging.dart';
2024-02-07 17:25:58 +00:00
import 'package:flutter/material.dart';
2024-02-10 12:08:25 +00:00
import 'package:goatagent/screens/account.dart';
import 'package:goatagent/screens/dashboard.dart';
import 'package:goatagent/screens/notifications.dart';
2024-02-07 17:25:58 +00:00
class AgentNavigation extends StatefulWidget {
2024-02-10 14:18:49 +00:00
AgentNavigation({super.key});
2024-02-07 17:25:58 +00:00
static const items = [
2024-02-10 14:18:49 +00:00
{'label': 'Dashboard', 'icon': Icon(Icons.home)},
{'label': 'Notifications', 'icon': Icon(Icons.notifications)},
{'label': 'Account', 'icon': Icon(Icons.account_circle)},
2024-02-07 17:25:58 +00:00
];
2024-02-10 14:18:49 +00:00
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();
2024-02-07 17:25:58 +00:00
@override
State<AgentNavigation> createState() => _AgentNavigationState();
}
class _AgentNavigationState extends State<AgentNavigation> {
2024-02-10 12:08:25 +00:00
int _view = 0;
2024-02-07 17:25:58 +00:00
2024-02-08 07:19:37 +00:00
Future<void> initMessage(BuildContext context) async {
void navigate() {
setState(() {
2024-02-10 12:08:25 +00:00
_view = 1;
2024-02-08 07:19:37 +00:00
});
}
RemoteMessage? initialMessage =
2024-02-10 14:18:49 +00:00
await FirebaseMessaging.instance.getInitialMessage();
2024-02-08 07:19:37 +00:00
if (initialMessage != null) {
navigate();
}
FirebaseMessaging.onMessageOpenedApp.listen((event) {
navigate();
});
}
@override
void initState() {
super.initState();
initMessage(context);
}
2024-02-07 17:25:58 +00:00
@override
Widget build(BuildContext context) {
2024-02-10 14:18:49 +00:00
final isLargeScreen = MediaQuery.of(context).size.width > 640;
final content = Stack(
children: [
Offstage(
offstage: _view != 0,
child: const DashboardScreen(),
),
Offstage(
offstage: _view != 1,
child: const NotificationScreen(),
),
Offstage(
offstage: _view != 2,
child: const AccountScreen(),
)
],
2024-02-07 17:25:58 +00:00
);
2024-02-10 14:18:49 +00:00
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;
});
},
),
);
}
2024-02-07 17:25:58 +00:00
}
}