Flutter SafeArea - 异形屏适配利器

栏目: ASP.NET · 发布时间: 4年前

内容简介:现在的手机已经不是方方正正的屏幕了,所以我们在写一些UI的时候可能会出现如下问题:为了解决这个问题,Flutter 引入了 SafeArea(安全区域),我们只需要在代码中加入SafeArea可以看到问题已经被解决。

现在的手机已经不是方方正正的屏幕了,所以我们在写一些UI的时候可能会出现如下问题:

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text(widget.title),
    ),
    body: ListView.builder(itemBuilder: (context, index) {
      return SizedBox(
        height: 30,
        child: Text(
          'Data',
          style: TextStyle(fontSize: 18),
        ),
      );
    }),
    floatingActionButton: FloatingActionButton(
      onPressed: _incrementCounter,
      tooltip: 'Increment',
      child: Icon(Icons.add),
    ),
  );
}
复制代码
Flutter SafeArea - 异形屏适配利器

如何解决

为了解决这个问题,Flutter 引入了 SafeArea(安全区域),我们只需要在代码中加入SafeArea

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SafeArea(  // 加入SafeArea
        child: ListView.builder(itemBuilder: (context, index) {
          return Padding(
            padding: EdgeInsets.only(left: 10, bottom: 18),
            child: Text(
              'Data',
              style: TextStyle(fontSize: 18),
            ),
          );
        }),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
复制代码
Flutter SafeArea - 异形屏适配利器

可以看到问题已经被解决。

我们还可以仅指定特定的某一位置:

SafeArea(
  top: false,
  bottom: true,
  left: true,
  right: false,
),
复制代码

原理是什么

我们点进 SafeArea 的源码查看 build 方法

Widget build(BuildContext context) {
    assert(debugCheckHasMediaQuery(context));
    // 这里获取到padding
    final EdgeInsets padding = MediaQuery.of(context).padding;
    return Padding( // 返回了一个 Padding widget
      padding: EdgeInsets.only(
        left: math.max(left ? padding.left : 0.0, minimum.left),
        top: math.max(top ? padding.top : 0.0, minimum.top),
        right: math.max(right ? padding.right : 0.0, minimum.right),
        bottom: math.max(bottom ? padding.bottom : 0.0, minimum.bottom),
      ),
      child: MediaQuery.removePadding(
        context: context,
        removeLeft: left,
        removeTop: top,
        removeRight: right,
        removeBottom: bottom,
        child: child,
      ),
    );
  }
复制代码

可以看到SafeArea通过MediaQuery来检测屏幕尺寸,使应用程序的大小能与屏幕适配。

然后返回了一个Padding Widget 来包裹住我们编写的页面。这样我们的页面就不会被异形屏幕给遮挡住了。

Flutter SafeArea - 异形屏适配利器

以上所述就是小编给大家介绍的《Flutter SafeArea - 异形屏适配利器》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Design Index by Content 3 (Web Design Index)

Web Design Index by Content 3 (Web Design Index)

Pepin Press (EDT) / Pepin Press / 2007-11 / USD 29.99

Would you like an overview of the state of the art in web design in a specific field? WEB DESIGN INDEX BY CONTENT provides exactly that: every year, 500 new designs are selected and grouped in more th......一起来看看 《Web Design Index by Content 3 (Web Design Index)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换