Skip to content

Instantly share code, notes, and snippets.

@ninogjoni
Created December 17, 2021 10:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ninogjoni/188296f8524e1b8214b7e6c14b1eacc1 to your computer and use it in GitHub Desktop.
Save ninogjoni/188296f8524e1b8214b7e6c14b1eacc1 to your computer and use it in GitHub Desktop.
Material You NavBar solution with bloc
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
part 'navigation_event.dart';
part 'navigation_state.dart';
class NavigationBloc extends Bloc<NavigationEvent, NavigationState> {
NavigationBloc() : super(const NavMealSteate(1)) {
on<NavHomeEvent>(_onHome);
on<NavMealEvent>(_onMeal);
on<NavWorkoutEvent>(_onWorkout);
}
void _onHome(NavHomeEvent event, Emitter<NavigationState> emit) {
emit(NavHomeState(event.index));
}
void _onMeal(NavMealEvent event, Emitter<NavigationState> emit) {
emit(NavMealSteate(event.index));
}
void _onWorkout(NavWorkoutEvent event, Emitter<NavigationState> emit) {
emit(NavWorkoutState(event.index));
}
}
part of 'navigation_bloc.dart';
@immutable
abstract class NavigationEvent extends Equatable {
const NavigationEvent();
@override
List<Object> get props => [];
}
class NavHomeEvent extends NavigationEvent {
const NavHomeEvent({required this.index});
final int index;
}
class NavMealEvent extends NavigationEvent {
const NavMealEvent({required this.index});
final int index;
}
class NavWorkoutEvent extends NavigationEvent {
const NavWorkoutEvent({required this.index});
final int index;
}
part of 'navigation_bloc.dart';
@immutable
abstract class NavigationState extends Equatable {
const NavigationState(this.index);
final int index;
@override
List<Object> get props => [index];
}
class NavigationInitial extends NavigationState {
const NavigationInitial(int index) : super(index);
}
class NavHomeState extends NavigationState {
const NavHomeState(int index) : super(index);
}
class NavMealSteate extends NavigationState {
const NavMealSteate(int index) : super(index);
}
class NavWorkoutState extends NavigationState {
const NavWorkoutState(int index) : super(index);
}
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