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

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