How to animate the color of AppBar on scroll?

697 Views Asked by At

How can I animate the color of my SliverAppBar as I scroll through the CustomScrollView?

Scaffold(
  body: CustomScrollView(
    controller: _scrollController,
    slivers: [
      SliverAppBar(...),
      buildBody(),
    ],
  ),
);
1

There are 1 best solutions below

1
On BEST ANSWER

try the following code:

ScrollController _scrollController;
Color _appColor = Colors.black;

  @override
  void initState() {
    super.initState();

  _scrollController= ScrollController()..addListener(() { 
    if(_scrollController.position.pixels == 0){
      _appColor = Colors.black;
      setState(() {});
    }
    else if(_scrollController.position.pixels == 100){
      _appColor = Colors.red;
      setState(() {});
    }
  });
  }

Scaffold(
  body: CustomScrollView(
    controller: _scrollController,
    slivers: [
      //put _appColor to appbar backgroundColor
      SliverAppBar(),
      buildBody(),
    ],
  ),
);