Futter中实现自定义动画,请解释一下AnimatedBuilder和Tween的概念

发布于 1周前 作者 itying888 最后一次编辑是 5天前 来自 分享

使用TweenAnimationBuilder自定义隐式动画,Tween可以定义动画区间


class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  @override
  void initState() {
    super.initState();
    _controller =
        AnimationController(vsync: this, duration: const Duration(seconds: 1))
          ..repeat(reverse: true);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Title'),
      ),
      body: Center(
        child: AnimatedBuilder(
          animation: _controller,
          builder: (BuildContext context, Widget? child) {
            return Opacity(
              opacity: Tween(begin: 0.5,end: 1.0).animate(_controller).value, //从0.5到1的变化
              child: Container(
                width: 200,
                height: 200,
                color: Colors.red,
                child: const Text("我是一个text组件"),
              ),
            );
          },
        ),
      ),
    );
  }
}

更多Flutter学习资料网盘下载地址:http://bbs.itying.com/topic/620268d1a4bcc00fe8e9d6e1

回到顶部