可能有时候会需要这种需求

比如文本框可以响应点击事件但是禁止用户输入

直接粗暴的设置

TextField(enabled: false,)

这样就无法得知ontap事件了,

当然你可以用

enableInteractiveSelection: false

配合ontap 收键盘来达到目的

但是 不够优雅

记录一下 flutter提供的两个组件AbsorbPointer  和 IgnorePointer

Column(

children: [

Text('AbsorbPointer'),

GestureDetector(

onTap: () {

print('AbsorbPointer');

},

child: AbsorbPointer(

absorbing: true,

child: Row(

children: [

TextField(enabled: false,),

RaisedButton(

onPressed: () {

print('onPressed');

},

),

RaisedButton(

onPressed: () {

print('onPressed');

},

),

RaisedButton(

onPressed: () {

print('onPressed');

},

),

RaisedButton(

onPressed: () {

print('onPressed');

},

),

],

),

),

),

Text('IgnorePointer'),

GestureDetector(

onTap: () {

print('IgnorePointer');

},

child: IgnorePointer(

ignoring: true,

child: Row(

children: [

RaisedButton(

onPressed: () {

print('onPressed');

},

),

RaisedButton(

onPressed: () {

print('onPressed');

},

),

RaisedButton(

onPressed: () {

print('onPressed');

},

),

RaisedButton(

onPressed: () {

print('onPressed');

},

),

],

),

),

)

],

)

区别:

IgnorePointer和AbsorbPointer,这两个Widget都能阻止子树接收指针事件,不同的是AbsorbPointer本身是可以接收指针事件的(但其子树不行),而IgnorePointer本身就不可以接收指针事件

都是true到时候点击

此时只有

AbsorbPointer可以响应 而IgnorePointer则没有反应

档都是false的时候

可以看到下边的事件都可以响应

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