How to get AppBar height in Flutter?

AndroidIosDartFlutterAppbar

Android Problem Overview


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;

Android Solutions


Solution 1 - Android

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;

Solution 2 - Android

you can use this :

var height = AppBar().preferredSize.height;

this way is very sample and easy

Solution 3 - Android

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!

Solution 4 - Android

There is a constant for the normal toolbar height: kToolbarHeight

Solution 5 - Android

Just watch in AppBar

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

Solution 6 - Android

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.

Solution 7 - Android

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);
}

Solution 8 - Android

You can get height of AppBar by using:

double height = appBar.preferredSize.height;

Make sure you have declared AppBar widget.

Solution 9 - Android

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);

Solution 10 - Android

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

Solution 11 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionR2T8View Question on Stackoverflow
Solution 1 - Androidap14View Answer on Stackoverflow
Solution 2 - AndroidMajid SoltaniView Answer on Stackoverflow
Solution 3 - Androidna2axlView Answer on Stackoverflow
Solution 4 - AndroidDaniel IllescasView Answer on Stackoverflow
Solution 5 - AndroidHellomik UView Answer on Stackoverflow
Solution 6 - AndroidKamleshView Answer on Stackoverflow
Solution 7 - AndroidSebastianView Answer on Stackoverflow
Solution 8 - Androidgoops17View Answer on Stackoverflow
Solution 9 - AndroidMehmet Emre ÖzView Answer on Stackoverflow
Solution 10 - Androidkrishank TripathiView Answer on Stackoverflow
Solution 11 - AndroidHardik HirparaView Answer on Stackoverflow