内容简介:现在的手机已经不是方方正正的屏幕了,所以我们在写一些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(安全区域),我们只需要在代码中加入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),
),
);
}
复制代码
可以看到问题已经被解决。
我们还可以仅指定特定的某一位置:
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 - 异形屏适配利器》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 冲突的公链!来自P2P协议的异形攻击漏洞
- flutter 屏幕尺寸适配 字体大小适配
- 前端适配:移动端/web端适配方案
- iOS 关于全面屏适配的方案及UI在不同尺寸下适配方案
- iOS 关于全面屏适配的方案及UI在不同尺寸下适配方案
- 【移动端适配】用vw、vh+媒体查询打造最完美的移动端适配方案
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
人机交互:以用户为中心的设计和评估
董建明、傅利民、[美]沙尔文迪 / 清华大学出版社 / 2003-9-1 / 28.00
本书综述部分介绍了与“用户为中心的设计和评估”方法相关的背景知识及发展概况。其后,分3篇分别介绍了解用户、用户界在设计和可用性评估的内容及一些相关的研究专题。最后,第11章讨论了在组织中实施以用户为中心的设计的专题。本书主要面向的读者包括:软件或网站的设计人员。同时本书也可成为“现代人因工程学”及“以用户为中心的设计”的教材,还可作为软件或网站公司经理的提高用户满意度或提升公司形象的手册。一起来看看 《人机交互:以用户为中心的设计和评估》 这本书的介绍吧!