Flutter- GestureDetector无法与堆栈中的容器一起使用

Him*_*dav 15 gesturedetector flutter

我有两个容器,两个容器都有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),
              )
            ],
          )
Run Code Online (Sandbox Code Playgroud)

布局截图

在此处输入图片说明

Mic*_*tes 47

您应该尝试用 AbsorbPointer 包装您的容器。

GestureDetector(
   child: AbsorbPointer(
      child: Container(...)
   )
)
Run Code Online (Sandbox Code Playgroud)

  • 如果能举个例子就更好了 (2认同)

小智 35

尝试将的behavior属性设置GestureDetectorHitTestBehavior.translucent

  • 是的,它有效。为什么默认情况下不是这种行为? (5认同)
  • 请注意,这不适用于综合浏览量。/sf/ask/3465748611/。 (4认同)
  • 这对我不起作用,还有其他建议吗? (2认同)

Rod*_*voi 10

还要考虑Listener小部件。它帮助了我。
https://api.flutter.dev/flutter/widgets/Listener-class.html
示例:

Listener(
    onPointerDown: (_) {}, // onTapDown
    onPointerUp: (_) => {}, // onTapUp
    child: Container(),
)
Run Code Online (Sandbox Code Playgroud)


Sob*_*adi 5

使用InkWell而不是GestureDetector解决了我的问题。

  • 是的,但它添加了其他(通常)不需要的部分 - 例如,你知道,墨水很好。在当前上下文中设置“行为”是正确的答案。 (2认同)
  • 仅供参考,“InkWell”是一个材质组件。它增加了视觉/声音反馈,这可能不是开发人员想要的。此外,应用程序可以使用 iOS Cupertino 主题。如果您只对点击/点击事件感兴趣,“GestureDetector”是一个不错的选择。(使用“HitTestBehavior.translucent”) (2认同)
  • OP 明确表示他们想要使用“GestureDetector”。 (2认同)

Hug*_*o H 5

对我来说,这是一个项目在Stack,之外溢出的问题clipBehavior: Clip.none

在这种情况下,该项目可见但不可触摸。所以我更新了我的布局以删除clipBehavior: Clip.noneStackGestureDetector开始工作。


Vir*_*iya 1

我认为你的小部件相互重叠并且导致了问题。您可以通过用容器包裹 GestureDetector 来检查它并提供颜色以便更好地理解。

您的代码还不够,这就是为什么我添加以下示例可以帮助您更清楚地理解。

交换示例中 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,
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)