AppBar height in Flutter

荒凉一梦 提交于 2019-12-09 14:49:55

问题


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;

回答1:


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;



回答2:


There is a constant for the normal toolbar height: kToolbarHeight




回答3:


you can use this :

var height = AppBar().preferredSize.Height;

this way is very sample and easy




回答4:


You can get height of AppBar by using:

double height = appBar.preferredSize.height;

Make sure you have declared AppBar widget.




回答5:


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



来源:https://stackoverflow.com/questions/50075945/appbar-height-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!