在Flutter中获取脚手架底部导航栏的高度

you*_*ani 6 flutter

我怎样才能获得heightBottomNavigationBarScaffold在颤振?我知道这里有MediaQuery.of(context).size屏幕尺寸。有类似的方法BottomNavigationBar吗?

wow*_*lol 11

Container(
   width: MediaQuery.of(context).size.width,
   height: kBottomNavigationBarHeight,
   child: Scaffold(
       backgroundColor: Colors.transparent,
       body: null,
       bottomNavigationBar: BottomNavigationBar()
  )
)
Run Code Online (Sandbox Code Playgroud)

这将创建一个只有底部导航栏小部件有足够空间的脚手架。

kBottomNavigationBarHeight 是一个常量,可以在constants.dart 文件中找到。

  • `kBottomNavigationBarHeight` 是一个常量,但这并没有考虑到与没有主页按钮的 iPhone 或使用最新导航栏的 Android 相关的 `SafeArea` 填充的潜力。 (3认同)

And*_*sky 9

要获取小部件的大小,您可以使用keyfield

final key = GlobalKey();
... set key field of widget
double height = key.currentContext.size.height;
Run Code Online (Sandbox Code Playgroud)


Rad*_*pur 8

如果您想知道具有底部导航栏的主支架的子屏幕之一中底部导航栏的高度,您可以使用 MediaQuery:

final bottomPadding = MediaQuery.of(context).padding.bottom;
Run Code Online (Sandbox Code Playgroud)

除了 SafeArea 之外,MediaQuery 的底部填充还考虑了 BottomNavigationBar 的高度。

更详细:

@override
  Widget build(BuildContext context) {

    final bottomPadding = MediaQuery.of(context).padding.bottom; // From here you will get only SafeArea padding.

    return Scaffold(
      body: PageView(
        children: const [

          // But in build() method of each of these screens you will get
          // SafeArea padding with bottomNavigationBar height
          // just by calling MediaQuery.of(context).padding.bottom;

          FirstScreen(), 
          SecondScreen(),
          ThirdScreen(),
          FourthScreen(),

        ],
      ),
      bottomNavigationBar: MyBottomNavigationBar(),
    );
  }
Run Code Online (Sandbox Code Playgroud)

  • 这为我返回了 0,而我有一个底部“BottomNavigationBar”。对我来说不是一个解决方案。 (5认同)