Flutter- GestureDetector not working with containers in stack

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

    Try setting the behavior property of GestureDetector to HitTestBehavior.translucent.

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

    I see that you are calling the callback method instead of assigning it to onTap property of GestureDetector widget. Pass the name of the method don't call it there.

    0 讨论(0)
  • 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!

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

    For me it was an issue with items overflowing outside a Stack, with clipBehavior: Clip.none.

    In that case the item is visible but not touchable. So I updated my layout to remove the clipBehavior: Clip.none on my Stack and the GestureDetector started working.

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