6.6 EasyLoading 配置
实现步骤:
第 1 步:下载插件 flutter_easyloading
1
| flutter pub add flutter_easyloading
|
第 2 步:loading 工具类
lib/common/utils/loading.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart';
class Loading { static const int _milliseconds = 500; static const int _dismissMilliseconds = 2000;
Loading() { EasyLoading.instance ..displayDuration = const Duration(milliseconds: _dismissMilliseconds) ..indicatorType = EasyLoadingIndicatorType.ring ..loadingStyle = EasyLoadingStyle.custom ..indicatorSize = 35.0 ..lineWidth = 2 ..radius = 10.0 ..progressColor = Colors.white ..backgroundColor = Colors.black.withOpacity(0.7) ..indicatorColor = Colors.white ..textColor = Colors.white ..maskColor = Colors.black.withOpacity(0.6) ..userInteractions = true ..dismissOnTap = false; }
static void show([String? text]) { EasyLoading.instance.userInteractions = false; EasyLoading.show(status: text ?? 'Loading...'); }
static void error([String? text]) { Future.delayed( const Duration(milliseconds: _milliseconds), () => EasyLoading.showError(text ?? 'Error'), ); }
static void success([String? text]) { Future.delayed( const Duration(milliseconds: _milliseconds), () => EasyLoading.showSuccess(text ?? 'Success'), ); }
static void toast(String text) { EasyLoading.showToast(text); }
static Future<void> dismiss() async { await Future.delayed( const Duration(milliseconds: _dismissMilliseconds), () { EasyLoading.instance.userInteractions = true; EasyLoading.dismiss(); }, ); } }
|
第 3 步:main build
lib/main.dart
1 2 3 4 5 6 7 8 9 10
| builder: (context, widget) { widget = EasyLoading.init()(context, widget);
return MediaQuery( data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), child: widget, ); },
|
这是有多个要初始的情况
最后:global 全局初始
lib/global.dart
1 2 3 4 5 6 7 8
| class Global { static Future<void> init() async { WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized(); FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
... Loading();
|
提交代码到 git