我希望在我的其他小部件上有一个模糊的图像,但是,当我这样做时,我不能与它下面的小部件交互。
发布于 2018-05-31 03:16:13
解决方案
您可以通过用IgnorePointer
包围您的BackdropFilter
来解决交互问题(无法与模糊图像下的IgnorePointer
进行交互)。
这意味着IgnorePointer
在这里是解决方案,因为它将忽略作为其子元素传递的Widget
的所有触摸事件。
IgnorePointer(child: BackdropFilter(...),)
可以通过更改ignoring
的bool
值来调整此属性
IgnorePointer(ignoring: false, ...)
这将再次启用 events。
吸收
这里有一个有趣但与问题无关的东西,那就是AbsorbPointer
Widget
,它可以用来将其子上发生的所有触摸事件反射到自己的上。
发布于 2019-03-30 20:22:12
您可以使用IgnorePointer
或AbsorbPointer
。
IgnorePointer
IgnorePointer(子: ElevatedButton( onPressed:() {},子:文本(‘不可点击按钮’),);
AbsorbPointer
AbsorbPointer(子: ElevatedButton( onPressed:() {},子:文本(‘不可点击按钮’),);
有什么不同?
如果您的主小部件下面有一个小部件,该小部件也能够接收单击事件,并且您在父小部件上使用IgnorePointer
,则子小部件仍将接收单击事件。
但是在主窗口小部件上使用AbsorbPointer
将不允许其他窗口小部件(在主窗口小部件下面)接收它们的点击事件。
显示不同之处的示例。
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
child: Stack(
children: <Widget>[
Positioned(
left: 0,
width: 250,
child: ElevatedButton(
color: Colors.red,
onPressed: () => print("Button 1"),
child: Text("Button 1"),
),
),
Positioned(
right: 0,
width: 250,
child: IgnorePointer( // replace this with AbsorbPointer and button 1 won't receive click
child: ElevatedButton(
onPressed: () => print("Button 2"),
child: Text("Button 2"),
),
),
),
],
),
);
}
发布于 2021-08-30 13:10:12
已更新
其他的答案非常吸引人。
让我们看一个使用IgnorePointer
小部件的实际示例。
当我们开始尝试实现一些东西时,这种情况是很常见的,比如在要删除的小部件上切换选择,或者像这样的东西。
示例Senario :持有WhatsApp消息,删除选项出现在顶部。如果在此选项处于活动状态时轻拍任何其他位置,它将转到。
我是这样实现的。
appBar: AppBar(
title: Text('MyApp'),
actions: [
if (_selected != null) // <-- Delete button only appear something selected.
IconButton(
icon: Icon(Icons.delete),
onPressed: () {
// Delete Code here
}
]
),
body: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
print('Tapped');
setState(() { _selected = null });
},
child: IgnorePointer(
ignoring: _selected != null ? true : false, // <-- Ignore only when selecting something.
child: Column(
children: [
...
// This is a sample message
GestureDetector(
onLongPress: () {
setState(() { _selected = messageId });
}
child: Container(
child: Text('This is a message'),
),
...
结果:
希望它能帮助到一些人!祝您今天愉快。
https://stackoverflow.com/questions/50600747
复制相似问题