如何在滚动时为 AppBar 的颜色设置动画?

How to animate the color of AppBar on scroll?

如何在滚动 CustomScrollView 时为 SliverAppBar 设置动画颜色?

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

试试下面的代码:

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(),
    ],
  ),
);