So starting out I have this simple bottomNavBar which changes pages. What I wanted to do was add a FAB bar in the middle.
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: 0,
backgroundColor: Color(0xFF0066EE),
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white,
items: const [
BottomNavigationBarItem(label: 'Home', icon: Icon(Icons.home)),
BottomNavigationBarItem(label: 'Diary', icon: Icon(Icons.book)),
BottomNavigationBarItem(
label: 'Plans', icon: Icon(Icons.spoke_rounded)),
BottomNavigationBarItem(label: 'More', icon: Icon(Icons.more)),
],
onTap: (index) {
if (index == 1) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
final foodItemsModel =
Provider.of<FoodItemsModel>(context, listen: false);
return MealPlanPage(mealList: foodItemsModel.foodItems);
},
),
);
} else if (index == 2) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return PlanPage();
},
),
);
}
},
),
My attempt of including the FAB bar involved wrapping the existing bottomNavBar in a bottomAppBar and then adding the floatingActionButton, the reason was because I assumed it was caused by BottomAppBar having a height that is larger than the bottomNavBar.
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: 0,
backgroundColor: Color(0xFF0066EE),
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white,
items: const [
BottomNavigationBarItem(label: 'Home', icon: Icon(Icons.home)),
BottomNavigationBarItem(label: 'Diary', icon: Icon(Icons.book)),
BottomNavigationBarItem(
label: 'Plans', icon: Icon(Icons.spoke_rounded)),
BottomNavigationBarItem(label: 'More', icon: Icon(Icons.more)),
],
onTap: (index) {
if (index == 1) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
final foodItemsModel =
Provider.of<FoodItemsModel>(context, listen: false);
return MealPlanPage(mealList: foodItemsModel.foodItems);
},
),
);
} else if (index == 2) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return PlanPage();
},
),
);
}
},
),
),
I tried setting the height of the bottomAppBar to match the height of the bottomNavBar but just ended up with a mess and improper FAB button placement still.
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
color: Color(0xFF0066EE),
elevation: 0,
shape: CircularNotchedRectangle(),
child: SizedBox(
height: kBottomNavigationBarHeight,
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: 0,
backgroundColor: Color(0xFF0066EE),
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white,
items: const [
BottomNavigationBarItem(label: 'Home', icon: Icon(Icons.home)),
BottomNavigationBarItem(label: 'Diary', icon: Icon(Icons.book)),
BottomNavigationBarItem(
label: 'Plans', icon: Icon(Icons.spoke_rounded)),
BottomNavigationBarItem(label: 'More', icon: Icon(Icons.more)),
],
onTap: (index) {
if (index == 1) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
final foodItemsModel =
Provider.of<FoodItemsModel>(context, listen: false);
return MealPlanPage(mealList: foodItemsModel.foodItems);
},
),
);
} else if (index == 2) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return PlanPage();
},
),
);
}
},
),
),
),
2
Answers
In android there’s no problem with it. I think you are using ios.
This can happen when you have
SafeArea
if you have it setbottom
property tofalse
.Or if you don’t have
SafeArea
, try to set notchMargin property of BottomAppBar to 0.example:
Check your main
MaterialApp
or theParent Widget
if there is anymargin
orPadding
set, Because otherwise your code should work.Working Code:
Output: