ChatGPT解决这个技术问题 Extra ChatGPT

How to get AppBar height in Flutter?

How can I get the height of an AppBar in Flutter? I am using the MarialApp Widget ('package:flutter/material.dart'). I have the height of my Context and would like to deduct the height of the appbar.

final double height = MediaQuery.of(context).size.height;
You can get the height of any Widget in Flutter and AppBar is a regular Widget.
@creativecreatorormaybenot OP asked HOW to get the height of the AppBar and you just said you can get height of the AppBar.

c
creativecreatorormaybenot

This is not an ideal way, I think, but it will work.

Firstly declare the AppBar widget that you will use in your Scaffold.

Widget demoPage() {
  AppBar appBar = AppBar(
    title: Text('Demo'),
  );
  return Scaffold(
    appBar: appBar,
    body: /*
    page body
    */,
  );
}

Now you can get the height of your appBar using its preferredSized:

double height = appBar.preferredSize.height;

You can use this height to reduce from the screen height.

final double height = MediaQuery.of(context).size.height;

I don't really get how to reduce appBar.preferredSize.height. Is that possible? thanks a lot. I tried to change the value of kToolbarHeight, but failed due to it's a const var.
You can simply use AppBar().preferredSize.height If you find it to be neater. It will produce the same result as the above but will be creating another instance of AppBar
bonus knowledge:- deduct the height of status bar by using MediaQuery.of(context).padding.top
appBar.preferredSize.height = kToolbarHeight = 56.0 but MediaQuery.of(context).padding.top + kToolbarHeight is exactly what I want to get.
Just remember that if you are inside a scaffold, it might override MediaQuery.padding - so you'll just get 0. Instead use : Scaffold.of(context).appBarMaxHeight; credits go to @na2axl !
v
vitaminwater

you can use this :

var height = AppBar().preferredSize.height;

this way is very sample and easy


I like this answer. Thanks!
n
na2axl

There is no need to store the AppBar instance, or to create a dummy one to get the height. Also, AppBar.preferredSize will always return the same value of 56.0, which is the standard of Material Design, and this value will not be always usable for some cases (it lacks the height of the status bar for example).

Since an AppBar is surely used with a Scaffold, IMHO, the smarter way to get the real AppBar height (the distance between the top of the device and the top of the Scaffold body) is to use:

double height = Scaffold.of(context).appBarMaxHeight;

With this, the computed height will include (independently of the platform):

The status bar height

The app bar height

The bottom app bar widget height if any (ex. TabBar)

Hope this will help!


How does that help you get the bottom bar height as well? I don't think my bottom bar and app bar are the same height.
Hi @Graham, by "bottom app bar widget", I mean the widget who is placed at the bottom of the AppBar (with the Appbar.bottom property), a TabBar for exemple. I don't mean the BottomAppBar widget who is placed at the bottom of the Scaffold.
For me this just returns 56 which is not the appbar+statusbar.
This is the only viable solution. Scaffold will override MediaQuery, and you will keep getting padding.top == 0 all the time.
May i ask how u reach scafold when ur building appBar as separate class/widget?
D
Daniel Illescas

There is a constant for the normal toolbar height: kToolbarHeight


it says 56, and it's too short
@AlirezaAkbari Because you need to also deduct MediaQuery.of(context).padding.top thanks to Avnish Kumar
H
Hellomik U

Just watch in AppBar

MediaQuery.of(context).padding.top + kToolbarHeight

K
Kamlesh

Get body height:

MediaQuery.of(context).size.height - (MediaQuery.of(context).padding.top + kToolbarHeight)

I hope, this solution will also help you. Thanks to ask this question.


awesome, smart solution for get height of body. thankyou brother!
MediaQuery.of(context).padding.top -> it gives status bar height, kToolbarHeight -> it gives toolbar height, Thanks :)
S
Sebastian

AppBar has a fixed height of 56.

You should create your own appbar implementing PreferredSizeWidget

class TestAppBar extends StatelessWidget implements PreferredSizeWidget {
  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: Text('TEST'),
      centerTitle: true,
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(34);
}

g
goops17

You can get height of AppBar by using:

double height = appBar.preferredSize.height;

Make sure you have declared AppBar widget.


M
Mehmet Emre Öz

You can use this:

  @override
  final Size preferredSize; // default is 56.0

  WidgetAppBar({@required this.appBarTitle, @required this.appBarColor, Key key}) : preferredSize = Size.fromHeight(40), super(key: key);

Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review
m
marc_s

Use preferred size

//defined as
Size preferredSize

preferred size is a size whose height is the sum of kToolbarHeight and the bottom widget's preferred height.

Scaffold uses this size to set its app bar's height.

It is defined like below in app bar class which implement PreferredSizeWidget

 preferredSize = new Size.fromHeight(kToolbarHeight + (bottom?.preferredSize?.height ?? 0.0))

a link for example ...

https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/app_bar.dart


H
Hardik Hirpara

This will give height of appbar or statusbar

var height = MediaQuery.of(context).padding.top;

However, this will work only in mobile devices, other devices don't have status bar