Flutter加载占位符动画插件shimmer_container的使用

Flutter加载占位符动画插件shimmer_container的使用

它将在您的应用中展示一个漂亮且可自定义的闪烁效果。您可以使用它来在从API加载数据时显示加载效果。

闪烁效果演示

平台支持

Android iOS MacOS Web Linux Windows
✔️ ✔️ ✔️ ✔️ ✔️ ✔️

使用此包作为库

添加依赖

运行以下命令:

$ dart pub add shimmer_container

或者在Flutter项目中:

$ flutter pub add shimmer_container

这将会在您的包的pubspec.yaml文件中添加一行(并运行隐式的dart pub get):

dependencies:
  shimmer_container: ^0.0.2

或者,您的编辑器可能支持dart pub getflutter pub get。请查阅您的编辑器文档以了解更多信息。

导入

现在可以在您的Dart代码中使用:

import 'package:shimmer_container/shimmer_container.dart';

如何使用

[@override](/user/override)
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: ShimmerContainer(
        height: 80,
        width: 350,
        radius: 4,
        highlightColor: Color(0xffF9F9FB),
        baseColor: Color(0xffE6E8EB),
      ),
    ),
  );
}

完整示例

import 'dart:async';

import 'package:flutter/material.dart';

import 'shimmer_container.dart'; // 假设插件已经正确安装

void main() {
  runApp(const MyApp());
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Shimmer Container Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Shimmer Container Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  void initState() {
    super.initState();
  }

  void safeSetState() {
    if (mounted) {
      setState(() {});
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            children: [
              ShimmerContainer(
                height: 80,
                width: 350,
                radius: 4,
                highlightColor: Color(0xffF9F9FB),
                baseColor: Color(0xffE6E8EB),
              ),
              SizedBox(height: 10,),
              ShimmerContainer(
                height: 80,
                width: 350,
                radius: 4,
                highlightColor: Color(0xffF9F9FB),
                baseColor: Color(0xffE6E8EB),
              ),
              SizedBox(height: 10,),
              ShimmerContainer(
                height: 80,
                width: 350,
                radius: 4,
                highlightColor: Color(0xffF9F9FB),
                baseColor: Color(0xffE6E8EB),
              ),
              SizedBox(height: 10,),
              ShimmerContainer(
                height: 80,
                width: 350,
                radius: 4,
                highlightColor: Color(0xffF9F9FB),
                baseColor: Color(0xffE6E8EB),
              ),
              SizedBox(height: 10,),
              ShimmerContainer(
                height: 80,
                width: 350,
                radius: 4,
                highlightColor: Color(0xffF9F9FB),
                baseColor: Color(0xffE6E8EB),
              ),
              SizedBox(height: 10,),
              ShimmerContainer(
                height: 80,
                width: 350,
                radius: 4,
                highlightColor: Color(0xffF9F9FB),
                baseColor: Color(0xffE6E8EB),
              ),
              SizedBox(height: 10,),
              ShimmerContainer(
                height: 80,
                width: 350,
                radius: 4,
                highlightColor: Color(0xffF9F9FB),
                baseColor: Color(0xffE6E8EB),
              ),
              SizedBox(height: 10,),
              ShimmerContainer(
                height: 80,
                width: 350,
                radius: 4,
                highlightColor: Color(0xffF9F9FB),
                baseColor: Color(0xffE6E8EB),
              ),
              SizedBox(height: 10,),
              ShimmerContainer(
                height: 80,
                width: 350,
                radius: 4,
                highlightColor: Color(0xffF9F9FB),
                baseColor: Color(0xffE6E8EB),
              ),
              SizedBox(height: 10,),
            ],
          ),
          scrollDirection: Axis.vertical,
        ),
      ),
    );
  }
}

更多关于Flutter加载占位符动画插件shimmer_container的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter加载占位符动画插件shimmer_container的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter中使用shimmer_container插件来加载占位符动画的示例代码。shimmer_container是一个用于创建占位符动画效果的Flutter插件,通常在数据加载时显示给用户一个视觉上的反馈。

首先,你需要在你的pubspec.yaml文件中添加shimmer_container依赖:

dependencies:
  flutter:
    sdk: flutter
  shimmer_container: ^x.y.z  # 请将x.y.z替换为最新版本号

然后运行flutter pub get来安装依赖。

接下来,在你的Flutter应用中,你可以使用ShimmerContainer来创建占位符动画。以下是一个完整的示例,展示如何在数据加载期间显示占位符动画:

import 'package:flutter/material.dart';
import 'package:shimmer_container/shimmer_container.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Shimmer Container Example'),
        ),
        body: Center(
          child: MyShimmerList(),
        ),
      ),
    );
  }
}

class MyShimmerList extends StatefulWidget {
  @override
  _MyShimmerListState createState() => _MyShimmerListState();
}

class _MyShimmerListState extends State<MyShimmerList> with SingleTickerProviderStateMixin {
  bool isDataLoaded = false;

  @override
  void initState() {
    super.initState();
    // 模拟数据加载
    Future.delayed(Duration(seconds: 2), () {
      setState(() {
        isDataLoaded = true;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        if (!isDataLoaded) {
          ShimmerContainer(
            height: 200,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                ShimmerItem(
                  baseColor: Colors.grey.withOpacity(0.3),
                  highlightColor: Colors.grey.withOpacity(0.6),
                  width: 200,
                  height: 20,
                ),
                SizedBox(height: 10),
                ShimmerItem(
                  baseColor: Colors.grey.withOpacity(0.3),
                  highlightColor: Colors.grey.withOpacity(0.6),
                  width: 150,
                  height: 20,
                ),
                SizedBox(height: 10),
                ShimmerItem(
                  baseColor: Colors.grey.withOpacity(0.3),
                  highlightColor: Colors.grey.withOpacity(0.6),
                  width: 200,
                  height: 20,
                ),
              ],
            ),
          ),
        } else {
          // 数据加载完成后显示的内容
          ListView.builder(
            shrinkWrap: true,
            itemCount: 10,
            itemBuilder: (context, index) {
              return ListTile(
                title: Text('Item ${index + 1}'),
              );
            },
          ),
        },
      ],
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个ShimmerContainer用于在数据加载期间显示占位符动画。当数据加载完成后(通过Future.delayed模拟),我们更新状态并显示实际的数据列表。

  • ShimmerContainer接收一个height参数来设置容器的高度,并且包含一个子Widget,通常是一个Column,里面包含多个ShimmerItem
  • ShimmerItem用于定义单个占位符动画项,你可以设置它的baseColor(基础颜色)和highlightColor(高亮颜色),以及宽度和高度。

通过这种方式,你可以在Flutter应用中轻松实现占位符动画效果,从而提升用户体验。

回到顶部