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,
                          ),
                        ),
                      ],
                    ))
              ],
            )),
      ),
    );
  }
}
相关推荐
GISer_Jing1 小时前
跨平台Hybrid App开发实战指南
android·flutter·react native
猫林老师12 小时前
Flutter for HarmonyOS开发指南(八):国际化与本地化深度实践
flutter·华为·harmonyos
dragon72517 小时前
FutureProvider会刷新两次的问题研究
前端·flutter
2501_9159090619 小时前
Flutter 应用怎么加固,多工具组合的工程化实战(Flutter 加固/Dart 混淆/IPA 成品加固/Ipa Guard + CI)
android·flutter·ios·ci/cd·小程序·uni-app·iphone
猪哥帅过吴彦祖1 天前
Flutter 从入门到精通:状态管理入门 - setState 的局限性与 Provider 的优雅之道
android·flutter·ios
天天开发1 天前
Flutter 每日库:轻松监听网络变化,就靠 connectivity_plus!
flutter
猫林老师1 天前
Flutter for HarmonyOS开发指南(七):插件开发与平台能力桥接
flutter·华为·harmonyos
Sindyue2 天前
flutter项目老是卡在Running Gradle task ‘assembleRelease‘......
flutter
西西学代码2 天前
Flutter---泛型
flutter
写不完的程序2 天前
Flutter 3.38 版本发布了,看看有哪些新特性
flutter