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('还原')),
        ],
      ),
    );
  }
}
相关推荐
coooliang4 小时前
Flutter 中的单例模式
javascript·flutter·单例模式
coooliang4 小时前
Flutter项目中设置安卓启动页
android·flutter
JIngles1234 小时前
flutter将utf-8编码的字节序列转换为中英文字符串
java·javascript·flutter
B.-7 小时前
在 Flutter 中实现文件读写
开发语言·学习·flutter·android studio·xcode
freflying111916 小时前
使用jenkins构建Android+Flutter项目依赖自动升级带来兼容性问题及Jenkins构建速度慢问题解决
android·flutter·jenkins
机器瓦力18 小时前
Flutter应用开发:对象存储管理图片
flutter
江上清风山间明月1 天前
Flutter最简单的路由管理方式Navigator
android·flutter·ios·路由·页面管理·navigator
weixin_411191841 天前
FlutterAssetsGenerator插件的使用
flutter
神秘_博士1 天前
自制AirTag,支持安卓/鸿蒙/PC/Home Assistant,无需拥有iPhone
arm开发·python·物联网·flutter·docker·gitee
陈皮话梅糖@2 天前
Flutter 网络请求与数据处理:从基础到单例封装
flutter·网络请求