您可以使用IgnorePointer 或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"),
),
),
),
],
),
);
}