Flutter- GestureDetector not working with containers in stack

前端 未结 10 752
一生所求
一生所求 2020-12-28 12:14

I have two containers in a stack and both containers have GestureDetector.The OnTap for the first container is working fine but it\'s not working with another container. The

相关标签:
10条回答
  • 2020-12-28 12:29

    Using InkWell instead of GestureDetector solved my problem.

    0 讨论(0)
  • 2020-12-28 12:29

    GestureDetector is not working when write code inside its block It will be doesn't work

    GestureDetector(
                      onTap: () {
                        setState(() {
                          _color = Colors.red;
                        });
                      },),
    

    You will write like that it will work

    GestureDetector(
                      onTap: () {
                        changed();
                      },),
    

    method

    void changed() {
    setState(() {
      _color = Colors.red;
    });}
    
    0 讨论(0)
  • 2020-12-28 12:33

    Because of stack..There are some widget on your GestureDetector.

    Solution:

    Stack(
            children: <Widget>[
                  ...//Other widgets,
                  GestureDetector()
            ]
    
    0 讨论(0)
  • 2020-12-28 12:37

    i think that your widgets are overlapping each other and that the causing a problem. you can check it by wrapping your GestureDetector with container and provide color to get better understanding.

    your code is not enough that's why i added following example may help you to understand more clearly.

    swap the position of GestureDetector in example and you can found that in first case it prints only second and in other case if you click in above part then it prints first to.

    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,
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-28 12:37

    In my case I had a CustomPainter inside the stack. Nothing of the above was working, until I gave the CustomerPainter a size. It was showing without explicitly giving a size, but no tap events were triggered:

    Container(
          child: CustomPaint(
        size: Size(30, 30),
        painter: RecordingButton(30),
      ))
    
    0 讨论(0)
  • 2020-12-28 12:37

    For others coming to this post, another thing that can cause this issue is an AbsorbPointer higher is the hierarchy.

    0 讨论(0)
提交回复
热议问题