Flutter加载提示插件any_loading的使用

Flutter加载提示插件any_loading的使用

any_loading 是一个用于 Flutter 的加载提示插件,支持 Toast、ModalDialog 和 Loading。它还支持 NetLoading,可以延迟显示加载条几秒钟。

特性

  • 支持 Toast、ModalDialog 和 Loading。
  • 支持 NetLoading,可以阻塞 UI 事件以延迟显示加载条几秒钟。

开始使用

pubspec.yaml 文件中添加依赖:

dependencies:
  any_loading: <latest_version>

使用方法

首先,在你的 MaterialAppCupertinoApp 中初始化 AnyLoading

MaterialApp(
    title: 'Flutter Demo',
    theme: ThemeData(
    primarySwatch: Colors.blue,
    ),
    home: const TestPage(),
    builder: AnyLoading.init(),
)

然后,你可以使用以下方法:

AnyLoading.showToast('消息'); // 显示Toast消息
AnyLoading.showModal(title: '标题', content: '内容', success: (bool isSuccess) => print('是否成功--->$isSuccess')); // 显示模态对话框
AnyLoading.showLoading(title: '加载中', maskType: AnyLoadingMaskType.black, style:AnyLoadingStyle.dark()); // 显示加载条
AnyLoading.showNetLoading(title: '加载中', position: AlignmentDirectional.center, delayShowIndicatorDuration: Duration(seconds: 5)); // 显示网络加载条,延迟5秒显示
AnyLoading.dismiss(); // 关闭所有加载提示
// ...

完整示例代码

以下是完整的示例代码,展示如何在 Flutter 应用中使用 any_loading 插件。

import 'package:any_loading/any_loading.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: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const TestPage(),
      builder: AnyLoading.init(), // 初始化AnyLoading
    );
  }
}

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

1 回复

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


any_loading 是一个用于在 Flutter 应用中显示加载提示的插件。它可以帮助你在应用中轻松地显示加载动画、进度条或自定义的加载提示。下面是如何使用 any_loading 插件的基本步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  any_loading: ^1.0.0  # 请检查最新版本

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

2. 导入包

在你的 Dart 文件中导入 any_loading 包:

import 'package:any_loading/any_loading.dart';

3. 使用 AnyLoading

any_loading 提供了多种方法来显示和隐藏加载提示。以下是基本用法:

显示加载提示

你可以使用 AnyLoading.show() 来显示一个默认的加载提示:

AnyLoading.show(); // 显示默认加载提示

自定义加载提示

你可以通过传递参数来自定义加载提示的样式、文本等:

AnyLoading.show(
  status: '加载中...', // 加载提示文本
  indicator: CircularProgressIndicator(), // 自定义加载指示器
  backgroundColor: Colors.black54, // 背景颜色
  textStyle: TextStyle(color: Colors.white), // 文本样式
);

隐藏加载提示

使用 AnyLoading.dismiss() 来隐藏加载提示:

AnyLoading.dismiss(); // 隐藏加载提示

4. 完整示例

以下是一个完整的示例,展示了如何在按钮点击时显示加载提示,并在 3 秒后隐藏它:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('AnyLoading 示例')),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              AnyLoading.show(status: '加载中...'); // 显示加载提示
              Future.delayed(Duration(seconds: 3), () {
                AnyLoading.dismiss(); // 3秒后隐藏加载提示
              });
            },
            child: Text('开始加载'),
          ),
        ),
      ),
    );
  }
}
回到顶部