title: Flutter - Disable all click events while animation is running
body: <p>There's a <code>AnimatedSwitcher</code> in my view which animates the page transition. I want to disable all click events while transition animation is running. Here's what I've tried:</p>
  <pre><code>class _MainPageState extends State&lt;MainPage&gt; {
    int pageIndex = 1;
    bool disableClicks = false;
  
    @override
    Widget build(BuildContext context) {
      return AbsorbPointer(
        absorbing: disableClicks,
        child: Scaffold(
          body: AnimatedSwitcher(
            duration: 500.milliseconds,
            child: pageIndex == 0
                ? Container(color: Colors.red, key: UniqueKey())
                : pageIndex == 1
                ? Container(color: Colors.blue, key: UniqueKey())
                : Container(color: Colors.green, key: UniqueKey()),
            transitionBuilder: (child, animation) {
              animation.addStatusListener((status) {
                switch (status) {
                  case AnimationStatus.forward:
                  case AnimationStatus.reverse:
                    if (mounted &amp;&amp; !disableClicks)
                      setState(() {
                        disableClicks = true;
                      });
                    break;
                  case AnimationStatus.dismissed:
                  case AnimationStatus.completed:
                    if (mounted &amp;&amp; disableClicks)
                      setState(() {
                        disableClicks = false;
                      });
                    break;
                }
              });
              return FadeTransition(child: child, opacity: animation);
            },
          ),
          
        ),
      );
    }
  }
  </code></pre>
  <p>But apparently I'm using <code>setState</code> while page is building and Flutter is throwing Exception:</p>
  <p><code>setState() or markNeedsBuild() called during build.</code></p>
  <p>What is the proper way to achieve this?</p>