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 应用,包含一个蓝色的正方形,在正方形上应用了缩放动画。当用户点击正方形时,将会触发缩放效果,提供更加生动的用户体验。

相关推荐
RaidenLiu12 小时前
从 Provider 迈向 Riverpod 3:核心架构与迁移指南
前端·flutter
叽哥12 小时前
Flutter面试:Dart基础2
flutter·面试·dart
tangweiguo0305198716 小时前
Android原生(Kotlin)与Flutter混合开发 - 设备控制与状态同步解决方案
android·flutter
江上清风山间明月1 天前
Flutter AlwaysScrollableScrollPhysics详解
flutter·滚动·scrollable·scrollphysics
普罗米拉稀1 天前
Flutter 复用艺术:Mixin 与 Abstract 的架构哲学与线性化解密
flutter·ios·面试
yangshuo12812 天前
AI编程工具对决:Kilo vs Augment 开发Flutter俄罗斯方块游戏实战对比
flutter·游戏·ai编程
tangweiguo030519872 天前
Flutter 自定义 Switch 切换组件完全指南
flutter
笔沫拾光2 天前
iOS 正式包签名指南
flutter·ios·ios签名
森之鸟2 天前
flutter项目适配鸿蒙
flutter·华为·harmonyos
傅里叶2 天前
Flutter在OrangePi 5 Plus上视频播放锁死问题
前端·flutter