Flutter: Ignore touch events on a Widget

后端 未结 2 1572
我寻月下人不归
我寻月下人不归 2020-11-30 05:44

I want to have a blurred image on top of my other widgets, however, I cannot interact with the widgets below it when I do that.

相关标签:
2条回答
  • 2020-11-30 05:58

    Solution

    You can solve your interaction issue (not being able to interact with the Widget below your blurred image) by surrounding your BackdropFilter with an IgnorePointer.

    This means that IgnorePointer is the solution here because it will ignore all touch events for the Widget's passed as its child.

    IgnorePointer(child: BackdropFilter(...),)
    

    You can adjust this attribute by changing the bool value of ignoring:

    IgnorePointer(ignoring: false, ...)
    

    This will enable all touch events again.

    Absorbing

    Something interesting to look at here, but unrelated to the problem, is the AbsorbPointer Widget, which can be used to reflect all touch events that occur on its child onto itself.

    0 讨论(0)
  • 2020-11-30 05:58

    You can either use IgnorePointer or AbsorbPointer.

    Example (IgnorePointer)

    IgnorePointer(
      child: RaisedButton(
        onPressed: () {},
        child: Text("Unclickable button"),
      ),
    );
    

    Example (AbsorbPointer)

    AbsorbPointer(
      child: RaisedButton(
        onPressed: () {},
        child: Text("Unclickable button"),
      ),
    );
    

    What's the difference?

    If there is a widget beneath your main widget which is also capable of receiving click events, and you use IgnorePointer on the parent widget, the child widget would still receive the click events.

    But using AbsorbPointer on main widget won't allow the other widget (beneath main widget) to receive their click events.

    Example showing the difference.

    @override
    Widget build(BuildContext context) {
      return SizedBox(
        width: double.infinity,
        child: Stack(
          children: <Widget>[
            Positioned(
              left: 0,
              width: 250,
              child: RaisedButton(
                color: Colors.red,
                onPressed: () => print("Button 1"),
                child: Text("Button 1"),
              ),
            ),
            Positioned(
              right: 0,
              width: 250,
              child: IgnorePointer( // replace this with AbsorbPointer and button 1 won't receive click
                child: RaisedButton(
                  onPressed: () => print("Button 2"),
                  child: Text("Button 2"),
                ),
              ),
            ),
          ],
        ),
      );
    }
    
    0 讨论(0)
提交回复
热议问题