Flutter骨架屏动画插件skeleton_animation的使用
Flutter骨架屏动画插件skeleton_animation的使用
flutter-skeleton-animation
插件可以创建简单的骨架屏动画,并且可以在 Android、iOS 和 Web 上使用。
以下是该插件在不同模式下的效果展示:
如何使用
首先,确保你已经将 skeleton_animation
添加到你的 pubspec.yaml
文件中:
dependencies:
skeleton_animation: ^2.0.0
然后,导入该包并使用以下代码:
import 'package:skeleton_animation/skeleton_animation.dart';
基础用法
你可以通过 Skeleton
组件来创建一个基础的骨架屏动画。例如:
Skeleton(
height: 12,
style: SkeletonStyle.text
),
或者使用 SkeletonText
组件:
SkeletonText(
height: 12
)
完整示例
以下是一个完整的示例,展示了如何在应用中使用 skeleton_animation
插件。
import 'package:flutter/material.dart';
import 'package:skeleton_animation/skeleton_animation.dart';
List tileList = [
[null, null],
[null, Colors.blue],
[null, Colors.blue.shade900],
[Colors.green, Colors.blue.shade900],
[null, Colors.green],
[null, Colors.green.shade900],
[null, Colors.red],
[null, Colors.red.shade200]
];
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('骨架动画'),
),
body: Container(
child: CustomScrollView(
slivers: [
SliverList(
delegate: SliverChildBuilderDelegate(
(context, i) => ListTile(
contentPadding: EdgeInsets.symmetric(horizontal: 16.0),
tileColor: tileList[i][1],
title: Skeleton(
style: SkeletonStyle.text,
textColor: tileList[i][0],
height: 14.0,
),
subtitle: Text(
'加载中的文本',
style: TextStyle(color: tileList[i][0]),
),
onTap: () {},
),
childCount: tileList.length),
)
],
)),
),
// 启用深色模式
darkTheme: ThemeData(brightness: Brightness.dark),
);
}
}
更多关于Flutter骨架屏动画插件skeleton_animation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter骨架屏动画插件skeleton_animation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter中使用skeleton_animation
插件来实现骨架屏动画的示例代码。这个插件可以帮助你在数据加载时显示一个占位符动画,以提升用户体验。
首先,确保你已经在pubspec.yaml
文件中添加了skeleton_animation
依赖:
dependencies:
flutter:
sdk: flutter
skeleton_animation: ^3.0.0 # 请根据需要替换为最新版本
然后运行flutter pub get
来安装依赖。
接下来,在你的Flutter项目中,你可以按照以下步骤实现骨架屏动画:
示例代码
import 'package:flutter/material.dart';
import 'package:skeleton_animation/skeleton_animation.dart';
import 'package:skeleton_animation/skeleton.dart';
import 'dart:async';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Skeleton Animation Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isLoading = true;
List<String> data = [];
@override
void initState() {
super.initState();
Timer(Duration(seconds: 2), () {
setState(() {
isLoading = false;
data = List.generate(20, (index) => "Item $index");
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Skeleton Animation Demo'),
),
body: isLoading
? _buildSkeletonScreen()
: ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(data[index]),
);
},
),
);
}
Widget _buildSkeletonScreen() {
return SkeletonAnimation(
child: ListView.builder(
itemCount: 20,
itemBuilder: (context, index) {
return SkeletonTile(
width: double.infinity,
height: 50,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Skeleton(
width: 40,
height: 40,
shape: BoxShape.circle,
animationDuration: Duration(milliseconds: 500),
),
SizedBox(width: 16),
Expanded(
child: Skeleton(
width: double.infinity,
height: 20,
animationDuration: Duration(milliseconds: 500),
),
),
],
),
),
);
},
),
);
}
}
解释
-
依赖安装:在
pubspec.yaml
中添加skeleton_animation
依赖,并运行flutter pub get
。 -
主应用结构:
MyApp
是根组件,MyHomePage
是主页面。 -
数据加载模拟:在
_MyHomePageState
的initState
方法中,使用Timer
模拟数据加载过程,2秒后更新数据状态。 -
骨架屏构建:
_buildSkeletonScreen
方法使用SkeletonAnimation
和SkeletonTile
组件构建骨架屏。每个SkeletonTile
包含一个圆形头像和一个文本占位符。 -
数据展示:当数据加载完成后,使用
ListView.builder
展示真实数据。
这个示例展示了如何使用skeleton_animation
插件在Flutter应用中实现骨架屏动画,以提升用户等待数据加载时的体验。你可以根据实际需求调整骨架屏的布局和动画效果。