Flutter:EasyLoading(loading加载、消息提示)

前言

官方虽然提供了内置的加载指示器和提示信息,但是功能比较简陋,这里推荐:flutter_easyloading

CircularProgressIndicator

dart 复制代码
CircularProgressIndicator()


加粗样式

dart 复制代码
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
     // 提示的内容
     content: Text("这是一条提示"),
     //   阴影
     elevation: 8,
     //持续时间,默认4秒
     duration: Duration(seconds: 4),
     //颜色
     backgroundColor: Colors.orange,
     //  形状
     shape: RoundedRectangleBorder(
       borderRadius: BorderRadius.all(Radius.circular(20.0)),
     ),
   ));

基本使用

官方地址
https://pub-web.flutter-io.cn/packages/flutter_easyloading

安装

dart 复制代码
flutter pub add flutter_easyloading

官方在线示例

https://nslogx.github.io/flutter_easyloading/#/

loading

初始化

首先需要在MaterialApp/CupertinoApp中进行初始化

dart 复制代码
MaterialApp(
   //指定显示哪一个页面
   home: const YcHomePage(),
   builder: EasyLoading.init(),
 );

简单使用

dart 复制代码
// 显示指示器
 EasyLoading.show(
   status: '加载中', // 要显示的文字
 );
 //  延时2秒
 Future.delayed(const Duration(seconds: 2), () {
   //  关闭指示器
   EasyLoading.dismiss();
 });


加载样式的设置

加载样式分为全局设置,和非全局设置。下面以加载wave为例

  • 全局设置
dart 复制代码
main() {
  runApp(const MyApp());
  configLoading();
}

// 设置loading,其他配置见官方文档
void configLoading() {
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)  // 加载时间
    ..indicatorType = EasyLoadingIndicatorType.wave  // 加载类型
    ..loadingStyle = EasyLoadingStyle.light  // 加载样式
    ..indicatorSize = 45.0   // 大小
    ..maskType = EasyLoadingMaskType.black // 遮罩
    ..userInteractions = true  // 使用单例模式
    ..dismissOnTap = false;  // 指示器结束的点击时间
}
  • 单独使用
dart 复制代码
onPressed: () {
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 2000)  // 加载时间
      ..indicatorType = EasyLoadingIndicatorType.cubeGrid  // 加载类型
      ..loadingStyle = EasyLoadingStyle.light  // 加载样式
      ..indicatorSize = 45.0   // 大小
      ..maskType = EasyLoadingMaskType.black // 遮罩
      ..userInteractions = true  // 使用单例模式
      ..dismissOnTap = false;  // 指示器结束的点击时间
    // 显示指示器
    EasyLoading.show(
      status: '加载中', // 要显示的文字
    );
    //  延时2秒
    Future.delayed(const Duration(seconds: 2), () {
      //  关闭指示器
      EasyLoading.dismiss();
    });
  },
  child: const Text("加载"))

消息提示

初始化

首先需要在MaterialApp/CupertinoApp中进行初始化

dart 复制代码
MaterialApp(
   //指定显示哪一个页面
   home: const YcHomePage(),
   builder: EasyLoading.init(),
 );

showToast

dart 复制代码
EasyLoading.showToast("这是一个Toast");
//  延时2秒
Future.delayed(const Duration(seconds: 2), () {
  EasyLoading.dismiss();
});


showInfo

dart 复制代码
EasyLoading.showInfo("这是一个Info");


showError

dart 复制代码
 EasyLoading.showError("这是一个Error");

showSuccess

dart 复制代码
 EasyLoading.showSuccess("这是一个success");


showProgress

dart 复制代码
EasyLoading.showProgress(0.2,status: '下载中...');

这个不知道是什么情况,没有显示出圆形进度条。

相关推荐
ujainu小4 分钟前
Flutter动画提效实战:animations 2.1.1 官方包全解析,4种Material动画开箱即用
flutter·animations
巴拉巴拉~~8 分钟前
深入探索Flutter自定义绘制:从零到一实现炫酷仪表盘
flutter·ui
ujainu小12 分钟前
Flutter 结合 path_provider 2.1.5 实现跨平台文件路径管理
flutter·path_provider
ujainu小13 分钟前
Flutter image_picker 1.2.1 插件:图片与视频选择全攻略
flutter
巴拉巴拉~~23 分钟前
Flutter 通用列表项组件 CommonListItemWidget:全场景布局 + 交互增强
flutter·php·交互
kirk_wang13 小时前
Flutter 导航锁踩坑实录:从断言失败到类型转换异常
前端·javascript·flutter
往来凡尘14 小时前
Flutter运行iOS26真机的两个问题
flutter·ios
yfmingo17 小时前
flutter项目大量使用.obs会导致项目性能极度下降吗
flutter
山璞17 小时前
Flutter3.32 中使用 webview4.13 与 vue3 项目的 h5 页面通信,以及如何调试
前端·flutter