点击优先级子> GesuterDetector。如果您在GestureDetector 上有一个IconButton 的孩子,则只有IconButton 可以工作。
假设您有一个列。
- 您可以根据条件在
onPressed 或IconButton 上传递null。
- 使用
AbsorbPointer 将阻止其子级的点击事件,而GestureDetector 的点击事件将在这种情况下起作用。
-
IgnorePointer 将完全忽略其区域内的任何点击事件。
演示小部件
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget();
// final String title;
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool _disableIconButton = false;
@override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.white,
body: Container(
child: Column(
children: [
GestureDetector(
onTap: () {
print("GestureDetector Tapped");
},
child: IconButton(
onPressed: () {
print(" only Icon will be working here");
},
icon: Icon(Icons.ac_unit),
),
),
SizedBox(
height: 100,
),
GestureDetector(
onTap: () {
print("GestureDetector Tapped");
},
child: Column(
children: [
Text("Inside Column"),
Switch(
value: _disableIconButton,
onChanged: (v) {
setState(() {
_disableIconButton = v;
});
},
),
///* Colors will faded on
IconButton(
onPressed: _disableIconButton
? null
: () {
print("Icon null checker tapped");
},
icon: Icon(Icons.ac_unit),
),
///* Colors will faded on like disable and will work on GuesterTap
AbsorbPointer(
absorbing: _disableIconButton,
child: IconButton(
onPressed: _disableIconButton
? null
: () {
print("Icon AbsorbPointer tapped");
},
icon: Icon(Icons.ac_unit),
),
),
///* it will ignore tap event
IgnorePointer(
ignoring: _disableIconButton,
child: IconButton(
onPressed: _disableIconButton
? null
: () {
print("Icon IgnorePointer tapped");
},
icon: Icon(Icons.ac_unit),
),
),
],
),
)
],
),
),
);
}
}