乐闻世界logo
搜索文章和话题

Flutter: How to Get the Status Bar Height?

2月7日 11:20

In Flutter, you can retrieve the status bar height using the MediaQuery class. The steps are as follows:

  1. First, ensure your widget has access to the BuildContext.
  2. Use MediaQuery.of(context) to obtain the current MediaQueryData.
  3. From the MediaQueryData, access the status bar height via the padding.top property.

Here is an example:

dart
import 'package:flutter/material.dart'; class MyWidget extends StatelessWidget { Widget build(BuildContext context) { double statusBarHeight = MediaQuery.of(context).padding.top; return Scaffold( appBar: AppBar(title: Text("Status Bar Height")), body: Center( child: Text("Status bar height is: $statusBarHeight pixels"), ), ); } }

In this example, we first retrieve the status bar height using MediaQuery.of(context).padding.top and display it on the screen. This method works consistently across different devices and platforms, including iOS and Android.

标签:Dart