Flutter 浅谈AbsorbPointer和IgnorePointer的区别

首先,AbsorbPointer和IgnorePointer都可以用来拦截点击事件,AbsorbPointer通过absorbing可以响应事件

如下示例,可以接收到点击事件

AbsorbPointer(
      absorbing: false,
      child: RaisedButton(
          child: Text("button01"),
          onPressed: () {
            print("onclick");
          }),
    );

但是两者又有所不同,在嵌套事件中,AbsorbPointer会消费掉点击事件,不会穿透容器而向下传递,但是
IgnorePointer却可以,如下

class TestWidget extends StatefulWidget {
  @override
  _TestWidgetState createState() => _TestWidgetState();
}

class _TestWidgetState extends State {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: Stack(
          alignment: Alignment.center,
          children: [
            Container(
              width: 200,
              height: 200,
              child: RaisedButton(
                color: Colors.red,
                  onPressed: (){
                print("click red");
              }),
            ),
            IgnorePointer( //忽略点击事件,不影响下层的点击事件
//            AbsorbPointer( //消费掉点击事件,下层也收不到,不会做出相应
              child: Container(
                width: 100,
                height: 100,
                child: RaisedButton(
                  color: Colors.blue,
                  onPressed: (){
                    print("click blue");
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

你可能感兴趣的:(Flutter 浅谈AbsorbPointer和IgnorePointer的区别)