当前位置:网站首页>Flutter Widget 如何启用和屏蔽点击事件
Flutter Widget 如何启用和屏蔽点击事件
2022-08-01 10:44:00 【️ 邪神】
AbsorbPointer 介绍
官方说明
/// A widget that absorbs pointers during hit testing.
一个可拦截子视图点击事件的Widget .
/// When [absorbing] is true, this widget prevents its subtree from receiving
/// pointer events by terminating hit testing at itself.
当 absorbing 属性值为 true 时 , AbsorbPointer 将用户的点击事件消耗掉不让其子组件接收到 .
///It still consumes space during layout and paints its child as usual.
AbsorbPointer 会占用布局的空间并包裹在子组件外面 .
/// It just prevents its children
/// from being the target of located events, because it returns true from
/// [RenderBox.hitTest].
AbsorbPointer 的作用就是控制子Widget 获取用户的点击事件 , 但不能将它作为点击事件的目标 .
absorbing 属性
Flutter AbsorbPointer 属性absorbing 设置
Column(
children: <Widget>[
Container(
width: 200.0,
height: 200.0,
margin: const EdgeInsets.only(top: 30.0),
child: AbsorbPointer(
absorbing: false,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green.shade200,
),
onPressed: () {
print("AbsorbPointer 子Widget ElevatedButton absorbing: false onPressed");
},
child: const Text("absorbing: false"),
),
),
),
Container(
width: 200.0,
height: 200.0,
margin: const EdgeInsets.only(top: 30.0),
child: AbsorbPointer(
absorbing: true,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.blue.shade200,
),
onPressed: () {
print("AbsorbPointer 子Widget ElevatedButton absorbing: true onPressed");
},
child: const Text("absorbing: true"),
),
),
),
],
)
控制点击区域
当点击除开绿色和蓝色区域外的红色区域触发点击事件
Container(
width: 300.0,
height: 200.0,
child: Stack(
children: [
Positioned(
left: 0.0,
top: 0.0,
right: 0.0,
bottom: 0.0,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red.shade200,
padding: const EdgeInsets.all(40.0),
),
onPressed: () {
},
child: null,
),
),
Positioned(
left: 30.0,
right: 30.0,
top: 30.0,
child: SizedBox(
width: 200.0,
height: 100.0,
child: AbsorbPointer(
absorbing: true,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green.shade200,
),
onPressed: () {
},
child: const Text("点击区域一"),
),
),
),
),
Positioned(
left: 30.0,
right: 30.0,
bottom: 10.0,
child: SizedBox(
width: 200.0,
height: 40.0,
child: AbsorbPointer(
absorbing: true,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.blue.shade200,
),
onPressed: () {
},
child: const Text("点击区域二"),
),
),
),
),
],
)),
IgnorePointer介绍
官方说明
/// A widget that is invisible during hit testing.
一个在接收到点击事件过程中不可见的Widget .
/// When [ignoring] is true, this widget (and its subtree) is invisible to hit testing.
当 ignoring 属性值为true时 , IgnorePointer 在收到点击事件时是不可见的 .
It still consumes space during layout and paints its child as usual.
IgnorePointer 在收到点击事件后虽然不可见,但是会占用空间的,同时会完成子Widget的绘制 .
ignoring 属性
控制子Widget是否可点击
Container(
width: 300.0,
height: 200.0,
color: Colors.red.shade200,
child: IgnorePointer(
ignoring: false,
key: ignorePointerKey,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green.shade200,
),
onPressed: () {
RenderBox? renderBox = ignorePointerKey.currentContext?.findRenderObject() as RenderBox?;
print("IgnorePointer onPressed 点击区域一 ${renderBox?.size}");
},
child: const Text("点击区域一"),
),
),
),
AbsorbPointer使用场景
常见组件会提供一种禁止输入的方法 .
例如 : 将TextButton的onPressed回掉设置为空值 .
const TextButton(
child: Text("点击我"),
onPressed: null,
)
可以设置ListView 属性 physics , 让ListView不要滚动
ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: 3,
itemBuilder: (context, index) {
return Container();
})
当我们需要将屏幕范围内的所有组件都禁止操作 (点击、滚动、输入时) 时, 我们需要 将所有组件 用 AbsorbPointer包裹起来 , 并设置属性值 absorbing , 获取取消屏蔽也可以.
IgnorePointer 使用场景
用户和界面交互过程中 , 我们有时需要某些区域无法交互 , 就需要将无法被用户进行操作的区域用IgnorePointer 进行包裹 并且设置 ignoring 属性为true . 我们可以用IgnorePointer可以做到将包裹区域的子Widget 不能进行点击、单击、拖动、滚动、所有动作 .
AbsorbPointer和IgnorePointer 区别
AbsorbPointer | IgnorePointer |
---|---|
获取点击事件前后都可见 | 获取到点击事件后不可见 |
可以不和子组件共享点击事件 | 和子widget共享事件,否则都不能获取点击事件 |
点击事件不可穿透 | 点击事件可穿透 |
Flutter小部件之AbsorbPointer
Flutter 浅谈AbsorbPointer和IgnorePointer的区别
深入研究flutter组件之(AbsorbPointer
边栏推荐
- Solve vscode input! Unable to quickly generate skeletons (three methods for the new version of vscode to quickly generate skeletons)
- redis6 跟着b站尚硅谷学习
- 怎么找出电脑隐藏的软件(如何清理电脑隐藏软件)
- Cross-domain network resource file download
- Small application project works WeChat gourmet recipes applet graduation design of finished product (1) the development profile
- 将本地项目推送到远程仓库
- Introduction to STM32 development Introduce IIC bus, read and write AT24C02 (EEPROM) (using analog timing)
- 在线GC日志分析工具——GCeasy
- MTK6225-紧急电话
- PowerPC技术与市场杂谈
猜你喜欢
Dapr 与 NestJs ,实战编写一个 Pub & Sub 装饰器
CTO strongly banning the use of the Calendar, that in what?
Py之yellowbrick:yellowbrick的简介、安装、使用方法之详细攻略
招聘随想2022
What is a stepper motor?40 pictures to show you!
微信公众号授权登录后报redirect_uri参数错误的问题
SAP ABAP OData 服务如何支持 $orderby (排序)操作试读版
各位大拿,安装Solaris 11.4操作系统,在安装数据库依赖包的时候包这个错,目前无原厂支持,也无安装盘,联网下载后报这个错,请教怎么解决?
mysql login in cmd and basic operations of database and table
DBPack SQL Tracing 功能及数据加密功能详解
随机推荐
机器学习 | MATLAB实现支持向量机回归RegressionSVM参数设定
redis6 跟着b站尚硅谷学习
图解MySQL内连接、外连接、左连接、右连接、全连接......太多了
Introduction and application of pointers
【云驻共创】分布式技术之华为云全域调度技术与实践
使用KeyStore生成证书
[Software Architecture Mode] The difference between MVVM mode and MVC mode
已解决(pip安装库报错)Consider using the-- user option or check the permissions.
玻璃拟态(Glassmorphism)设计风格
小程序毕设作品之微信美食菜谱小程序毕业设计成品(2)小程序功能
这是我见过写得最烂的Controller层代码,没有之一!
编码解码(btoa、encodeURIComponent、encodeURI、escape)
Introduction to data warehouse layering (real-time data warehouse architecture)
RK3399 platform development series on introduction to (kernel) 1.52, printk function analysis - the function call will be closed
The meaning and trigger conditions of gc
阿里腾讯面试一二
从零开始Blazor Server(4)--登录系统
MTK6225-紧急电话
Mysql index related knowledge review one
RK3399平台开发系列讲解(内核入门篇)1.52、printk函数分析 - 其函数调用时候会关闭中断