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,
                          ),
                        ),
                      ],
                    ))
              ],
            )),
      ),
    );
  }
}
相关推荐
kirk_wang1 小时前
Flutter艺术探索-Flutter调试工具:DevTools使用指南
flutter·移动开发·flutter教程·移动开发教程
小雨下雨的雨3 小时前
Flutter 框架跨平台鸿蒙开发 —— SingleChildScrollView 控件之长内容滚动艺术
flutter·ui·华为·harmonyos·鸿蒙
牛马1113 小时前
Flutter 多语言
前端·flutter
getapi4 小时前
在 Google Play 上更新你已上架的 Flutter 应用
flutter·googlecloud·web app
奋斗的小青年!!6 小时前
Flutter跨平台开发适配OpenHarmony:文件系统操作深度实践
flutter·harmonyos·鸿蒙
西西学代码7 小时前
Flutter---路径管理器项目
flutter
奋斗的小青年!!7 小时前
Flutter跨平台开发OpenHarmony应用:个人中心实现
开发语言·前端·flutter·harmonyos·鸿蒙
LawrenceLan7 小时前
Flutter 零基础入门(十五):继承、多态与面向对象三大特性
开发语言·前端·flutter·dart
Rysxt_8 小时前
Flutter与UniApp底层逻辑深度对比
flutter·uni-app