relatica/lib/controls/app_bottom_nav_bar.dart

127 wiersze
3.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../riverpod_controllers/account_services.dart';
import '../riverpod_controllers/notification_services.dart';
import '../routes.dart';
enum NavBarButtons {
2022-12-08 19:23:11 +00:00
timelines,
notifications,
contacts,
explore,
}
class AppBottomNavBar extends ConsumerWidget {
final NavBarButtons currentButton;
final Function()? onHomeButtonReclick;
const AppBottomNavBar({
super.key,
required this.currentButton,
this.onHomeButtonReclick,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final profile = ref.watch(activeProfileProvider);
ref.watch(notificationsManagerProvider(profile));
final hasNotifications = ref.read(hasNotificationsProvider(profile, false));
return BottomNavigationBar(
onTap: (index) {
final newButton = _indexToButton(index);
if (newButton == currentButton) {
if (newButton == NavBarButtons.timelines &&
onHomeButtonReclick != null) {
onHomeButtonReclick!();
}
return;
}
switch (newButton) {
2022-12-08 19:23:11 +00:00
case NavBarButtons.timelines:
context.go(ScreenPaths.timelines);
break;
case NavBarButtons.notifications:
context.pushNamed(ScreenPaths.notifications);
break;
case NavBarButtons.contacts:
2022-12-15 03:27:30 +00:00
context.pushNamed(ScreenPaths.contacts);
break;
case NavBarButtons.explore:
context.pushNamed(ScreenPaths.explore);
break;
}
},
type: BottomNavigationBarType.fixed,
currentIndex: _buttonToIndex(currentButton),
2023-01-20 01:37:35 +00:00
items: _menuItems(context, 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.contacts:
return 2;
case NavBarButtons.explore:
return 3;
}
}
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.contacts;
}
if (index == 3) {
return NavBarButtons.explore;
}
throw ArgumentError('$index has no button type');
}
2023-03-14 03:49:52 +00:00
List<BottomNavigationBarItem> _menuItems(
BuildContext context, 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: 'Contacts',
icon: Icon(Icons.people_outline),
activeIcon: Icon(Icons.people_sharp),
),
const BottomNavigationBarItem(
label: 'Explore',
icon: Icon(Icons.explore_outlined),
activeIcon: Icon(Icons.explore),
),
];
}
}