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('还原')),
        ],
      ),
    );
  }
}
相关推荐
dragon72512 小时前
一个 Flutter Loading 关不掉的诡异 Bug:从 showDialog 的竞态说起
flutter
一枚菜鸟_12 小时前
在 Flutter 项目里装 Skills:从 0 搭建 AI 辅助开发体系
flutter·ai编程
浮江雾14 小时前
Flutter第五节------Flutter开发环境配置与项目创建指南
开发语言·前端·git·学习·flutter·入门·函数
qq_2468397519 小时前
记一次 Flutter 预热帧引起的初始化问题
android·flutter
心中有国也有家19 小时前
AtomGit Flutter 鸿蒙客户端:MoodStorage 的 CRUD 集成验证
android·服务器·flutter·华为·harmonyos
心中有国也有家20 小时前
AtomGit Flutter 鸿蒙客户端:错误处理与优雅降级策略
android·javascript·flutter·华为·harmonyos
西西学代码2 天前
Flutter---Text
flutter
左有道2 天前
用 Flutter 做一个更顺手的日历组件:calendarview_flutter
flutter
一枚菜鸟_2 天前
Flutter + Cursor 一天出 App?这 3 个坑让开发者多花了 3 小时
flutter·cursor
心中有国也有家2 天前
AtomGit Flutter 鸿蒙客户端:零外部资源的应用打包策略
android·javascript·flutter·华为·harmonyos