Created
December 17, 2021 10:36
-
-
Save ninogjoni/188296f8524e1b8214b7e6c14b1eacc1 to your computer and use it in GitHub Desktop.
Material You NavBar solution with bloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RootPage extends StatelessWidget { | |
const RootPage({Key? key}) : super(key: key); | |
@override | |
@override | |
Widget build(BuildContext context) { | |
return BlocProvider( | |
create: (_) => NavigationBloc(), | |
child: const RootView(), | |
); | |
} | |
} | |
class RootView extends StatelessWidget { | |
const RootView({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: BlocBuilder<NavigationBloc, NavigationState>( | |
buildWhen: (prev, state) => prev.runtimeType != state.runtimeType, | |
builder: (context, state) { | |
if (state is NavHomeState) { return const HomePage(); } | |
if (state is NavMealSteate) { return const MealPage(); } | |
if (state is NavWorkoutState) { return const WorkoutPage(); } | |
return Container(); | |
}, | |
), | |
bottomNavigationBar: BlocBuilder<NavigationBloc, NavigationState>( | |
builder: (_, state) { | |
//Material You NavigationBar Widget | |
return NavigationBar( | |
height: 65, | |
selectedIndex: state.index, | |
labelBehavior: NavigationDestinationLabelBehavior.alwaysShow, | |
animationDuration: const Duration(seconds: 1), | |
onDestinationSelected: (index) { | |
switch (index) { | |
case 0: context.read<NavigationBloc>().add(NavHomeEvent(index: index)); | |
break; | |
case 1: context.read<NavigationBloc>().add(NavMealEvent(index: index)); | |
break; | |
case 2: context.read<NavigationBloc>().add(NavWorkoutEvent(index: index)); | |
break; | |
} | |
}, | |
destinations: const [ | |
NavigationDestination( | |
icon: Icon(Icons.home), | |
label: 'Home', | |
selectedIcon: Icon(Icons.home_outlined), | |
), | |
NavigationDestination( | |
icon: Icon(Icons.restaurant), | |
label: 'Meal', | |
selectedIcon: Icon(Icons.restaurant_outlined), | |
), | |
NavigationDestination( | |
icon: Icon(Icons.fitness_center), | |
label: 'Workout', | |
selectedIcon: Icon(Icons.fitness_center_outlined), | |
), | |
], | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment