Created
August 27, 2020 13:28
-
-
Save roipeker/ade71dde5cadb746aec41630c121e86c to your computer and use it in GitHub Desktop.
GetX minimal sample bottom navbar.
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
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)), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 👍