简体   繁体   English

在颤动中将动画滑动到顶部时更改图标颜色

[英]change icon color when sliding animation to top in flutter

I need to change the color of the icon while sliding to the top.我需要在滑动到顶部时更改图标的颜色。 Using two colors, one at the beginning and the second at the end.使用两种颜色,一种在开头,另一种在结尾。 I tried this code below, but the color remains the same.我在下面尝试了此代码,但颜色保持不变。

class Loading extends StatefulWidget {
  const Loading({Key? key}) : super(key: key);
  static String tag = '/Loading';

  @override
  State<Loading> createState() => _LoadingState();
}

class _LoadingState extends State<Loading> with TickerProviderStateMixin {
  late AnimationController controller, colorController;
  late Animation<Offset> offset;
  late Animation animation;

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

    controller =
        AnimationController(duration: Duration(seconds: 1), vsync: this);
    Future.delayed(Duration(milliseconds: 100))
        .then((_) => controller.forward());

    offset = Tween<Offset>(begin: Offset(0.0, 10.0), end: Offset.zero)
        .animate(controller);

    colorController = AnimationController(
      duration:
          const Duration(milliseconds: 5000), //controll animation duration
      vsync: this,
    );

    animation = ColorTween(
      begin: Colors.grey,
      end: Colors.red,
    ).animate(colorController);
  }

  // @override
  // void dispose() {
  //   controller2.dispose();
  //   super.dispose();
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Align(
      alignment: Alignment.center,
      child: SlideTransition(
        position: offset,
        child: Icon(
          Icons.favorite,
          color: animation.value,
        ),
      ),
    ));
  }
}

You are missing to call forward on colorController and to change the UI use addListener to call setState .您缺少在colorController上调用forward并更改 UI 使用addListener来调用setState

colorController = AnimationController(
  duration: const Duration(milliseconds: 500),  
  vsync: this,
)
  ..addListener(() {
    setState(() {});
  })
  ..forward();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM