Flutter占位符加载插件skeletons_forked的使用
Flutter占位符加载插件skeletons_forked的使用
skeletons_forked
是一个用于构建自定义骨架小部件(Skeleton Widgets)的 Flutter 包,可以在页面加载时模仿页面布局。
示例
以下是 skeletons_forked
插件的一些示例图:
Items | Paragraphs | ListView (Default) |
---|---|---|
![]() |
![]() |
![]() |
ListView (Custom) | ListView (Complex Cards) | SkeletonTheme |
---|---|---|
![]() |
![]() |
![]() |
Light/Dark modes | Right-To-Left | Custom Shimmer |
---|---|---|
![]() |
![]() |
![]() |
所有示例可以在这里找到:https://github.com/Guihgo/skeletons_forked/tree/master/example/lib/examples。
如何使用
Skeleton
小部件可以通过包裹子小部件来使用:
import 'package:skeletons_forked/skeletons_forked.dart';
Skeleton(
isLoading: _isLoading,
skeleton: SkeletonListView(),
child: Container(
child: Center(
child: Text("Content"),
),
),
)
或者直接使用:
Container(
child: _isLoading
? SkeletonListView()
: Container(
child: Center(
child: Text("Content"),
),
),
)
还可以通过 SkeletonTheme
设置默认配置:
SkeletonTheme(
shimmerGradient: LinearGradient(
colors: [
Color(0xFFD8E3E7),
Color(0xFFC8D5DA),
Color(0xFFD8E3E7),
],
stops: [
0.1,
0.5,
0.9,
],
),
darkShimmerGradient: LinearGradient(
colors: [
Color(0xFF222222),
Color(0xFF242424),
Color(0xFF2B2B2B),
Color(0xFF242424),
Color(0xFF222222),
],
stops: [
0.0,
0.2,
0.5,
0.8,
1,
],
begin: Alignment(-2.4, -0.2),
end: Alignment(2.4, 0.2),
tileMode: TileMode.clamp,
),
child: MaterialApp(
...
),
)
更多定制化
对于更复杂的形状,可以在 SkeletonItem
小部件内构建你的骨架:
ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: 5,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Colors.white),
child: SkeletonItem(
child: Column(
children: [
Row(
children: [
SkeletonAvatar(
style: SkeletonAvatarStyle(
shape: BoxShape.circle, width: 50, height: 50),
),
SizedBox(width: 8),
Expanded(
child: SkeletonParagraph(
style: SkeletonParagraphStyle(
lines: 3,
spacing: 6,
lineStyle: SkeletonLineStyle(
randomLength: true,
height: 10,
borderRadius: BorderRadius.circular(8),
minLength: MediaQuery.of(context).size.width / 6,
maxLength: MediaQuery.of(context).size.width / 3,
)),
),
)
],
),
SizedBox(height: 12),
SkeletonParagraph(
style: SkeletonParagraphStyle(
lines: 3,
spacing: 6,
lineStyle: SkeletonLineStyle(
randomLength: true,
height: 10,
borderRadius: BorderRadius.circular(8),
minLength: MediaQuery.of(context).size.width / 2,
)),
),
SizedBox(height: 12),
SkeletonAvatar(
style: SkeletonAvatarStyle(
width: double.infinity,
minHeight: MediaQuery.of(context).size.height / 8,
maxHeight: MediaQuery.of(context).size.height / 3,
),
),
SizedBox(height: 8),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
SkeletonAvatar(style: SkeletonAvatarStyle(width: 20, height: 20)),
SizedBox(width: 8),
SkeletonAvatar(style: SkeletonAvatarStyle(width: 20, height: 20)),
SizedBox(width: 8),
SkeletonAvatar(style: SkeletonAvatarStyle(width: 20, height: 20)),
],
),
SkeletonLine(
style: SkeletonLineStyle(
height: 16,
width: 64,
borderRadius: BorderRadius.circular(8)),
)
],
)
],
),
),
),
),
);
更多关于Flutter占位符加载插件skeletons_forked的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复