Last active
January 2, 2023 15:15
-
-
Save idan054/0f379b21ff4446dd61dfe0573aa8a8de to your computer and use it in GitHub Desktop.
A Working BottomNavigationBar (Just use it on main.dart)
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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:get/get_state_manager/src/simple/get_controllers.dart'; | |
import 'package:get/get_state_manager/src/simple/get_state.dart'; | |
import 'package:rilmit_streamit/feed_screen/feed_mainScreen.dart'; | |
class MainDashboard extends StatefulWidget { | |
@override | |
_MainDashboardState createState() => _MainDashboardState(); | |
} | |
class _MainDashboardState extends State<MainDashboard> { | |
int _selectedIndex = 0; | |
final PageController _pageController = PageController(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: PageView( | |
controller: _pageController, | |
children: const <Widget>[ | |
Scaffold(), | |
FeedMainScreen(), | |
], | |
// physics: const NeverScrollableScrollPhysics(), // disable swipe | |
onPageChanged: (pageIndex) { // On Swipe left or right | |
setState(() => _selectedIndex = pageIndex); | |
}, | |
), | |
bottomNavigationBar: BottomNavigationBar( | |
selectedItemColor: Colors.orange, | |
currentIndex: _selectedIndex, | |
onTap: (value) { // onTap Bottom icon | |
setState(() => _selectedIndex = value); | |
_pageController.jumpToPage(value); | |
}, | |
items: [ | |
BottomNavigationBarItem( | |
icon: Icon(Icons.chat), title: Text('Shop')), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.home), title: Text('Home')), | |
], | |
), | |
); | |
} | |
} |
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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:get/get_core/src/get_main.dart'; | |
import 'package:get/get_state_manager/src/simple/get_controllers.dart'; | |
import 'package:get/get_instance/src/extension_instance.dart'; | |
import 'package:get/get_state_manager/src/simple/get_state.dart'; | |
import 'feed_screen/feed_mainScreen.dart'; | |
class DashboardController extends GetxController{ | |
int selectedIndex = 0; | |
PageController pageController = PageController(); | |
void changeIndex(value){ | |
selectedIndex = value; | |
// To notify GetBuilder | |
update(); | |
} | |
} | |
class MainDashboardNew extends StatelessWidget { | |
MainDashboardNew({Key? key}) : super(key: key); | |
final DashboardController dashboardController = Get.put(DashboardController()); | |
@override | |
Widget build(BuildContext context) { | |
return GetBuilder<DashboardController>( | |
builder: (_) { | |
PageController _pageController = dashboardController.pageController; | |
int _selectedIndex = dashboardController.selectedIndex; | |
return Scaffold( | |
body: PageView( | |
controller: _pageController, | |
children: const <Widget>[ | |
Scaffold(backgroundColor: Colors.green,), | |
FeedMainScreen(), | |
], | |
// physics: const NeverScrollableScrollPhysics(), // disable swipe | |
onPageChanged: (pageIndex) { // On Swipe left or right | |
dashboardController.changeIndex(pageIndex); | |
}, | |
), | |
bottomNavigationBar: BottomNavigationBar( | |
selectedItemColor: Colors.orange, | |
currentIndex: _selectedIndex, | |
onTap: (value) { // onTap Bottom icon | |
dashboardController.changeIndex(value); | |
_pageController.jumpToPage(value); | |
}, | |
items: const [ | |
BottomNavigationBarItem( | |
icon: Icon(Icons.chat), title: Text('Shop')), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.home), title: Text('Home')), | |
], | |
), | |
); | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment