Created
December 22, 2018 11:00
-
-
Save ponnamkarthik/5e54e8522ac555134f5a95919147ff19 to your computer and use it in GitHub Desktop.
BottomNatigationBar with PageView
This file contains hidden or 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'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int bottomSelectedIndex = 0; | |
List<BottomNavigationBarItem> buildBottomNavBarItems() { | |
return [ | |
BottomNavigationBarItem( | |
icon: new Icon(Icons.home), | |
title: new Text('Red') | |
), | |
BottomNavigationBarItem( | |
icon: new Icon(Icons.search), | |
title: new Text('Blue'), | |
), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.info_outline), | |
title: Text('Yellow') | |
) | |
]; | |
} | |
PageController pageController = PageController( | |
initialPage: 0, | |
keepPage: true, | |
); | |
Widget buildPageView() { | |
return PageView( | |
controller: pageController, | |
onPageChanged: (index) { | |
pageChanged(index); | |
}, | |
children: <Widget>[ | |
Red(), | |
Blue(), | |
Yellow(), | |
], | |
); | |
} | |
@override | |
void initState() { | |
super.initState(); | |
} | |
void pageChanged(int index) { | |
setState(() { | |
bottomSelectedIndex = index; | |
}); | |
} | |
void bottomTapped(int index) { | |
setState(() { | |
bottomSelectedIndex = index; | |
pageController.animateToPage(index, duration: Duration(milliseconds: 500), curve: Curves.ease); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: buildPageView(), | |
bottomNavigationBar: BottomNavigationBar( | |
currentIndex: bottomSelectedIndex, | |
onTap: (index) { | |
bottomTapped(index); | |
}, | |
items: buildBottomNavBarItems(), | |
), | |
); | |
} | |
} | |
class Red extends StatefulWidget { | |
@override | |
_RedState createState() => _RedState(); | |
} | |
class _RedState extends State<Red> { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: Colors.red, | |
); | |
} | |
} | |
class Blue extends StatefulWidget { | |
@override | |
_BlueState createState() => _BlueState(); | |
} | |
class _BlueState extends State<Blue> { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: Colors.blueAccent, | |
); | |
} | |
} | |
class Yellow extends StatefulWidget { | |
@override | |
_YellowState createState() => _YellowState(); | |
} | |
class _YellowState extends State<Yellow> { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: Colors.yellowAccent, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment