How to get AppBar height in Flutter?

前端 未结 10 1887
长发绾君心
长发绾君心 2020-12-14 14:50

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 C

相关标签:
10条回答
  • 2020-12-14 15:29

    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!

    0 讨论(0)
  • 2020-12-14 15:29

    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);
    }
    
    0 讨论(0)
  • 2020-12-14 15:32

    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);
    
    0 讨论(0)
  • 2020-12-14 15:38

    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.

    0 讨论(0)
  • 2020-12-14 15:45

    Just watch in AppBar

    MediaQuery.of(context).padding.top + kToolbarHeight
    
    0 讨论(0)
  • 2020-12-14 15:45

    You can get height of AppBar by using:

    double height = appBar.preferredSize.height;
    

    Make sure you have declared AppBar widget.

    0 讨论(0)
提交回复
热议问题