flutter -忽略点击事件
absorbPointer开启absorbing:本身可以接收点击事件,但不会把事件传递给子组件。
ignorePointer开启ignoring:本身和子组件都不能接收点击事件。
import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; void main() => runApp(MaterialApp( title: '啦啦啦啦', theme: ThemeData(primarySwatch: Colors.red, primaryColor: Colors.green), home: PointerIgnorePage(),) ); class PointerIgnorePage extends StatefulWidget { @override State<StatefulWidget> createState() => PointerIgnorePageState(); } class PointerIgnorePageState extends State<PointerIgnorePage> { bool _ifIgnore = false; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('测试忽略点击事件'),), body: Container( alignment: Alignment.center, child: Column( children: <Widget>[ Switch( value: _ifIgnore, onChanged: (value) => setState((){_ifIgnore = value;}), ), GestureDetector( onTap: () => print('外层tap1'), child: IgnorePointer( ignoring: _ifIgnore, child: FlatButton(child: Text('点我'), onPressed: () => print('点击了button1'),), ), ), GestureDetector( onTap: () => print('外层tap2'), child: AbsorbPointer( absorbing: _ifIgnore, child: FlatButton(child: Text('点我'), onPressed: () => print('点击了button2'),), ), ), ], ), ), ); } }
关闭_ifIgnore,二者本身都能接收点击事件,child优先接收点击事件,所以分别输出button1/button2。
开启_ifIgnore,只有absorbPointer才能接收点击事件,但其内部button无法接收点击事件,所以能输出tap2。而ignorePointer则完全无法接收点击事件,所以tap1无法输出。
进击的小🐴农