Flutter 检测扩展文件'上的手势;他在《颤栗》中的主角

Flutter 检测扩展文件'上的手势;他在《颤栗》中的主角,flutter,dart,widget,gesture,Flutter,Dart,Widget,Gesture,如何在flatter中检测到扩展文件的主要小部件上的手势?我无法可靠地使主图标触发函数,相反,当我点击主图标时,扩展磁贴只会扩展 我想要的是,当用户点击前导图标时,扩展磁贴不会改变其状态,并且会调用我指定的函数 我试着混合手势检测器,忽略指针,但到目前为止没有任何效果 ExpansionTile( leading: GestureDetector( child: Icon(Icons.home), onTap: () { print('I wa

如何在flatter中检测到
扩展文件
的主要小部件上的手势?我无法可靠地使主图标触发函数,相反,当我点击主图标时,扩展磁贴只会扩展

我想要的是,当用户点击前导图标时,扩展磁贴不会改变其状态,并且会调用我指定的函数

我试着混合
手势检测器
忽略指针
,但到目前为止没有任何效果

  ExpansionTile(
    leading: GestureDetector(
      child: Icon(Icons.home),
      onTap: () {
        print('I want to execute something there...');
        print('and I also want to prevent toggling the expansion on the tile');
      },
    ),
    title: Text('Title'),
    children: [
      Text('child 1'),
      Text('child 2'),
    ],
    initiallyExpanded: true,
  );

使用
IconButton
而不是
Icon
,并在其
onPressed()函数中实现您的逻辑:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final appTitle = 'ExpansionTile Issue';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: appTitle,
      home: MyHomePage(title: appTitle),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  MyHomePage({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(title)),
      body: Center(child: ExpansionTile(
        leading: IconButton(
            icon: Icon(Icons.home),
            onPressed: (){
              print('function is executed, but there is no expanstion triggering !');
            }
        ),

        title: Text('Title'),
            children: [
            Text('child 1'),
        Text('child 2'),
        ],
        initiallyExpanded: true,
      )),
    );
  }
}
编辑:

在进一步调查该问题后,发现
GestureDetector
没有任何问题,它只是包装了它的子
小部件
,当您使用鼠标单击时,它工作正常。但是当手指接触到更大的区域时,它们会触发
手势检测器
及其父
扩展文件

这个问题的一个解决方案是将您的
前导
小部件包装到一个宽度更大的
容器
(比如
宽度:100.0
),因此任何远离其边界的操作肯定不是为它准备的:

GestureDetector(
          child: Container(
            color: Colors.yellow,
            width: 80.0,
            height: 40.0,
            child: Icon(Icons.home),
          ),
          onTap: () {
            print('Now any click in this colored area will only trigger the GestureDetector, but make sure to set size depending on MediaQuery and textScaleFactor if you intend to use a Text Widget');
          },
        ),

如果我的主要部件不是图标?