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无法输出。

  • 相关阅读:
    bbs树形打印(一)
    ORM
    kafka的分区
    Content-type"是"application/json的作用
    idea增强for循环
    rabbitmq的发布订阅
    搭建mqtt服务器apollo
    kafka生产者集群和分区,消费者轮询接收
    http实时推送技术
    Kafka:Configured broker.id 2 doesn't match stored broker.id 0 in meta.properties.
  • 原文地址:https://www.cnblogs.com/FdWzy/p/13501429.html
Copyright © 2011-2022 走看看