Flutter交错动画效果插件staggered_animated_widget的使用

Flutter交错动画效果插件staggered_animated_widget的使用

特性

  • 提供一种简单的方法来为Flutter小部件添加交错动画。
  • 支持多种动画方向:从顶部、底部、左侧或右侧进入。
  • 易于使用且支持可自定义的延迟。
  • 添加了对可自定义持续时间和曲线属性的支持。

开始使用

要使用此包,请将其添加到您的pubspec.yaml文件中:

dependencies:
  staggered_animated_widget: ^2.0.2 

使用方法

以下是一个简单的示例,展示如何使用StaggeredAnimatedWidget

import 'package:flutter/material.dart';
import 'package:staggered_animated_widget/animation_direction.dart';
import 'package:staggered_animated_widget/staggered_animated_widget.dart';

void main() {
  runApp(const StaggeredAnimatedWidgetExample());
}

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

  @override
  State<StaggeredAnimatedWidgetExample> createState() => _StaggeredAnimatedWidgetExampleState();
}

class _StaggeredAnimatedWidgetExampleState extends State<StaggeredAnimatedWidgetExample> {
  late bool _showSecond = false;
  late bool _showThird = false;
  late bool _showFourth = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(15.0),
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                StaggeredAnimatedWidget(
                  delay: 200,
                  curve: Curves.fastLinearToSlowEaseIn,
                  direction: AnimationDirection.fromTop,
                  duration: const Duration(milliseconds: 1000),
                  child: SizedBox(
                    height: 70,
                    width: double.infinity,
                    child: MaterialButton(
                      onPressed: () {
                        setState(() {
                          _showSecond = true;
                        });
                      },
                      color: Colors.blue,
                      child: const Text(
                        'Show Second ^_^',
                        style: TextStyle(fontSize: 18),
                      ),
                    ),
                  ),
                ),
                const SizedBox(height: 20),
                _showSecond
                    ? StaggeredAnimatedWidget(
                        delay: 200,
                        curve: Curves.linearToEaseOut,
                        direction: AnimationDirection.fromRight,
                        duration: const Duration(milliseconds: 1000),
                        child: SizedBox(
                          height: 70,
                          width: double.infinity,
                          child: MaterialButton(
                            onPressed: () {
                              setState(() {
                                _showThird = true;
                              });
                            },
                            color: Colors.red,
                            child: const Text(
                              'Show Third',
                              style: TextStyle(fontSize: 18),
                            ),
                          ),
                        ),
                      )
                    : const SizedBox(),
                const SizedBox(height: 20),
                _showThird
                    ? StaggeredAnimatedWidget(
                        delay: 200,
                        curve: Curves.easeInCubic,
                        direction: AnimationDirection.fromLeft,
                        duration: const Duration(milliseconds: 1000),
                        child: SizedBox(
                          height: 70,
                          width: double.infinity,
                          child: MaterialButton(
                            onPressed: () {
                              setState(() {
                                _showFourth = true;
                              });
                            },
                            color: Colors.yellow,
                            child: const Text(
                              'Show Last',
                              style: TextStyle(fontSize: 18),
                            ),
                          ),
                        ),
                      )
                    : const SizedBox(),
                const SizedBox(height: 20),
                _showFourth
                    ? StaggeredAnimatedWidget(
                        delay: 200,
                        curve: Curves.bounceIn,
                        direction: AnimationDirection.fromBottom,
                        duration: const Duration(milliseconds: 200),
                        child: SizedBox(
                          height: 70,
                          width: double.infinity,
                          child: MaterialButton(
                            onPressed: () {},
                            color: Colors.green,
                            child: const Text(
                              'Last ^_^',
                              style: TextStyle(fontSize: 18),
                            ),
                          ),
                        ),
                      )
                    : const SizedBox(),
                const SizedBox(height: 20),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

参数

  • child: 需要进行动画的小部件。
  • delay: 动画开始之前的延迟时间(以毫秒为单位)。
  • direction: 小部件进入的方向。可以是以下之一:
    • AnimationDirection.fromTop
    • AnimationDirection.fromBottom
    • AnimationDirection.fromLeft
    • AnimationDirection.fromRight
  • duration: 动画的持续时间(默认为800毫秒)。
  • curve: 动画使用的曲线(默认为Curves.easeInOut)。

其他信息

对于更复杂的动画,可以在StaggeredAnimatedWidget内部自定义durationcurve属性。对于详细用法,建议在包的/example文件夹中添加更多示例。

许可证

该包受MIT许可证保护。

联系方式


更多关于Flutter交错动画效果插件staggered_animated_widget的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter交错动画效果插件staggered_animated_widget的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter中使用staggered_animated_widget插件来实现交错动画效果的示例代码。这个插件允许你以交错的方式对列表项进行动画处理,从而创建出更加吸引人的视觉效果。

首先,你需要在你的pubspec.yaml文件中添加staggered_animated_widget的依赖:

dependencies:
  flutter:
    sdk: flutter
  staggered_animated_widget: ^0.1.0  # 请检查最新版本号

然后运行flutter pub get来安装依赖。

接下来,你可以在你的Flutter项目中实现交错动画效果。以下是一个完整的示例代码:

import 'package:flutter/material.dart';
import 'package:staggered_animated_widget/staggered_animated_widget.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Staggered Animated Widget Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  List<String> items = List.generate(20, (index) => "Item ${index + 1}");
  bool isLoading = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Staggered Animated Widget Demo'),
      ),
      body: isLoading
          ? Center(child: CircularProgressIndicator())
          : StaggeredAnimationList(
              animationType: StaggeredAnimationType.fadeIn,
              crossAxisCount: 2,
              itemCount: items.length,
              itemBuilder: (context, index) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Card(
                    child: ListTile(
                      title: Text(items[index]),
                    ),
                  ),
                );
              },
            ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            isLoading = true;
            // 模拟加载数据
            Future.delayed(Duration(seconds: 2), () {
              setState(() {
                isLoading = false;
                // 更新数据(这里只是重复添加相同的数据作为示例)
                items.addAll(List.generate(10, (index) => "New Item ${items.length + index + 1}"));
              });
            });
          });
          },
          tooltip: 'Add Items',
          child: Icon(Icons.add),
        ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个StaggeredAnimationList,它展示了如何以交错的方式对列表项进行动画处理。以下是几个关键点:

  1. StaggeredAnimationList: 这是一个自定义的ListView,它支持交错动画效果。
  2. animationType: 你可以指定动画的类型,比如fadeInslideInLeft等。
  3. crossAxisCount: 指定网格的列数。
  4. itemCount: 列表项的数量。
  5. itemBuilder: 用于构建每个列表项的回调。

此外,我们还添加了一个FloatingActionButton,用于模拟加载更多数据并更新列表。每次点击按钮时,都会触发一个加载动画,然后添加新的列表项到items列表中,并重新渲染StaggeredAnimationList

你可以根据需要调整动画类型、列数和其他参数,以实现不同的交错动画效果。

回到顶部