title: An efficient way in Flutter to change appbar color when scrolled
body: <p>I have been working on the Scroll of the page, and at the same time, doing these things:</p>
  <ul>
  <li>Changing color of the AppBar background when scrolling</li>
  <li>Changing the AppBar background to transparent when scrolled to the top</li>
  </ul>
  <p>My code works fine, but the problem here is, it is laggy when it comes to smooth scrolling.</p>
  <p>I have read about the flutter documentation, and found out, that, <strong>using <code>setState()</code> inside the addListener() is not the best practice you can follow</strong>.</p>
  <p>I have tried searching more about it, but couldn't find an efficient solution.</p>
  <p>Here is my code:</p>
  <pre><code>class HomePageState extends State&lt;HomePage&gt; {
    int _selectIndex = 0;
    bool isAppbarCollapsing = false;
    final ScrollController _homeController = new ScrollController();
  
    @override
    void initState() {
      super.initState();
  
      // for adding appbar background color change effect 
      // when scrolled
      _initializeController();
    }
  
    @override
    void dispose(){
      _homeController?.dispose();
      super.dispose();
    }
  
    void _initializeController(){
      _homeController.addListener(() {
        if(_homeController.offset == 0.0 &amp;&amp; !_homeController.position.outOfRange){
          //Fully expanded situation
          if(!mounted) return;
          setState(() =&gt; isAppbarCollapsing = false);
        }
        if(_homeController.offset &gt;= 9.0 &amp;&amp; !_homeController.position.outOfRange){
          //Collapsing situation
          if(!mounted) return;
          setState(()  =&gt; isAppbarCollapsing = true);
        }
      });
    }
  
    @override
    Widget build(BuildContext context) {
       return Scaffold(
         appBar: AppBar(
            // here is how I am changing the color
            backgroundColor: isAppbarCollapsing ? Colors.white.withOpacity(0.8) : Colors.transparent
         ),
         body: Container(
           height: MediaQuery.of(context).size.height,
           width: MediaQuery.of(context).size.width,
           child: ListView(
             controller: _homeController
           )
         )
       );
    }
  }
  </code></pre>