Skip to content

Instantly share code, notes, and snippets.

@roipeker
Created August 27, 2020 13:28
Show Gist options
  • Save roipeker/ade71dde5cadb746aec41630c121e86c to your computer and use it in GitHub Desktop.
Save roipeker/ade71dde5cadb746aec41630c121e86c to your computer and use it in GitHub Desktop.
GetX minimal sample bottom navbar.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class SampleBottomNavDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
themeMode: ThemeMode.dark,
debugShowCheckedModeBanner: false,
theme: ThemeData.light(),
darkTheme: ThemeData.dark().copyWith(primaryColor: Colors.deepPurple),
getPages: [
GetPage(
name: '/',
page: () => SampleBottomNav(),
binding: HomeBinding(),
),
],
);
}
}
class HomeBinding extends Bindings {
@override
void dependencies() => Get.put(HomeController());
}
class HomeController extends GetxController {
final _navIndex = 0.obs;
int get navIndex => _navIndex.value;
void navTo(int idx) => _navIndex.value = idx;
}
final navMenuData = List.generate(5, (index) => 'Page $index');
class SampleBottomNav extends GetView<HomeController> {
final pages = navMenuData.map((e) => PageSample(title: '$e')).toList();
final menuItems = navMenuData
.map((e) => BottomNavigationBarItem(icon: Text(e), label: e))
.toList();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('bottom nav sample'),
centerTitle: false,
),
body: Obx(() => pages[controller.navIndex]),
bottomNavigationBar: Obx(
() => BottomNavigationBar(
items: menuItems,
currentIndex: controller.navIndex,
onTap: controller.navTo,
),
),
);
}
}
class PageSample extends StatelessWidget {
final String title;
const PageSample({Key key, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text(title, style: Get.textTheme.headline3)),
);
}
}
@aligitshow
Copy link

Thanks for the useful snippet.
I'm trying to implement bottom navbar with your sample and then nav to details page from main pages with Get.to() or Get.toNamed().
But in the details page there is no back button in the appbar!
My goal is bottom navbar appears only in main pages like Home, About, Settings. ... and back button appears in detail pages.
Thanks

@roipeker
Copy link
Author

This gists doesn't cover navigation... the back button is by default, inferred in the AppBar:leading argument.
if you need nested navigation like iOS tab navigation... is more complicated, but doable, there's another gists around I think.
This snippet only takes care of changing the selectedItem and body, but never changes the Route.

@aligitshow
Copy link

Wow, Thanks for your quick response!
I checked your other gist:
https://gist.github.com/roipeker/e89e197a2dcd03802a74d214b450ab87
But in that sample bottom navbar also appears in details page!

@roipeker
Copy link
Author

Usually bottom navbarsticks around in subviews... if you wanna get rid of it (and Appbar) for a details page, you should navigate to another page using the root Navigator (don't pass any id in Get.to).

@aligitshow
Copy link

Actually I'm new in Flutter and Get!
I'm really confused by GetPage, onGenerateRoute, Navigator and ...
Tried many approach of this concept but can't get it done.

@roipeker
Copy link
Author

Nested Navigation in Flutter is a little tedious. If you are new to all the ecosystem, you should definitely make some research first. First check how Flutter Navigation works by itself, and then try to accomplish the same with GetX. There's a ton of material in Medium and YouTube about it...

@aligitshow
Copy link

I'm working on this for a week or so.
Watched almost all YouTube tuts about GetX and read every docs and contents about navigation in Flutter and GetX!
But its really confusing or I'm really stupid :D
Thanks for your time 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment