-
-
Save roipeker/ade71dde5cadb746aec41630c121e86c to your computer and use it in GitHub Desktop.
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)), | |
); | |
} | |
} |
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!
Usually bottom navbar
sticks 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).
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.
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...
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 👍
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
.