[Day24] Flutter with GetX Shimmer

Shimmer

iOS Swift的话是类似SkeletonView
一般用在等待的时候 像是API fetch data
这边用前几篇的News API, 把转圈圈修改成Shimmer

先是建立Shimmer widget class
再来是建立摆在ListView里面的 Shimmer cell (ListViewShimmer)
这样就可以直接替换成ListView效果

class ShimmerWidget extends StatelessWidget {
  final double width;
  final double height;
  late ShapeBorder shapeBorder;
  final double borderRadius;

  ShimmerWidget.rectangle({
    this.width = double.infinity,
    required this.height,
    this.borderRadius = 8,
  });

  @override
  Widget build(BuildContext context) {
    shapeBorder = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(borderRadius));
    return Shimmer.fromColors(
      child: Container(
        width: width,
        height: height,
        decoration: ShapeDecoration(
          color: Colors.grey[400]!,
          shape: shapeBorder,
        ),
      ),
      baseColor: Colors.grey[400]!,
      highlightColor: Colors.grey[200]!,
    );
  }
}

class ListViewShimmer extends StatelessWidget {
  ListViewShimmer({Key? key, this.cellCount = 3}) : super(key: key);

  final int cellCount;
  final cellWidth = Get.width * 0.95;

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        ListView.builder(
          physics: BouncingScrollPhysics(),
          shrinkWrap: true,
          itemCount: cellCount,
          itemBuilder: (BuildContext context, int index) {
            return Card(
              child: ListTile(
                title: ShimmerWidget.rectangle(height: 15),
                subtitle: ShimmerWidget.rectangle(height: 10),
              ),
            );
          },
        ),
      ],
    );
  }
}

本篇的GitHub source code

下一篇将为大家介绍Shimmer和 API fetch之间的切换
AnimatedSwitcher with API fetch


<<:  Day26 - 动态模型 part1 (LSTM)

>>:  用React刻自己的投资Dashboard Day23 - 非同步呼叫API,完成首页资料串接

[2020铁人赛] Day27 - 部署与相关设定

本章主要介绍将ASP.NET Core网站给发行上IIS服务器,我们可以用Visual Studio...

[Day29] 悬浮视窗提醒

这次要来教的是如何制作一个悬浮视窗的提醒,这个功能在许多目前市面上常用的APP上都能看到,例如:Li...

Day26 - HTML 与 CSS (8) - 背景图片

背景图片 background-image:使用 url(pic路径) 来显示图片 backgrou...

D25 Pytest 学习纪录-pytest规则跟常用固件

pytest档案命名规则 python 档名为 test_*.py 或 *_test.py meth...

Day30 Android - 回顾

反思 今天是铁人赛的最後一天了~ 说真的我挺开心能够完成这次的铁人赛并且跟着团体一起前进, 从我第一...