Flutter Get Bottom Navigation Bar Height
In Flutter, the bottom navigation bar height can be obtained using the following steps:
- Wrap the bottom navigation bar inside a widget such as a MediaQuery.
- Use the MediaQuery to retrieve the size information and extract the height of the bottom navigation bar.
Example:
Assume we have a bottom navigation bar widget called MyBottomNavigationBar
:
import 'package:flutter/material.dart';
class MyBottomNavigationBar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Business',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'School',
),
],
);
}
}
You can get the height of the bottom navigation bar by wrapping it inside a MediaQuery
widget and accessing the size information:
import 'package:flutter/material.dart';
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My App'),
),
body: Column(
children: [
Expanded(
child: Container(
child: Placeholder(),
),
),
MediaQuery.of(context).padding.bottom != 0
? Container(
height: MediaQuery.of(context).size.height *
0.07,
child: MyBottomNavigationBar(),
)
: Container(),
],
),
);
}
}
In this example, the MyHomePage
widget contains a Column
with two children:
- The first child is an
Expanded
container that represents the main content of the page. - The second child checks if the bottom padding of the media query is not equal to zero (indicating the presence of a soft navigation bar), and if so, it includes the
MyBottomNavigationBar
widget wrapped inside a container with a height calculated based on a percentage of the screen height. If the bottom padding is zero, an empty container is shown.
By using MediaQuery.of(context).size.height
with a percentage value such as 0.07
, you can adjust the height of the bottom navigation bar to be relative to the screen height.
- Html table javascript add column dynamically?
- Flutter gesturedetector pass through
- Flutter expanded border
- Flutter failed to delete a directory at “build”. the flutter tool cannot access the file or directory. please ensure that the sdk and/or project is installed in a location that has read/write permissions for the current user.
- Flutter failed host lookup: (os error: no address associated with hostname, errno = 7)
- Flutter get asset file path