Flutter占位符加载插件flutter_skeleton_plus的使用

Flutter占位符加载插件flutter_skeleton_plus的使用

Flutter Skeleton Plus: Effortless Shimmering Placeholders

Flutter Skeleton Plus 是一个用于在您的 Flutter 应用中创建平滑动画占位符(也称为闪烁效果)的包。这些占位符在应用程序加载数据时为用户提供视觉反馈,使等待过程感觉更短,并提供更精致的体验。

为什么使用 Flutter Skeleton Plus?

  • 易于使用:只需导入预构建的骨架控件并根据您的应用外观进行自定义。
  • 流畅的动画:引人入胜的闪烁效果保持用户参与。
  • 灵活:轻松构建复杂的加载状态。
  • 维护良好:由 Apps Lanka 开发,定期更新和维护。

如何开始

  1. 安装:将 flutter_skeleton_plus 添加到您的 pubspec.yaml 文件中。
  2. 导入import 'package:flutter_skeleton_plus/flutter_skeleton_plus.dart';
  3. 使用:在数据加载时,将内容包裹在 Skeleton 控件中。

使用示例

可以使用以下方式封装子控件:

import 'package:flutter_skeleton_plus/flutter_skeleton_plus.dart';

Skeleton(
    isLoading: _isLoading,
    skeleton: SkeletonListView(),
    child: Container(child: Center(child: Text("Content"))),
)

或者直接使用:

Container(
    child: _isLoading
    ? SkeletonListView()
    : Container(child: Center(
                       child: Text("Content"))
        ),
)

还可以使用 SkeletonTheme 来设置树中所有骨架后代的默认配置:

SkeletonTheme(
    // themeMode: ThemeMode.light,
    shimmerGradient: LinearGradient(
        colors: [
          Color(0xFFD8E3E7),
          Color(0xFFC8D5DA),
          Color(0xFFD8E3E7),
        ],
        stops: [
          0.1,
          0.5,
          0.9,
        ],
      ),
      darkShimmerGradient: LinearGradient(
        colors: [
          Color(0xFF222222),
          Color(0xFF242424),
          Color(0xFF2B2B2B),
          Color(0xFF242424),
          Color(0xFF222222),
        ],
        stops: [
          0.0,
          0.2,
          0.5,
          0.8,
          1.0,
        ],
        begin: Alignment(-2.4, -0.2),
        end: Alignment(2.4, 0.2),
        tileMode: TileMode.clamp,
      ),
      child: MaterialApp(
        ...
      ),
)

更多定制

对于更复杂的形状,可以在 SkeletonItem 控件内部构建骨架:

ListView.builder(
  physics: NeverScrollableScrollPhysics(),
  itemCount: 5,
  itemBuilder: (context, index) => Padding(
    padding: const EdgeInsets.all(8.0),
    child: Container(
      padding: const EdgeInsets.all(8.0),
      decoration: BoxDecoration(color: Colors.white),
      child: SkeletonItem(
          child: Column(
            children: [
              Row(
                children: [
                  SkeletonAvatar(
                    style: SkeletonAvatarStyle(
                      shape: BoxShape.circle, width: 50, height: 50),
                  ),
                  SizedBox(width: 8),
                  Expanded(
                    child: SkeletonParagraph(
                      style: SkeletonParagraphStyle(
                        lines: 3,
                        spacing: 6,
                        lineStyle: SkeletonLineStyle(
                          randomLength: true,
                          height: 110,
                          borderRadius: BorderRadius.circular(8),
                          minLength: MediaQuery.of(context).size.width / 6,
                          maxLength: MediaQuery.of(context).size.width / 3,
                        )),
                    ),
                  )
                ],
              ),
              SizedBox(height: 12),
              SkeletonParagraph(
                style: SkeletonParagraphStyle(
                  lines: 3,
                  spacing: 6,
                  lineStyle: SkeletonLineStyle(
                    randomLength: true,
                    height: 10,
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
              ),
              SizedBox(height: 12),
              SkeletonAvatar(
                style: SkeletonAvatarStyle(
                  width: double.infinity,
                  minHeight: MediaQuery.of(context).size.height / 8,
                  maxHeight: MediaQuery.of(context).size.height / 3,
                ),
              ),
              SizedBox(height: 8),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      SkeletonAvatar(
                        style: SkeletonAvatarStyle(width: 20, height: 20)),
                      SizedBox(width: 8),
                      SkeletonAvatar(
                        style: SkeletonAvatarStyle(width: 20, height: 20)),
                      SizedBox(width: 8),
                      SkeletonAvatar(
                        style: SkeletonAvatarStyle(width: 20, height: 20)),
                    ],
                  ),
                  SkeletonLine(
                    style: SkeletonLineStyle(
                      height: 16,
                      width: 64,
                      borderRadius: BorderRadius.circular(8)),
                  )
                ],
              )
            ],
          )),
    ),
  ),
);

