How to get AppBar height in Flutter?

前端 未结 10 1888
长发绾君心
长发绾君心 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:50

    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

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

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

    you can use this :

    var height = AppBar().preferredSize.height;
    

    this way is very sample and easy

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

    There is a constant for the normal toolbar height: kToolbarHeight

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