Flutter UI骨架屏加载插件skeletor的使用
Flutter UI骨架屏加载插件skeletor的使用
Skeletor
提供了一种优雅的方式来展示骨架动画。
使用方法
-
将
Skeleton
包裹在内容之上Skeleton( isShown: true, // 控制是否显示骨架屏 child: content, // 需要显示骨架屏的内容 )
-
然后,使用
Bone
包裹需要显示骨架动画的组件-
单个骨骼
Bone( width: 300, // 宽度 height: 30, // 高度 child: Text("What is Lorem Ipsum?"), // 骨架屏下的实际内容 ),
-
多个骨骼
Bone.multiple( number: 8, // 骨骼的数量 spacing: 8, // 骨骼之间的间距 height: 30, // 骨骼的高度 variants: [1, 1, 0.95], // 骨骼的不透明度变化 borderRadius: BorderRadius.circular(8), // 骨骼的圆角 child: Text( "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"), )
-
隐藏一个组件
Bone.hidden(child: Text("A hidden text"))
-
完整示例Demo
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:skeletor/skeletor.dart';
class App extends StatefulWidget {
const App({Key? key, required this.content}) : super(key: key);
final Widget content;
[@override](/user/override)
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
bool isLoading = false; // 控制骨架屏的显示状态
[@override](/user/override)
Widget build(BuildContext context) {
return CupertinoApp(
home: CupertinoPageScaffold(
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Skeleton(
isShown: isLoading, // 根据状态决定是否显示骨架屏
child: widget.content,
),
CupertinoButton(
child: Text("toggle"), // 切换按钮
onPressed: () {
setState(() {
isLoading = !isLoading; // 改变状态
});
},
)
],
),
),
),
);
}
}
void main() {
runApp(
App(
content: SafeArea(
child: Container(
margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Bone(
width: 100, // 图片宽度
height: 100, // 图片高度
child: SizedBox(
width: 100,
height: 100,
child: Image(
image: NetworkImage(
"https://en.gravatar.com/userimage/137591909/6a42a5a20cd79d50edb957644bc41b0c.png?size=200"), // 网络图片
),
),
),
SizedBox(
width: 8, // 间隔
),
Bone.hidden(child: Text("MarkG")) // 隐藏的文字
],
),
SizedBox(
height: 16, // 间隔
),
Bone(
width: 300, // 文本框宽度
height: 30, // 文本框高度
borderRadius: BorderRadius.circular(8), // 圆角
child: Container(
height: 30,
child: Text(
"What is Lorem Ipsum?", // 文本内容
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
),
),
SizedBox(height: 16), // 间隔
Bone.multiple(
number: 8, // 骨骼数量
spacing: 8, // 骨骼间距
height: 30, // 骨骼高度
variants: [1, 1, 0.95], // 骨骼不透明度变化
borderRadius: BorderRadius.circular(8), // 圆角
child: SizedBox(
height: 8 * 7 + 8 * 30, // 计算总高度
child: Text(
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"),
),
)
],
),
),
),
),
);
}
更多关于Flutter UI骨架屏加载插件skeletor的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter UI骨架屏加载插件skeletor的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
Skeletor
是一个用于 Flutter 的骨架屏加载插件,它可以帮助你在数据加载时展示一个占位符效果,提升用户体验。以下是如何使用 Skeletor
插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 skeletor
依赖:
dependencies:
flutter:
sdk: flutter
skeletor: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 基本使用
Skeletor
提供了一个 Skeleton
小部件,你可以在需要展示骨架屏的地方使用它。
import 'package:flutter/material.dart';
import 'package:skeletor/skeletor.dart';
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Skeletor Example'),
),
body: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return Skeleton(
child: ListTile(
leading: CircleAvatar(),
title: Text('Loading...'),
subtitle: Text('Loading...'),
),
);
},
),
);
}
}
3. 自定义骨架屏
你可以通过 Skeleton
的 style
参数来自定义骨架屏的外观。
Skeleton(
style: SkeletonStyle(
baseColor: Colors.grey[300],
highlightColor: Colors.grey[100],
borderRadius: BorderRadius.circular(8),
shimmerDuration: Duration(seconds: 2),
),
child: ListTile(
leading: CircleAvatar(),
title: Text('Loading...'),
subtitle: Text('Loading...'),
),
);
4. 控制骨架屏的显示
你可以通过条件渲染来控制骨架屏的显示和隐藏。例如,当数据加载完成时,隐藏骨架屏并展示真实数据。
bool isLoading = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Skeletor Example'),
),
body: isLoading
? ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return Skeleton(
child: ListTile(
leading: CircleAvatar(),
title: Text('Loading...'),
subtitle: Text('Loading...'),
),
);
},
)
: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(),
title: Text('Item $index'),
subtitle: Text('Subtitle $index'),
);
},
),
);
}
5. 动画效果
Skeletor
默认提供了一个闪烁动画效果,你可以通过 shimmerDuration
参数来控制动画的速度。
Skeleton(
style: SkeletonStyle(
shimmerDuration: Duration(seconds: 3),
),
child: ListTile(
leading: CircleAvatar(),
title: Text('Loading...'),
subtitle: Text('Loading...'),
),
);