极简实现酷炫动效:Flutter隐式动画指南第二篇之一些酷炫的隐式动画效果

目录

前言

1.弹性放大按钮效果

2.旋转和缩放组合动画

3.颜色渐变背景动画

4.缩放进出效果


前言

在上一篇文章中,我们介绍了Flutter中的隐式动画的一些相关知识,在这篇文章中,我们可以结合多个隐式动画 Widget 在 Flutter 中创建一些酷炫的视觉效果,比如弹性按钮、旋转动画、颜色过渡等。以下示例展示如何组合不同的隐式动画,以实现更复杂的视觉效果。

1.弹性放大按钮效果

我们可以通过结合隐式动画的Widget组件并且通过设置动画的属性,实现一个弹簧效果。

效果如下:

图1.使用AnimatedContainer实现弹簧动画效果

核心代码如下:

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

class ElasticButtonExample extends StatefulWidget {
  const ElasticButtonExample({super.key});

  @override
  State<ElasticButtonExample> createState() => _ElasticButtonExampleState();
}

class _ElasticButtonExampleState extends State<ElasticButtonExample> {
  bool _isTapped = false;

  void _toggleSize() {
    setState(() {
      _isTapped = !_isTapped;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.purple,
        title: const Text('弹簧动画效果',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),),
      ),
      body: Center(
        child: GestureDetector(
          onTap: _toggleSize,
          child: AnimatedContainer(
            duration: const Duration(milliseconds: 200),
            curve: Curves.easeOutBack,
            width: _isTapped ? 120 : 100,
            height: _isTapped ? 120 : 100,
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.circular(20),
            ),
            child: const Center(
              child: Text(
                'Press Me',
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

2.旋转和缩放组合动画

点击按钮时实现颜色渐变、旋转和缩放组合动画,让 Widget 更加立体和动感。

在下面的例子中,我们通过结合AnimatedContainer的颜色渐变、旋转和缩放等动画属性实现一个组合的酷炫动画效果。

图2.旋转缩放组合动画效果

3.颜色渐变背景动画

实现点击屏幕时背景颜色渐变,带来柔和的视觉效果,适合用于内容页面切换时。

Flutter实现背景渐变动画效果

核心的代码如下:

Dart 复制代码
class GradientBackgroundExample extends StatefulWidget {
  @override
  _GradientBackgroundExampleState createState() =>
      _GradientBackgroundExampleState();
}

class _GradientBackgroundExampleState extends State<GradientBackgroundExample> {
  bool _isColorChanged = false;

  void _changeBackground() {
    setState(() {
      _isColorChanged = !_isColorChanged;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
        onTap: _changeBackground,
        child: AnimatedContainer(
          duration: const Duration(seconds: 1),
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: _isColorChanged
                  ? [Colors.purple, Colors.blue]
                  : [Colors.orange, Colors.red],
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
            ),
          ),
          child: const Center(
            child: Text(
              'Tap to Change Background',
              style: TextStyle(fontSize: 24, color: Colors.white),
            ),
          ),
        ),
      ),
    );
  }
}

4.缩放进出效果

创建一个 Widget 进入和退出时具有缩放动画效果,用于过渡显示内容。

例如我们要实现下面的效果图,主要的核心代码如下:

图4.缩放进出动画效果

主要的核心代码如下:

Dart 复制代码
class ScaleInOutExample extends StatefulWidget {
  @override
  _ScaleInOutExampleState createState() => _ScaleInOutExampleState();
}

class _ScaleInOutExampleState extends State<ScaleInOutExample> {
  bool _isVisible = true;

  void _toggleVisibility() {
    setState(() {
      _isVisible = !_isVisible;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Scale In/Out Example')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AnimatedOpacity(
              duration: const Duration(milliseconds: 500),
              opacity: _isVisible ? 1.0 : 0.0,
              child: AnimatedScale(
                duration: const Duration(milliseconds: 500),
                scale: _isVisible ? 1.0 : 0.5,
                child: Container(
                  width: 100,
                  height: 100,
                  color: Colors.blue,
                  child: const Center(
                    child: Text(
                      'Hello!',
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                  ),
                ),
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: _toggleVisibility,
              child: const Text('Toggle Visibility'),
            ),
          ],
        ),
      ),
    );
  }
}
相关推荐
程序员老刘1 天前
跨平台开发地图 | 2026年10月
flutter·ai编程·客户端
林瞅瞅1 天前
Flutter 开发环境从零搭建与调试记录
flutter
FungLeo2 天前
成为全栈·Flutter App 篇·Markdown 编辑器:编辑、预览与离开保护
flutter·移动开发·markdown编辑器·表单设计·成为全栈·草稿保存
FungLeo2 天前
成为全栈·Flutter App 篇·会员中心:资料、密码、通知与私有数据缓存
flutter·dart·身份认证·数据隔离·成为全栈·会员中心
穆梓兰煊2 天前
Flutter端云协同推理架构解析
flutter·github
FungLeo2 天前
成为全栈·Flutter App 篇·内置 WebView:网页历史、App 返回与外链安全
flutter·网络安全·移动开发·webview·路由管理·成为全栈
m0_738185823 天前
Flutter 鸿蒙化实战:volume_controller 适配 OpenHarmony,系统音量控制
flutter·华为·harmonyos·鸿蒙
FungLeo3 天前
成为全栈·Flutter App 篇·Flutter Markdown 阅读器:支持范围与渲染边界
flutter·markdown·dart·代码高亮·成为全栈·移动阅读
m0_738185823 天前
Flutter 鸿蒙化实战:webview_cookie_manager 适配 OpenHarmony,WebView Cookie 管理
flutter·华为·harmonyos·鸿蒙