Flutter列表动画插件pretty_list_animation的使用
Flutter列表动画插件pretty_list_animation的使用
示例
以下是使用PrettyListAnimation
的完整示例代码:
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:pretty_list_animation/pretty_list_animation.dart';
void main() {
runApp(const MyApp());
}
/// 主类,应用的入口点
class MyApp extends StatelessWidget {
const MyApp({super.key});
/// 构建方法,返回应用的主要视图组件
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
/// 配置应用主题
theme: ThemeData(useMaterial3: true),
/// 主要展示的页面
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
[@override](/user/override)
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
/// 声明一个类型为int的列表变量 'numbers' 并初始化为空
late List<int> numbers = [];
[@override](/user/override)
void initState() {
setState(() {
numbers = [0, 1, 2];
});
super.initState();
}
/// 方法用于向 'numbers' 列表中添加数字
_addNumber() {
setState(() {
numbers = numbers +
[
/// 向 'numbers' 列表中添加随机数
Random().nextInt(100),
];
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('示例')),
body: PrettyListAnimation(
onRefresh: () async {},
padding: const EdgeInsets.all(16),
items: numbers,
/// 方法用于显示 'numbers' 列表中的每个项目
itemBuilder: (context, number, index, animation) {
/// 可以根据需要调整动画组件,可以使用flutter提供的各种动画组件
/// 例如:[SizeTransition] 和 [FadeTransition]
return SizeTransition(
sizeFactor: animation,
child: SizedBox(
height: 80.0,
child: Card(
color: Color.fromRGBO(
Random().nextInt(256),
Random().nextInt(256),
Random().nextInt(256),
1,
),
child: Center(
child: Text('Item $number'),
),
),
),
);
},
isInfinite: true,
isTextIndicator: false,
indicatorType: IndicatorType.pacman,
size: 30,
),
floatingActionButton: FloatingActionButton(
heroTag: '1',
onPressed: () {
_addNumber();
},
child: const Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
);
}
}
更多关于Flutter列表动画插件pretty_list_animation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复