示例

以下是几个示例图像和代码片段:

示例 描述
items 占位符列表
paragraphs 占位符段落
listview_default 默认列表视图
示例 描述
listview_custom 自定义列表视图
listview_cards 列表视图复杂卡片
skeleton_theme 骨架主题
示例 描述
light_dark_modes 光明/黑暗模式
rtl 右到左布局
custom_shimmer 定制闪烁

示例代码

import 'package:example/home.dart';
import 'package:flutter/material.dart';

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Skeletons Package Examples',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

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

1 回复

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


当然,以下是如何在Flutter项目中使用flutter_skeleton_plus插件来实现占位符加载的一个代码示例。这个插件允许你在数据加载时显示一个占位符,从而提升用户体验。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  flutter_skeleton_plus: ^2.0.0  # 请检查最新版本号

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

2. 使用flutter_skeleton_plus

以下是一个简单的示例,展示如何在数据加载期间使用占位符:

import 'package:flutter/material.dart';
import 'package:flutter_skeleton_plus/flutter_skeleton_plus.dart';
import 'dart:async';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Skeleton Plus Example'),
        ),
        body: SkeletonScreenExample(),
      ),
    );
  }
}

class SkeletonScreenExample extends StatefulWidget {
  @override
  _SkeletonScreenExampleState createState() => _SkeletonScreenExampleState();
}

class _SkeletonScreenExampleState extends State<SkeletonScreenExample> {
  bool isLoading = true;
  List<String> data = [];

  @override
  void initState() {
    super.initState();
    Timer(Duration(seconds: 2), () {
      setState(() {
        isLoading = false;
        data = List.generate(20, (index) => "Item $index");
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return isLoading
        ? SkeletonScreen(
            containerColor: Colors.grey[200]!,
            list: [
              SkeletonItem(
                count: 1,
                width: double.infinity,
                height: 50,
                margin: EdgeInsets.only(bottom: 16),
              ),
              SkeletonList(
                count: 10,
                itemBuilder: (context, index) {
                  return SkeletonItem(
                    width: double.infinity,
                    height: 50,
                    margin: EdgeInsets.only(bottom: 8),
                  );
                },
              ),
            ],
          )
        : ListView.builder(
            itemCount: data.length,
            itemBuilder: (context, index) {
              return ListTile(
                title: Text(data[index]),
              );
            },
          );
  }
}

代码解释

  1. 依赖添加:在pubspec.yaml文件中添加flutter_skeleton_plus依赖。
  2. 示例应用:创建一个简单的Flutter应用,其中包含一个SkeletonScreenExample组件。
  3. 状态管理:使用isLoading状态变量来跟踪数据加载状态。
  4. 模拟数据加载:在initState方法中使用Timer模拟一个2秒的数据加载过程。
  5. UI渲染
    • isLoadingtrue时,显示SkeletonScreen,其中包含一个标题占位符和一个列表占位符。
    • isLoadingfalse时,显示实际数据列表。

SkeletonScreen 和 SkeletonItem

  • SkeletonScreen:用于显示一个包含多个SkeletonItem的屏幕占位符。
  • SkeletonItem:用于显示单个占位符,可以自定义宽度、高度和边距。
  • SkeletonList:用于显示一个占位符列表,通过itemBuilder自定义每个占位符。

这样,你就可以在Flutter应用中使用flutter_skeleton_plus插件来显示加载占位符,从而提升用户体验。

回到顶部