Flutter动画列表插件animated_list的使用

Flutter 动画列表插件 animated_list 的使用

Animated List

测试

这是一个允许你轻松为列表添加过渡和发光效果的包。

使用的包

  • ✅ shimmer: ^3.0.0
  • ✅ flutter_staggered_animations: ^1.1.1

如何使用

在你的 pubspec.yaml 文件中添加依赖项:

dependencies:
  animated_list: ^0.0.4

实现到你的项目的示例代码:

import 'package:flutter/material.dart'; // 导入必要的库
import 'package:animated_list/animated_list.dart'; // 导入 animated_list 包

class MyWidget extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Animated List 示例'),
      ),
      body: Center(
        child: ListAnimated(
          text: "Animated List Item", // 文本
          icon: Icons.star, // 图标
          colorBorder: Colors.grey, // 边框颜色
          colorShine: Colors.amber, // 发光颜色
          duration: Duration(seconds: 2), // 持续时间
          horizontalOffset: 500.0, // 水平偏移
          colorIcon: Colors.red, // 图标颜色
          colorText: Colors.black, // 文本颜色
        ),
      ),
    );
  }
}

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

1 回复

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


当然,以下是一个关于如何使用Flutter的AnimatedList插件来实现动画列表的示例代码。AnimatedList是一个非常有用的Flutter组件,它允许你在列表项插入、删除或移动时添加动画效果。

首先,确保你的pubspec.yaml文件中已经包含了Flutter SDK的依赖:

dependencies:
  flutter:
    sdk: flutter

接下来,是一个完整的示例代码,展示如何使用AnimatedList

import 'package:flutter/material.dart';

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

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

class AnimatedListDemo extends StatefulWidget {
  @override
  _AnimatedListDemoState createState() => _AnimatedListDemoState();
}

class _AnimatedListDemoState extends State<AnimatedListDemo> {
  final List<String> _items = List<String>.generate(10, (i) => "Item $i");
  final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();

  void _removeItem(int index) {
    _items.removeAt(index);
    final AnimatedListState listState = _listKey.currentState;
    listState?.removeItem(index, (context, animation) =>
      _buildItem(context, animation, _items[index])
    );
  }

  void _addItem() {
    int index = _items.length;
    _items.insert(index, "Item $_items.length");
    final AnimatedListState listState = _listKey.currentState;
    listState?.insertItem(index, duration: Duration(milliseconds: 200));
  }

  Widget _buildItem(BuildContext context, Animation<double> animation, String item) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: Offset(0.0, 0.5),
        end: Offset.zero,
      ).animate(animation),
      child: Dismissible(
        key: ValueKey<String>(item),
        onDismissed: (direction) {
          _removeItem(_items.indexOf(item));
        },
        background: Container(color: Colors.red),
        child: ListTile(
          title: Text(item),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AnimatedList Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          children: <Widget>[
            ElevatedButton(
              onPressed: _addItem,
              child: Text('Add Item'),
            ),
            Expanded(
              child: AnimatedList(
                key: _listKey,
                initialItemCount: _items.length,
                itemBuilder: (context, index, animation) {
                  return _buildItem(context, animation, _items[index]);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

代码解释:

  1. 创建状态管理

    • AnimatedListDemo是一个有状态的widget,它管理一个字符串列表_items
    • 使用GlobalKey<AnimatedListState>来访问AnimatedList的状态。
  2. 删除项

    • _removeItem方法从列表中移除一个项,并通知AnimatedList进行动画删除。
  3. 添加项

    • _addItem方法在列表末尾添加一个项,并通知AnimatedList进行动画插入。
  4. 构建项

    • _buildItem方法使用SlideTransitionTween来创建滑动动画效果。
    • Dismissible widget允许用户通过滑动来删除列表项。
  5. UI布局

    • Scaffold包含了一个AppBar和一个Column,其中包含一个按钮用于添加项,以及一个AnimatedList用于显示动画列表。

这个示例展示了如何使用AnimatedList来实现列表项的插入和删除动画,以及如何通过Dismissible widget来实现滑动删除功能。希望这个示例对你有所帮助!

回到顶部