Flutter:AnimatedBuilder自定义显示动画

1、自定义显示动画,实现淡入淡出

js 复制代码
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{
  late AnimationController _controller;
  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500)
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('标题'),
      ),
      body: Column(
        children: [
          Container(
            child: AnimatedBuilder(
              animation: _controller,
              builder: (context,child){
                return Opacity(
                  // Tween定义显示范围0.5-1.0之前,animate(_controller).value 会在0- 1之间自动切换
                  opacity: Tween(begin: 0.5,end: 1.0).animate(_controller).value,
                  child: Container(
                    width: 200,
                    height: 200,
                    color: Colors.red,
                  ),
                );
              },
            ),
          ),
          ElevatedButton(onPressed: (){
            _controller.forward();
          }, child: const Text('显示')),
          ElevatedButton(onPressed: (){
            _controller.reverse();
          }, child: const Text('隐藏')),
        ],
      ),
    );
  }
}

2、自定义显示动画,实现位移

js 复制代码
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{
  late AnimationController _controller;
  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500)
    );
  }

  @override
  Widget build(BuildContext context) {
    // 可以通过追加chain定义动画曲线
    Animation x =
        Tween(begin: -100.0,end: 100.0)
        .chain(CurveTween(curve: Curves.bounceInOut))
        .animate(_controller);
    return Scaffold(
      appBar: AppBar(
        title: const Text('标题'),
      ),
      body: Column(
        children: [
          AnimatedBuilder(
            animation: _controller,
            builder: (context,value){
            return Container(
              width: 100,
              height: 100,
              color: Colors.red,
              // Tween(-100 到100 之间)进行动画
              // animate(_controller).value 从0到1 之间过度
              transform: Matrix4.translationValues(x.value, 0.0, 0.0),
            );
          }),
          ElevatedButton(onPressed: (){
            _controller.forward();
          }, child: const Text('移动')),
          ElevatedButton(onPressed: (){
            _controller.reverse();
          }, child: const Text('还原')),
        ],
      ),
    );
  }
}
相关推荐
用户965597361901 天前
Provider vs Bloc vs GetX vs Riverpod:Flutter 状态管理方案怎么选?
flutter
恋猫de小郭1 天前
Flutter Patchwork,不用 Fork 改依赖包源码的第三方工具
android·前端·flutter
程序员老刘2 天前
跑分第一的编程大模型,我为啥不用?
flutter·ai编程·vibecoding
恋猫de小郭2 天前
苹果 AirPods 协议,Android 也可以使用完整版 AirPods 能力
android·前端·flutter
张风捷特烈2 天前
Flutter 类库大揭秘#01 | path_provider架构与设计
android·flutter
恋猫de小郭5 天前
Android 限制侧载新进展,谷歌联合国内厂商推验证计划
android·前端·flutter
恋猫de小郭5 天前
解读 Android 17 全新内存限制,有没有“豁免”后门?
android·前端·flutter
程序员老刘7 天前
跨平台开发地图 | 2026年6月
flutter·ai编程·客户端
悟空瞎说8 天前
Flutter 架构详解:新手必懂底层原理
flutter
SoaringHeart8 天前
Flutter最佳实践:IM聊天文字链接自动识别跳转
前端·flutter