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
2024-02-10 20:08:25 +08:00

89 lines
2.0 KiB
Dart

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:goatagent/screens/account.dart';
import 'package:goatagent/screens/dashboard.dart';
import 'package:goatagent/screens/notifications.dart';
class AgentNavigation extends StatefulWidget {
const AgentNavigation({super.key});
static const items = [
NavigationDestination(
icon: Icon(Icons.home),
label: 'Dashboard',
),
NavigationDestination(
icon: Icon(Icons.notifications),
label: 'Notifications',
),
NavigationDestination(
icon: Icon(Icons.account_circle),
label: 'Account',
)
];
@override
State<AgentNavigation> createState() => _AgentNavigationState();
}
class _AgentNavigationState extends State<AgentNavigation> {
int _view = 0;
Future<void> initMessage(BuildContext context) async {
void navigate() {
setState(() {
_view = 1;
});
}
RemoteMessage? initialMessage =
await FirebaseMessaging.instance.getInitialMessage();
if (initialMessage != null) {
navigate();
}
FirebaseMessaging.onMessageOpenedApp.listen((event) {
navigate();
});
}
@override
void initState() {
super.initState();
initMessage(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Offstage(
offstage: _view != 0,
child: const DashboardScreen(),
),
Offstage(
offstage: _view != 1,
child: const NotificationScreen(),
),
Offstage(
offstage: _view != 2,
child: const AccountScreen(),
)
],
),
bottomNavigationBar: NavigationBar(
selectedIndex: _view,
labelBehavior: NavigationDestinationLabelBehavior.onlyShowSelected,
destinations: AgentNavigation.items,
onDestinationSelected: (index) {
setState(() {
_view = index;
});
},
),
);
}
}