flutter:占位视图(骨架屏、shimmer)

前言

有时候打开美团,在刚加载数据时会显示一个占位视图,如下:

那么这个是如何实现的呢?我们可以使用shimmer来开发该功能

实现

官方文档
https://pub-web.flutter-io.cn/packages/shimmer

安装

dart 复制代码
flutter pub add shimmer

示例1

dart 复制代码
SizedBox(
  width: 200.0,
  height: 100.0,
  child: Shimmer.fromColors(
    baseColor: Colors.red,
    highlightColor: Colors.yellow,
    child: Text(
      'Shimmer',
      textAlign: TextAlign.center,
      style: TextStyle(
        fontSize: 40.0,
        fontWeight:
        FontWeight.bold,
      ),
    ),
  ),
);

示例2

dart 复制代码
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: Text(widget.title),
        ),
        body: ListView(
          children: const [
            ProductDisplay(),
            ProductDisplay(),
            ProductDisplay(),
            ProductDisplay(),
          ],
        ));
  }
}

class ProductDisplay extends StatelessWidget {
  const ProductDisplay({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 150,
      width: MediaQuery.of(context).size.width,
      margin: const EdgeInsets.all(10),
      child: Card(
        color: Colors.white,
        child: Shimmer.fromColors(
            baseColor: Colors.grey.shade300,
            highlightColor: Colors.grey.shade100,
            child: Row(
              children: [
                Container(
                  width: 120,
                  height: 130,
                  margin: const EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: Colors.grey,
                  ),
                ),
                Expanded(
                    child: Column(
                      children: [
                        Container(
                          height:30,
                          margin: const EdgeInsets.all(10),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(5),
                            color: Colors.grey,
                          ),

                        ),
                        Container(
                          height:20,
                          margin: const EdgeInsets.only(left: 10,right: 10,bottom: 10),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(5),
                            color: Colors.grey,
                          ),
                        ),
                        Container(
                          height:20,
                          margin: const EdgeInsets.only(left: 10,right: 50,bottom: 10),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(5),
                            color: Colors.grey,
                          ),
                        ),
                        Container(
                          height:20,
                          margin: const EdgeInsets.only(left: 10,right: 100,bottom: 10),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(5),
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ))
              ],
            )),
      ),
    );
  }
}
相关推荐
molihuan11 小时前
最新 spine 4.3 flutter 适配鸿蒙
flutter·动画·harmonyos·鸿蒙·spine
GitLqr11 小时前
StatefulWidget 里的隐形炸弹:为什么不要在 State 类中使用 context.mounted
flutter·面试·dart
恋猫de小郭2 天前
Android R8 为什么可以让 Kotlin 协程提速 2 倍?
android·前端·flutter
杉氧2 天前
弹性滚动的奥秘:在 Flutter 中使用 CustomScrollView 与 Sliver 打造极致流畅列表
android·前端·flutter
张风捷特烈2 天前
Flutter UI 解耦 - 天下大势,合久必分, 分久必合
android·前端·flutter
Patrick_Wilson2 天前
从 React 到 Flutter:写给前端的一张跨端知识地图
前端·flutter·react.js
奎叔2 天前
Flutter 分层架构:从页面堆叠到可演进的业务边界
flutter
奎叔2 天前
Flutter 客户端 Trace 与稳定性治理:从链路追踪到灰度、降级和复盘
flutter
GitLqr2 天前
别被“Flutter 传感器延迟 150ms”带偏了:这可能只是你的实现方式错了
flutter·架构·kotlin
杉氧2 天前
Flutter 像素级还原实战:用 CustomPaint 与 Bezier 曲线手绘精致图针
android·前端·flutter