Flutter 颤振-手势检测器不适用于堆叠中的容器

Flutter 颤振-手势检测器不适用于堆叠中的容器,flutter,gesturedetector,Flutter,Gesturedetector,我在一个堆栈中有两个容器,两个容器都有GestureDetector。第一个容器的OnTap工作正常,但与另一个容器不工作。 第一个容器是图像,第二个容器是在第一个容器上部分对齐的绿色背景 new Stack( alignment: Alignment(0.0, 1.44), children: <Widget>[ GestureDetector( onTap: () =>

我在一个堆栈中有两个容器,两个容器都有GestureDetector。第一个容器的OnTap工作正常,但与另一个容器不工作。 第一个容器是图像,第二个容器是在第一个容器上部分对齐的绿色背景

new Stack(
            alignment: Alignment(0.0, 1.44),
            children: <Widget>[
              GestureDetector(
                onTap: () => _openImage(context),
                child: Container(
                  width: 340.0,
                  foregroundDecoration: new BoxDecoration(
                      color: Color.fromRGBO(155, 85, 250, 0.55)),
                  height: 240.0,
                  child: FadeInImage.assetNetwork(
                    placeholder: 'assets/dimlight.png',
                    image: post.imageUrl,
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              new GestureDetector(
                child: new Container(
                  color: Colors.green,
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    children: <Widget>[
                      SizedBox(width: 7.0),
                      CircleAvatar(
                        backgroundImage: 
                           new AssetImage("assets/boy.png")
                        radius: 30.0,
                      ),
                      SizedBox(
                        width: 7.0,
                      ),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          new SizedBox(
                            height: 20.0,
                          ),
                          Text(
                            post.user.name,
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                          Text(
                            getTimeString(post.timestamp.toString()),
                            style: TextStyle(
                                color: Colors.grey, fontSize: 10.0),
                          ),
                        ],
                      ),
                      SizedBox(
                        width: 20.0,
                      ),
                    ],
                  ),
                ),
                onTap: () => _navigateToDetails(context),
              )
            ],
          )
新堆栈(
对齐:对齐(0.0,1.44),
儿童:[
手势检测器(
onTap:()=>\u openImage(上下文),
子:容器(
宽度:340.0,
前场装饰:新盒子装饰(
颜色:color.fromRGBO(155,85250,0.55)),
身高:240.0,
子项:FadeInImage.assetNetwork(
占位符:“assets/dimlight.png”,
image:post.imageUrl,
适合:BoxFit.cover,
),
),
),
新手势检测器(
子容器:新容器(
颜色:颜色。绿色,
孩子:排(
mainAxisSize:mainAxisSize.max,
儿童:[
尺寸箱(宽度:7.0),
圆形(
背景图片:
新资产估值(“assets/boy.png”)
半径:30.0,
),
大小盒子(
宽度:7.0,
),
纵队(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
新尺寸盒子(
身高:20.0,
),
正文(
post.user.name,
样式:TextStyle(fontWeight:fontWeight.bold),
),
正文(
getTimeString(post.timestamp.toString()),
样式:TextStyle(
颜色:颜色。灰色,字体大小:10.0),
),
],
),
大小盒子(
宽度:20.0,
),
],
),
),
onTap:()=>\u导航详细信息(上下文),
)
],
)
布局截图


我认为您的小部件相互重叠,导致了问题。您可以通过用容器包装您的手势检测器来检查它,并提供颜色以获得更好的理解

您的代码还不够,这就是我添加以下示例的原因,可能会帮助您更清楚地理解

交换示例中GestureDetector的位置,您可以发现,在第一种情况下,它只打印第二个,而在另一种情况下,如果您单击上面的部分,它将首先打印到第二个

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Firebase Auth Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Card(
        margin: EdgeInsets.all(40.0),
        child: new Column(
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            GestureDetector(
              onTap: () => print("first container"),
              child: Container(
                width: 340.0,
                foregroundDecoration: new BoxDecoration(
                    color: Color.fromRGBO(155, 85, 250, 0.0)),
                height: 240.0,
                child: FadeInImage.assetNetwork(
                  placeholder: 'images/p1.png',
                  image:
                  "https://www.straitstimes.com/sites/default/files/styles/article_pictrure_780x520_/public/articles/2016/06/15/ST_20160615_LLIMH_2368135.jpg?itok=8Dggu2PM&timestamp=1465926004",
                  fit: BoxFit.cover,
                ),
              ),
            ),
            new GestureDetector(
              child: new Container(
                foregroundDecoration: BoxDecoration(
                    color: Color.fromRGBO(155, 85, 250, 0.4)),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  children: <Widget>[
                    SizedBox(width: 7.0),
                    CircleAvatar(
                      backgroundImage: new AssetImage("images/p2.jpg"),
                      radius: 30.0,
                    ),
                    SizedBox(
                      width: 7.0,
                    ),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          new SizedBox(
                            height: 20.0,
                          ),
                          Text(
                            "sfvgefbv",
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                          Text(
                            "sfvmsfkv",
                            style: TextStyle(
                                color: Colors.grey, fontSize: 10.0),
                          ),
                        ],
                      ),
                    ),
                    new Container(
                      alignment: AlignmentDirectional.centerEnd,
//            todo add here check if not logged in then ask to
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          IconButton(
                              icon: Icon(
                                Icons.comment,
                                color: Colors.green,
                              ),
                              onPressed: () => print("message click")),
                          Text(
                            "2",
                            style: TextStyle(
                              color: Colors.green,
                            ),
                          ),
                          SizedBox(
                            width: 10.0,
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              onTap: () => print("this is second container"),
            ),
            new Expanded(
              child: Container(
                padding: EdgeInsets.all(10.0),
                child: Column(
                  children: <Widget>[
                    Text(
                      "fsvkmfskbnmkffvberk",
                      style: TextStyle(
                          color: Colors.green, fontWeight: FontWeight.bold),
                    ),
                    new Text(
                      "svklmfslkbnernkjrnvkrwjnvrw",
                      maxLines: 6,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“Firebase身份验证演示”,
主页:MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
归还新脚手架(
正文:新卡(
边距:所有边缘集(40.0),
子:新列(
mainAxisSize:mainAxisSize.max,
儿童:[
手势检测器(
onTap:()=>打印(“第一个容器”),
子:容器(
宽度:340.0,
前场装饰:新盒子装饰(
颜色:color.fromRGBO(155,85250,0.0)),
身高:240.0,
子项:FadeInImage.assetNetwork(
占位符:“images/p1.png”,
图片:
"https://www.straitstimes.com/sites/default/files/styles/article_pictrure_780x520_/public/articles/2016/06/15/ST_20160615_LLIMH_2368135.jpg?itok=8Dggu2PM×tamp=1465926004",
适合:BoxFit.cover,
),
),
),
新手势检测器(
子容器:新容器(
前场装饰:盒子装饰(
颜色:color.fromRGBO(155,85250,0.4)),
孩子:排(
mainAxisSize:mainAxisSize.max,
儿童:[
尺寸箱(宽度:7.0),
圆形(
背景图片:新资产评估(“images/p2.jpg”),
半径:30.0,
),
大小盒子(
宽度:7.0,
),
扩大(
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
新尺寸盒子(
身高:20.0,
),
正文(
“sfvgefbv”,
样式:TextStyle(fontWeight:fontWeight.bold),
),
正文(
“sfvmsfkv”,
样式:TextStyle(
颜色:颜色。灰色,字体大小:10.0),
),
],
),
),
新容器(
对齐:对齐方向.centerEnd,
//todo在此处添加检查如果未登录,请询问
孩子:排(
主要尺寸:毫安
Stack(
        children: <Widget>[
              ...//Other widgets,
              GestureDetector()
        ]
Container(
      child: CustomPaint(
    size: Size(30, 30),
    painter: RecordingButton(30),
  ))
final FocusNode _focusNode = new FocusNode();

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {            
        print("tap");
        if (_focusNode.hasPrimaryFocus) {
          _focusNode.unfocus();
          rebuildAfterFocusChange();
        }
      },
      child: Stack(children: [
        AbsorbPointer(absorbing: _focusNode.hasPrimaryFocus, child: StoreMapView()),
        showSearchField(model),
      ]),
    );
  }

Widget showSearchField(StoreFinderViewModel model) {
    return SafeArea(
      child: Padding(
        padding: const EdgeInsets.only(left: 8.0, right: 8.0, top: 12.0),
        child: TextField(
          onTap: () {
            rebuildAfterFocusChange();
          },
          onSubmitted: (String input) {
            rebuildAfterFocusChange();
            // do other stuff
            });
          },
          focusNode: _focusNode,
        ),
      ),
    );
  }

  void rebuildAfterFocusChange() {
    setState(() {});
  }