I created a separate file for bottom navigation bar and included the three screens which is to be included in bottom navigation bar .
class _bottomnavscreen extends State<bottomnavscreen> {
int _selectedIndex = 0;
List<Widget> pageList = [home(), create(), profile()];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.add_circle_outline_sharp),
label: 'Create',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
body: pageList.elementAt(_selectedIndex),
);
}
I put this bottomnavscreen as the home in main.dart:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: bottomnavscreen(),
);
}
}
But this bootomnavigation widget is seen in my detailedpost screen and comment screen.
detailedpost screen is pushed from home() through Navigation.push()
Comment screen is pushed from postdetails() through
Navigation.push()
How can I hide this bottom navigation widget in my comment screen and detailedpost screen?
This is how I push to detailpost screen from home()
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => detailpost(
body: document['body'],
title: document['title'],
date: document['date'],
)),
);
You can add condition for specific index like this :
class _bottomnavscreen extends State<bottomnavscreen> {
int _selectedIndex = 0;
List<Widget> pageList = [home(), create(), profile()];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: _selectedIndex == 1 ? Container() : BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.add_circle_outline_sharp),
label: 'Create',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
body: pageList.elementAt(_selectedIndex),
);
}
You should start a base page from the MyApp and add BottomNavigations in that page only.
Now when you navigate to detailedpost screen and comment screen, the BottomNavigations will not be visible.
you can use the offstage property to hide the bottom navigation bar on specific pages by wrapping it in an Offstage widget and setting the offstage property to true:
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _currentIndex = 0;
final List<Widget> _pages = [ HomePage(), SettingsPage(), ];
#override
Widget build(BuildContext context) {
return Scaffold(
body: _pages[_currentIndex],
bottomNavigationBar: Offstage(
offstage: _currentIndex == 1,
child: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: Text('Settings'),
),
],
),
),
);
}
}
Related
I am creating a dynamic list of BottomNavigationBarItem and assigning it to 'items' of BottomNavigationBar. So based on a condition (here is my case, checks and add one more BottomNavigationBarItem if it is not billed. So the number of Icons displayed change.
Normally for a fixed number of items, ontap provide index of the icon tapped. As the order/sequence of icons change, their index also differs.
Now how do I read the label of selected BottomNavigationBarItem and respond in onTap handler instead of tapped index value ?
(Of course in this particular case, I can add the additional button as last one and done with. Need a better solution.)
List<BottomNavigationBarItem> getNavbarItems() {
List<BottomNavigationBarItem> navItems = [];
navItems.add(
const BottomNavigationBarItem(
icon: Icon(Icons.delete_outline,), label: 'Delete',),
);
if (widget.invStatus.isBilled == 0) {
navItems.add(
const BottomNavigationBarItem(
icon: Icon(Icons.receipt), label: 'Bill it ?'),
);
}
navItems.add(
const BottomNavigationBarItem(
icon: Icon(Icons.category_rounded), label: 'Add a Product !'),
);
return navItems;
}
Thanks.
If I understand you correctly, you can use the following approach to get the "label".
Call:
getNavbarItems()[index].label
And have a button to dynamically toggle isBilled.
Complete example:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Nav(),
),
),
);
}
}
class Nav extends StatefulWidget {
#override
createState() => NavState();
}
class NavState extends State<Nav> {
var _selectedIndex = 0;
var billIt = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Nav'),
),
body: Center(
child: TextButton(
child: Text('Toggle `billIt`'),
onPressed: () {
setState(() {
billIt = !billIt;
});
},
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedIndex,
items: getNavbarItems(),
onTap: (int index) {
setState(() {
_selectedIndex = index;
print('label: ${getNavbarItems()[index].label}');
});
},
),
);
}
List<BottomNavigationBarItem> getNavbarItems() {
List<BottomNavigationBarItem> navItems = [];
navItems.add(
const BottomNavigationBarItem(
icon: Icon(
Icons.delete_outline,
),
label: 'Delete',
),
);
if (billIt) {
navItems.add(
const BottomNavigationBarItem(
icon: Icon(Icons.receipt), label: 'Bill it ?'),
);
}
navItems.add(
const BottomNavigationBarItem(
icon: Icon(Icons.category_rounded), label: 'Add a Product !'),
);
return navItems;
}
}
is there a way to give the first item of bottom navigtaion bar multi pages ? i tried to make multi widgets on the first item class and call the widgets on button onPressed but the bottom navigation bar disappears,
I'm not sure what you exactly want to implement .but you can try this if it works for you.
class TabsScreen extends StatefulWidget {
static const routeName = 'tab-screen';
#override
_TabsScreenState createState() => _TabsScreenState();
}
class _TabsScreenState extends State<TabsScreen> {
int selectedIndex = 0;
List<Map> tabPages = [
{
'page': FirstScreen(),
},
{
'page': SecondScreen(),
},
{
'page': ThirdScreen(),
}
];
void _selectPage(int index) {
setState(() {
selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: tabPages[selectedIndex]['page'],
bottomNavigationBar: BottomNavigationBar(
selectedItemColor: Colors.orange,
unselectedItemColor: Colors.grey,
currentIndex: selectedIndex,
onTap: _selectPage,
backgroundColor: Colors.grey[300],
items: [
BottomNavigationBarItem(
icon: Icon(
Icons.home,
size: 30,
),
label: 'FirstScreen'),
BottomNavigationBarItem(
icon: Icon(
Icons.restaurant,
size: 30,
),
label: 'SecondScreen'),
BottomNavigationBarItem(
icon: Icon(
Icons.settings,
size: 30,
),
label: 'ThirdScreen'),
],
),
);
}
}
I have a main.dart with bottomNavigationBar with 5 tabs and when clicked on tabs it goes to the respective pages.
void main() => runApp(MyStatefulWidget());
class MyStatefulWidget extends StatefulWidget {
#override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
final List<Widget> _children = [
ProfilePage1(),
EventPage3(),
HomePage2(),
AllEventPage(),
ProfilePage1(),
];
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<EventModifier>(
create: (context) => EventModifier()),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(child: _children.elementAt(_selectedIndex)),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("")),
BottomNavigationBarItem(
icon: Icon(Icons.calendar_today), title: Text("")),
BottomNavigationBarItem(
icon: Icon(
Icons.account_circle,
size: 45,
color: Color(0xFF334192),
),
title: Text("")),
BottomNavigationBarItem(
icon: Icon(Icons.message), title: Text("")),
BottomNavigationBarItem(
icon: Icon(Icons.table_chart), title: Text("")),
],
currentIndex: _selectedIndex,
selectedItemColor: Color(0xFF334192),
unselectedItemColor: Colors.grey,
onTap: _onItemTapped,
),
),
),
);
}
}
Now, in the 3rd tab there is a button called "See All" and when clicked it should go to the 4th page With bottomNavigationBar and automatically it should be clicked the 4th tab of the bottomNavigationBar.
How to do that in flutter?flutt
To change the bottomNavigationBar selected item manually you would set
_selectedIndex = 4;
To navigate to the AllEventTab when the ‘See All’ button is clicked... one way is using Navigator.push() in the button’s onPressed(), like this:
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AllEventTab()),
);
}
see “Navigation Basics”
Following is my code which i am using for bottom navigation
class NaviBottom extends StatefulWidget {
#override
_NaviBottomState createState() => _NaviBottomState();
}
class _NaviBottomState extends State<NaviBottom> {
int _currentIndex = 0;
final List<Widget> _children = [
HomeScreen(),
AddProperties(),
MyFavProperties(),
MyProfile(),
Login()
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Open Houze")),
body: _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.blue,
selectedItemColor: Colors.black,
unselectedItemColor: Colors.white,
onTap: onTabTapped,
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.home), title: new Text('First')),
BottomNavigationBarItem(
icon: new Icon(Icons.mail), title: new Text('Second')),
BottomNavigationBarItem(
icon: Icon(Icons.person), title: Text('Third')),
BottomNavigationBarItem(
icon: Icon(Icons.person), title: Text('Forth')),
BottomNavigationBarItem(
icon: Icon(Icons.person), title: Text('Fifith'))
],
),
);
}
void onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
}
In this my first three tab views to be shown with bottom navigation bar whereas when I click on the last two tabs I need to Navigate and show other screens without BottomNavigation bar,
void onTabTapped(int index) {
if(index >= 0 && index < 3)
setState(() {
_currentIndex = index;
});
if(index == 3)
Navigator.push(
context,
MaterialPageRoute(builder: (context) => FourthPage()),
);
if(index == 4)
Navigator.push(
context,
MaterialPageRoute(builder: (context) => FifthPage()),
);
}
N.B : Dart has type inference, meaning that you don't need to annotate the type if it's explicit. So you can just type final _children and remove the List<Widget>.
I have BottomNavigationBar in flutter & called separate widgets for each tab. For example I am having button into that widget which redirects to new screen, I want that BottomNavigationBar should be persisted in new screen as well. I have checked couple of articles but unable to understand. Below is code for BottomTabNavigationBar
import 'package:flutter/material.dart';
import 'package:timeout_dubai/fragments/HomeFragment.dart';
import 'package:timeout_dubai/fragments/MtTimeFragment.dart';
import 'package:timeout_dubai/fragments/MyLocFragment.dart';
import 'package:timeout_dubai/fragments/ThingsLoveFragment.dart';
class LoginScreen extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _HomeState();
}
}
class _HomeState extends State<LoginScreen> {
int _currentIndex = 0;
GlobalKey globalKey = new GlobalKey(debugLabel: 'btm_app_bar');
final List<Widget> _children = [
new HomeFragment(),
new ThingsLoveFragment(),
new MyLocFragment(),
new MyTimeFragment()
];
#override
Widget build(BuildContext context) {
return Scaffold(
body: _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
onTap: onTabTapped,
key: globalKey,
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home,),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
title: Text('Things I Love'),
),
BottomNavigationBarItem(
icon: Icon(Icons.location_on),
title: Text('My Locations'),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('My Time Out')
),
],
),
);
}
void onTabTapped(int index) {
print("Index $index");
setState(() {
_currentIndex = index;
});
}
}