Flutter 缩放动画组件封装与使用

在 Flutter 中,动画是为了提升用户体验而不可或缺的一部分。在应用程序中,缩放动画是常用的一种交互效果,可以使界面元素在用户交互时具有生动感。为了更好地组织代码和提高复用性,我们可以封装一个缩放动画组件,以下是一个简单的封装示例:

dart 复制代码
import 'package:flutter/material.dart';

class ScaleAnimationWidget extends StatefulWidget {
  final Widget child;

  const ScaleAnimationWidget({Key? key, required this.child}) : super(key: key);

  @override
  _ScaleAnimationWidgetState createState() => _ScaleAnimationWidgetState();
}

class _ScaleAnimationWidgetState extends State<ScaleAnimationWidget>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _scaleAnimation;

  @override
  void initState() {
    super.initState();

    // 初始化动画控制器
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 300),
    );

    // 定义缩放动画
    _scaleAnimation = Tween<double>(begin: 1.0, end: 0.8).animate(
      CurvedAnimation(
        parent: _animationController,
        curve: Curves.easeInOut,
      ),
    );
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (_) {
        // 手指按下时启动缩放动画
        _animationController.forward();
      },
      onTapUp: (_) {
        // 手指抬起时反向播放缩放动画
        _animationController.reverse();
      },
      onTapCancel: () {
        // 手指取消点击时反向播放缩放动画
        _animationController.reverse();
      },
      child: ScaleTransition(
        scale: _scaleAnimation,
        child: widget.child,
      ),
    );
  }
}

如何使用

在使用缩放动画组件时,只需将要添加动画效果的 Widget 作为 child 传入 ScaleAnimationWidget 中即可。以下是一个简单的示例:

dart 复制代码
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Scale Animation Example'),
        ),
        body: Center(
          child: ScaleAnimationWidget(
            child: Container(
              width: 100,
              height: 100,
              color: Colors.blue,
              child: Center(
                child: Text(
                  'Tap me!',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的 Flutter 应用,包含一个蓝色的正方形,在正方形上应用了缩放动画。当用户点击正方形时,将会触发缩放效果,提供更加生动的用户体验。

相关推荐
只可远观11 小时前
Flutter目录结构介绍、入口、Widget、Center组件、Text组件、MaterialApp组件、Scaffold组件
前端·flutter
周胡杰11 小时前
组件导航 (HMRouter)+flutter项目搭建-混合开发+分栏效果
前端·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
肥肥呀呀呀12 小时前
flutter Stream 有哪两种订阅模式。
flutter
WDeLiang20 小时前
Flutter - 集成三方库:日志(logger)
flutter·dart
hudawei99620 小时前
flutter缓存网络视频到本地,可离线观看
flutter·缓存·音视频
0wioiw02 天前
Flutter基础()
flutter
肥肥呀呀呀2 天前
flutter 视频通话flutter_webrtc
flutter
明似水2 天前
2025年Flutter项目管理技能要求
flutter
肥肥呀呀呀2 天前
flutter使用命令生成BinarySize分析图
flutter
程序猿阿伟2 天前
《数字分身进化论:React Native与Flutter如何打造沉浸式虚拟形象编辑》
flutter·react native·react.js