我想在其他小部件的顶部放置一张模糊的图片,但是这样做时,我无法与其下方的小部件进行交互。
我想在其他小部件的顶部放置一张模糊的图片,但是这样做时,我无法与其下方的小部件进行交互。
您可以通过将BackdropFilter
包裹在IgnorePointer
中来解决交互问题(即无法与模糊图像下方的Widget
进行交互)。
这意味着IgnorePointer
是这里的解决方法,因为它将忽略所有传递给它的Widget
子元素的触摸事件。
IgnorePointer(child: BackdropFilter(...),)
您可以通过更改ignoring
的bool
值来调整此属性:
IgnorePointer(ignoring: false, ...)
这将再次启用所有触摸事件。
有趣的是,这里有一些有趣的信息,但与问题无关。您可以使用AbsorbPointer
Widget
,将发生在其子代上的所有触摸事件 反射到 它自己身上。
IgnorePointer
和AbsorbPointer
小部件没有被更改。 - creativecreatorormaybenotgestureSettings
设置为必需的,这破坏了我的自定义RenderWidget
。 - Dennis BarzanoffIgnorePointer
或AbsorbPointer
。
IgnorePointer
IgnorePointer(
child: ElevatedButton(
onPressed: () {},
child: Text('Not clickable Button'),
),
);
AbsorbPointer
AbsorbPointer(
child: ElevatedButton(
onPressed: () {},
child: Text('Not clickable Button'),
),
);
有什么区别?
如果您的主要小部件下面有一个小部件,它也能够接收点击事件,并且您在父小部件上使用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"),
),
),
),
],
),
);
}
ignorePointer()
小部件怎么办呢?我们该如何处理? - Flutter更新
其他答案非常吸引人。
让我们看一个使用IgnorePointer
小部件的实际示例。
当我们开始尝试实现像切换小部件上的选择以删除或类似于此类东西时,这种情况非常普遍。
示例场景:长按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'),
),
...
结果:
希望能对某些人有所帮助!祝你有美好的一天。