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('还原')),
        ],
      ),
    );
  }
}
相关推荐
GISer_Jing21 分钟前
跨端框架对决:React Native vs Flutter深度对比
flutter·react native·react.js
猪哥帅过吴彦祖4 小时前
Flutter 从入门到精通:深入 Navigator 2.0 - GoRouter 路由完全指南
android·flutter·ios
恋猫de小郭5 小时前
来了解一下,为什么你的 Flutter WebView 在 iOS 26 上有点击问题?
android·前端·flutter
你听得到111 天前
肝了半个月,我用 Flutter 写了个功能强大的图片编辑器,告别image_cropper
android·前端·flutter
旧时光_1 天前
第3章:基础组件 —— 3.2 按钮
flutter
旧时光_1 天前
第3章:基础组件 —— 3.1 文本及样式
flutter
旧时光_1 天前
第2章:第一个Flutter应用 —— 2.8 Flutter异常捕获
flutter
猪哥帅过吴彦祖1 天前
Flutter 系列教程:应用导航 - Navigator 1.0 与命名路由
android·flutter·ios
旧时光_1 天前
第2章:第一个Flutter应用 —— 2.7 调试Flutter应用
flutter
鹏多多1 天前
flutter图片选择库multi_image_picker_plus和image_picker的对比和使用解析
android·flutter·ios