relatica/lib/controls/app_bottom_nav_bar.dart

135 lines
3.6 KiB
Dart
Raw Normal View History

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import '../routes.dart';
import '../services/notifications_manager.dart';
enum NavBarButtons {
2022-12-08 19:23:11 +00:00
timelines,
notifications,
messages,
contacts,
profile,
}
class AppBottomNavBar extends StatelessWidget {
final NavBarButtons currentButton;
const AppBottomNavBar({super.key, required this.currentButton});
@override
Widget build(BuildContext context) {
final notificationManager = context.watch<NotificationsManager>();
final hasNotifications = notificationManager.notifications.isNotEmpty;
return BottomNavigationBar(
onTap: (index) {
final newButton = _indexToButton(index);
if (newButton == currentButton) {
print('same button do nothing');
return;
}
switch (newButton) {
2022-12-08 19:23:11 +00:00
case NavBarButtons.timelines:
Navigator.of(context).popUntil((route) {
2022-12-08 19:23:11 +00:00
return route.settings.name == ScreenPaths.timelines;
});
break;
case NavBarButtons.notifications:
context.pushNamed(ScreenPaths.notifications);
break;
case NavBarButtons.messages:
// TODO: Handle this case.
break;
case NavBarButtons.contacts:
// TODO: Handle this case.
break;
case NavBarButtons.profile:
context.pushNamed(ScreenPaths.profile);
break;
}
},
type: BottomNavigationBarType.fixed,
selectedItemColor: Colors.black,
unselectedItemColor: Colors.black54,
currentIndex: _buttonToIndex(currentButton),
items: _menuItems(hasNotifications),
);
}
int _buttonToIndex(NavBarButtons button) {
switch (button) {
2022-12-08 19:23:11 +00:00
case NavBarButtons.timelines:
return 0;
case NavBarButtons.notifications:
return 1;
case NavBarButtons.messages:
return 2;
case NavBarButtons.contacts:
return 3;
case NavBarButtons.profile:
return 4;
}
}
NavBarButtons _indexToButton(int index) {
if (index == 0) {
2022-12-08 19:23:11 +00:00
return NavBarButtons.timelines;
}
if (index == 1) {
return NavBarButtons.notifications;
}
if (index == 2) {
return NavBarButtons.messages;
}
if (index == 3) {
return NavBarButtons.contacts;
}
if (index == 4) {
return NavBarButtons.profile;
}
throw ArgumentError('$index has no button type');
}
List<BottomNavigationBarItem> _menuItems(bool hasNotifications) {
return [
const BottomNavigationBarItem(
2022-12-08 19:23:11 +00:00
label: 'Timelines',
icon: Icon(Icons.home_outlined),
activeIcon: Icon(Icons.home_filled),
),
BottomNavigationBarItem(
label: 'Notifications',
icon: Icon(hasNotifications
? Icons.notifications_active_outlined
: Icons.notifications_none_outlined),
activeIcon: Icon(hasNotifications
? Icons.notifications_active
: Icons.notifications),
),
const BottomNavigationBarItem(
label: 'Messages',
icon: Icon(Icons.messenger_outline),
activeIcon: Icon(Icons.messenger),
),
const BottomNavigationBarItem(
label: 'Contacts',
icon: Icon(Icons.people_outline),
activeIcon: Icon(Icons.people_sharp),
),
const BottomNavigationBarItem(
label: 'Profile',
icon: Icon(Icons.person_outline),
activeIcon: Icon(Icons.person),
),
];
}
}