zoukankan      html  css  js  c++  java
  • 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无法输出。

  • 相关阅读:
    微信用户授权,获取code
    关于swiper在vue中不生效的问题
    ES6-Set 和 Map 数据结构
    Js中caller和callee的区别
    类与对象基础总结--继承,多态
    java 类与对象基础整理
    java 的数据库操作--JDBC
    Socket的长连接和短连接
    java 的底层通信--Socket
    算法--树与递归
  • 原文地址:https://www.cnblogs.com/FdWzy/p/13501429.html
Copyright © 2011-2022 走看看