Flutter- GestureDetector not working with containers in stack

前端 未结 10 821
一生所求
一生所求 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:51

    Another GestureDetector in the BuildContext tree

    Ok on my side it was quite stupid but since it may happens to many of us I will explain:

    My build context (simplified for the example) tree was this way:

    Widget build(BuildContext context) {
    
    return (...) (
        child: GestureDetector(
            onTap: () => _myFunction(),
            child: Container(
                height: 64.0,
                width: 128.0,
                child: (...)
    

    Seems legit right? But then I inspected into more details the bottom of my tree:

    (...)
            onTap: () => _myFunction(),
            child: Container(
                height: 64.0,
                width: 128.0,
                child: TheSourceOfEvil() // <-- whoopsie!
    

    Quickly inspected TheSourceOfEvil to find out that the culprit was in fact ANOTHER GestureDetector has a child, that I had not yet implemented onTap: () => {},:

    Widget TheSourceOfEvil( {
    
    return (...) (
        child: GestureDetector(
            onTap: () => {}, // override previous GestureDetector
            child: Container(
                child: (...);
    

    Got rid of it and it instantly fixed my puzzling issue. I hope it will help!

提交回复
热议问题