From 1107bac6693ac3062e1820e0ff249da8523cad88 Mon Sep 17 00:00:00 2001 From: huangjianke Date: Fri, 12 Mar 2021 11:31:51 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20It's=20support=20Null=20Safety?= =?UTF-8?q?=20now?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 + README-zh_CN.md | 2 +- README.md | 2 +- .../contents.xcworkspacedata | 2 +- example/lib/custom_animation.dart | 3 +- example/lib/main.dart | 34 ++- example/lib/test.dart | 11 +- example/pubspec.lock | 115 +++------ example/pubspec.yaml | 6 +- lib/src/animations/offset_animation.dart | 3 +- lib/src/animations/opacity_animation.dart | 3 +- lib/src/animations/scale_animation.dart | 3 +- lib/src/easy_loading.dart | 220 ++++++++---------- lib/src/theme.dart | 32 +-- lib/src/widgets/container.dart | 101 ++++---- lib/src/widgets/indicator.dart | 6 +- lib/src/widgets/loading.dart | 24 +- lib/src/widgets/overlay_entry.dart | 6 +- lib/src/widgets/progress.dart | 16 +- pubspec.lock | 101 +++----- pubspec.yaml | 8 +- 21 files changed, 295 insertions(+), 407 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed0390c..fdedf55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [3.0.0] - 2021.03.12 + +* 🎉 It's support Null Safety now + ## [2.2.2] - 2020.12.28 * fixed [#77](https://github.com/kokohuang/flutter_easyloading/issues/77) diff --git a/README-zh_CN.md b/README-zh_CN.md index 3b0f297..30b9fa4 100644 --- a/README-zh_CN.md +++ b/README-zh_CN.md @@ -16,7 +16,7 @@ ```yaml dependencies: - flutter_easyloading: ^2.2.2 + flutter_easyloading: ^3.0.0 ``` ## 导入 diff --git a/README.md b/README.md index 3a74dc0..baaa8eb 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Add this to your package's `pubspec.yaml` file: ```yaml dependencies: - flutter_easyloading: ^2.2.2 + flutter_easyloading: ^3.0.0 ``` ## Import diff --git a/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 1d526a1..919434a 100644 --- a/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ b/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -2,6 +2,6 @@ + location = "self:"> diff --git a/example/lib/custom_animation.dart b/example/lib/custom_animation.dart index 8c7bfbd..af00820 100644 --- a/example/lib/custom_animation.dart +++ b/example/lib/custom_animation.dart @@ -10,9 +10,8 @@ class CustomAnimation extends EasyLoadingAnimation { AnimationController controller, AlignmentGeometry alignment, ) { - double opacity = controller?.value ?? 0; return Opacity( - opacity: opacity, + opacity: controller.value, child: RotationTransition( turns: controller, child: child, diff --git a/example/lib/main.dart b/example/lib/main.dart index 6da70be..85266e2 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -46,17 +46,17 @@ class MyApp extends StatelessWidget { } class MyHomePage extends StatefulWidget { - MyHomePage({Key key, this.title}) : super(key: key); + MyHomePage({Key? key, this.title}) : super(key: key); - final String title; + final String? title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { - Timer _timer; - double _progress; + Timer? _timer; + late double _progress; @override void initState() { @@ -75,7 +75,7 @@ class _MyHomePageState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text(widget.title), + title: Text(widget.title ?? ''), ), body: Container( width: MediaQuery.of(context).size.width, @@ -89,8 +89,7 @@ class _MyHomePageState extends State { runAlignment: WrapAlignment.center, crossAxisAlignment: WrapCrossAlignment.center, children: [ - FlatButton( - textColor: Colors.blue, + TextButton( child: Text('open test page'), onPressed: () { _timer?.cancel(); @@ -102,8 +101,7 @@ class _MyHomePageState extends State { ); }, ), - FlatButton( - textColor: Colors.blue, + TextButton( child: Text('dismiss'), onPressed: () async { _timer?.cancel(); @@ -111,8 +109,7 @@ class _MyHomePageState extends State { print('EasyLoading dismiss'); }, ), - FlatButton( - textColor: Colors.blue, + TextButton( child: Text('show'), onPressed: () async { _timer?.cancel(); @@ -123,8 +120,7 @@ class _MyHomePageState extends State { print('EasyLoading show'); }, ), - FlatButton( - textColor: Colors.blue, + TextButton( child: Text('showToast'), onPressed: () { _timer?.cancel(); @@ -133,8 +129,7 @@ class _MyHomePageState extends State { ); }, ), - FlatButton( - textColor: Colors.blue, + TextButton( child: Text('showSuccess'), onPressed: () async { _timer?.cancel(); @@ -142,24 +137,21 @@ class _MyHomePageState extends State { print('EasyLoading showSuccess'); }, ), - FlatButton( - textColor: Colors.blue, + TextButton( child: Text('showError'), onPressed: () { _timer?.cancel(); EasyLoading.showError('Failed with Error'); }, ), - FlatButton( - textColor: Colors.blue, + TextButton( child: Text('showInfo'), onPressed: () { _timer?.cancel(); EasyLoading.showInfo('Useful Information.'); }, ), - FlatButton( - textColor: Colors.blue, + TextButton( child: Text('showProgress'), onPressed: () { _progress = 0; diff --git a/example/lib/test.dart b/example/lib/test.dart index d551788..02878a0 100644 --- a/example/lib/test.dart +++ b/example/lib/test.dart @@ -1,6 +1,7 @@ +import 'dart:io'; + import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; -import 'package:dio/dio.dart'; class TestPage extends StatefulWidget { @override @@ -30,7 +31,10 @@ class _TestPageState extends State { void loadData() async { try { await EasyLoading.show(); - Response response = await Dio().get('https://github.com'); + HttpClient client = HttpClient(); + HttpClientRequest request = + await client.getUrl(Uri.parse('https://github.com')); + HttpClientResponse response = await request.close(); print(response); await EasyLoading.dismiss(); } catch (e) { @@ -46,8 +50,7 @@ class _TestPageState extends State { title: Text('Test Page'), ), body: Center( - child: FlatButton( - textColor: Colors.blue, + child: TextButton( child: Text('loadData'), onPressed: () { loadData(); diff --git a/example/pubspec.lock b/example/pubspec.lock index a9871d9..d0fabf1 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -1,62 +1,48 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.6.0" async: dependency: transitive description: name: async url: "https://pub.flutter-io.cn" source: hosted - version: "2.4.1" + version: "2.5.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.0" - charcode: + version: "2.1.0" + characters: dependency: transitive description: - name: charcode + name: characters url: "https://pub.flutter-io.cn" source: hosted - version: "1.1.3" - collection: + version: "1.1.0" + charcode: dependency: transitive description: - name: collection + name: charcode url: "https://pub.flutter-io.cn" source: hosted - version: "1.14.12" - convert: + version: "1.2.0" + clock: dependency: transitive description: - name: convert + name: clock url: "https://pub.flutter-io.cn" source: hosted - version: "2.1.1" - crypto: + version: "1.1.0" + collection: dependency: transitive description: - name: crypto + name: collection url: "https://pub.flutter-io.cn" source: hosted - version: "2.1.4" + version: "1.15.0" cupertino_icons: dependency: "direct main" description: @@ -64,13 +50,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "0.1.3" - dio: - dependency: "direct main" + fake_async: + dependency: transitive description: - name: dio + name: fake_async url: "https://pub.flutter-io.cn" source: hosted - version: "3.0.10" + version: "1.2.0" flutter: dependency: "direct main" description: flutter @@ -82,68 +68,40 @@ packages: path: ".." relative: true source: path - version: "2.2.1" + version: "3.0.0" flutter_spinkit: dependency: transitive description: name: flutter_spinkit url: "https://pub.flutter-io.cn" source: hosted - version: "4.1.2+1" + version: "5.0.0" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" - http_parser: - dependency: transitive - description: - name: http_parser - url: "https://pub.flutter-io.cn" - source: hosted - version: "3.1.4" - image: - dependency: transitive - description: - name: image - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.1.12" matcher: dependency: transitive description: name: matcher url: "https://pub.flutter-io.cn" source: hosted - version: "0.12.6" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.flutter-io.cn" source: hosted - version: "1.1.8" + version: "1.3.0" path: dependency: transitive description: name: path url: "https://pub.flutter-io.cn" source: hosted - version: "1.6.4" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.1.3" + version: "1.8.0" sky_engine: dependency: transitive description: flutter @@ -155,63 +113,56 @@ packages: name: source_span url: "https://pub.flutter-io.cn" source: hosted - version: "1.7.0" + version: "1.8.0" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.flutter-io.cn" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.0" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.flutter-io.cn" source: hosted - version: "1.0.5" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.flutter-io.cn" source: hosted - version: "1.1.0" + version: "1.2.0" test_api: dependency: transitive description: name: test_api url: "https://pub.flutter-io.cn" source: hosted - version: "0.2.15" + version: "0.2.19" typed_data: dependency: transitive description: name: typed_data url: "https://pub.flutter-io.cn" source: hosted - version: "1.1.6" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.flutter-io.cn" - source: hosted - version: "3.6.1" + version: "2.1.0" sdks: - dart: ">=2.6.0 <3.0.0" - flutter: ">=0.1.4 <2.0.0" + dart: ">=2.12.0 <3.0.0" + flutter: ">=2.0.0" diff --git a/example/pubspec.yaml b/example/pubspec.yaml index dce7ba8..bb2c7c5 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -14,15 +14,13 @@ description: A new Flutter project. version: 1.0.0+1 environment: - sdk: ">=2.1.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: sdk: flutter - dio: ^3.0.10 - flutter_easyloading: - path: ../ + flutter_easyloading: ^5.0.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. diff --git a/lib/src/animations/offset_animation.dart b/lib/src/animations/offset_animation.dart index f4d0924..51c51fc 100644 --- a/lib/src/animations/offset_animation.dart +++ b/lib/src/animations/offset_animation.dart @@ -42,9 +42,8 @@ class OffsetAnimation extends EasyLoadingAnimation { begin: _begin, end: Offset(0, 0), ).animate(controller); - double value = controller?.value ?? 0; return Opacity( - opacity: value, + opacity: controller.value, child: SlideTransition( position: _animation, child: child, diff --git a/lib/src/animations/opacity_animation.dart b/lib/src/animations/opacity_animation.dart index f0457d5..b725525 100644 --- a/lib/src/animations/opacity_animation.dart +++ b/lib/src/animations/opacity_animation.dart @@ -33,9 +33,8 @@ class OpacityAnimation extends EasyLoadingAnimation { AnimationController controller, AlignmentGeometry alignment, ) { - double opacity = controller?.value ?? 0; return Opacity( - opacity: opacity, + opacity: controller.value, child: child, ); } diff --git a/lib/src/animations/scale_animation.dart b/lib/src/animations/scale_animation.dart index 52ad0f0..9944c99 100644 --- a/lib/src/animations/scale_animation.dart +++ b/lib/src/animations/scale_animation.dart @@ -33,9 +33,8 @@ class ScaleAnimation extends EasyLoadingAnimation { AnimationController controller, AlignmentGeometry alignment, ) { - double opacity = controller?.value ?? 0; return Opacity( - opacity: opacity, + opacity: controller.value, child: ScaleTransition( scale: controller, child: child, diff --git a/lib/src/easy_loading.dart b/lib/src/easy_loading.dart index 5673856..4ff4c47 100644 --- a/lib/src/easy_loading.dart +++ b/lib/src/easy_loading.dart @@ -104,106 +104,105 @@ typedef EasyLoadingStatusCallback = void Function(EasyLoadingStatus status); class EasyLoading { /// loading style, default [EasyLoadingStyle.dark]. - EasyLoadingStyle loadingStyle; + late EasyLoadingStyle loadingStyle; /// loading indicator type, default [EasyLoadingIndicatorType.fadingCircle]. - EasyLoadingIndicatorType indicatorType; + late EasyLoadingIndicatorType indicatorType; /// loading mask type, default [EasyLoadingMaskType.none]. - EasyLoadingMaskType maskType; + late EasyLoadingMaskType maskType; /// toast position, default [EasyLoadingToastPosition.center]. - EasyLoadingToastPosition toastPosition; + late EasyLoadingToastPosition toastPosition; /// loading animationStyle, default [EasyLoadingAnimationStyle.opacity]. - EasyLoadingAnimationStyle animationStyle; - - /// loading custom animation, default null. - EasyLoadingAnimation customAnimation; + late EasyLoadingAnimationStyle animationStyle; /// textAlign of status, default [TextAlign.center]. - TextAlign textAlign; - - /// textStyle of status, default null. - TextStyle textStyle; + late TextAlign textAlign; /// content padding of loading. - EdgeInsets contentPadding; + late EdgeInsets contentPadding; /// padding of [status]. - EdgeInsets textPadding; + late EdgeInsets textPadding; /// size of indicator, default 40.0. - double indicatorSize; + late double indicatorSize; /// radius of loading, default 5.0. - double radius; + late double radius; /// fontSize of loading, default 15.0. - double fontSize; + late double fontSize; /// width of progress indicator, default 2.0. - double progressWidth; + late double progressWidth; /// width of indicator, default 4.0, only used for [EasyLoadingIndicatorType.ring, EasyLoadingIndicatorType.dualRing]. - double lineWidth; + late double lineWidth; /// display duration of [showSuccess] [showError] [showInfo] [showToast], default 2000ms. - Duration displayDuration; + late Duration displayDuration; /// animation duration of indicator, default 200ms. - Duration animationDuration; + late Duration animationDuration; + + /// loading custom animation, default null. + EasyLoadingAnimation? customAnimation; + + /// textStyle of status, default null. + TextStyle? textStyle; /// color of loading status, only used for [EasyLoadingStyle.custom]. - Color textColor; + Color? textColor; /// color of loading indicator, only used for [EasyLoadingStyle.custom]. - Color indicatorColor; + Color? indicatorColor; /// progress color of loading, only used for [EasyLoadingStyle.custom]. - Color progressColor; + Color? progressColor; /// background color of loading, only used for [EasyLoadingStyle.custom]. - Color backgroundColor; + Color? backgroundColor; /// mask color of loading, only used for [EasyLoadingMaskType.custom]. - Color maskColor; + Color? maskColor; /// should allow user interactions while loading is displayed. - bool userInteractions; + bool? userInteractions; /// should dismiss on user tap. - bool dismissOnTap; + bool? dismissOnTap; /// indicator widget of loading - Widget indicatorWidget; + Widget? indicatorWidget; /// success widget of loading - Widget successWidget; + Widget? successWidget; /// error widget of loading - Widget errorWidget; + Widget? errorWidget; /// info widget of loading - Widget infoWidget; + Widget? infoWidget; - EasyLoadingOverlayEntry overlayEntry; - Widget _w; + Widget? _w; - GlobalKey _key; - GlobalKey _progressKey; - Timer _timer; + EasyLoadingOverlayEntry? overlayEntry; + GlobalKey? _key; + GlobalKey? _progressKey; + Timer? _timer; - Widget get w => _w; - GlobalKey get key => _key; - GlobalKey get progressKey => _progressKey; + Widget? get w => _w; + GlobalKey? get key => _key; + GlobalKey? get progressKey => _progressKey; final List _statusCallbacks = []; - factory EasyLoading() => _getInstance(); - static EasyLoading _instance; - static EasyLoading get instance => _getInstance(); + factory EasyLoading() => _instance; + static final EasyLoading _instance = EasyLoading._internal(); EasyLoading._internal() { /// set deafult value @@ -227,18 +226,14 @@ class EasyLoading { ); } - static EasyLoading _getInstance() { - if (_instance == null) _instance = EasyLoading._internal(); - return _instance; - } - - static bool get isShow => _getInstance().w != null; + static EasyLoading get instance => _instance; + static bool get isShow => _instance.w != null; /// init EasyLoading static TransitionBuilder init({ - TransitionBuilder builder, + TransitionBuilder? builder, }) { - return (BuildContext context, Widget child) { + return (BuildContext context, Widget? child) { if (builder != null) { return builder(context, FlutterEasyLoading(child: child)); } else { @@ -249,14 +244,13 @@ class EasyLoading { /// show loading with [status] [indicator] [maskType] static Future show({ - String status, - Widget indicator, - EasyLoadingMaskType maskType, - bool dismissOnTap, + String? status, + Widget? indicator, + EasyLoadingMaskType? maskType, + bool? dismissOnTap, }) { - Widget w = - indicator ?? (_getInstance().indicatorWidget ?? LoadingIndicator()); - return _getInstance()._show( + Widget w = indicator ?? (_instance.indicatorWidget ?? LoadingIndicator()); + return _instance._show( status: status, maskType: maskType, dismissOnTap: dismissOnTap, @@ -267,57 +261,57 @@ class EasyLoading { /// show progress with [value] [status] [maskType], value should be 0.0 ~ 1.0. static Future showProgress( double value, { - String status, - EasyLoadingMaskType maskType, + String? status, + EasyLoadingMaskType? maskType, }) async { assert( value >= 0.0 && value <= 1.0, 'progress value should be 0.0 ~ 1.0', ); - if (_getInstance().loadingStyle == EasyLoadingStyle.custom) { + if (_instance.loadingStyle == EasyLoadingStyle.custom) { assert( - _getInstance().progressColor != null, + _instance.progressColor != null, 'while loading style is custom, progressColor should not be null', ); } - if (_getInstance().w == null || _getInstance().progressKey == null) { - if (_getInstance().key != null) await dismiss(animation: false); + if (_instance.w == null || _instance.progressKey == null) { + if (_instance.key != null) await dismiss(animation: false); GlobalKey _progressKey = GlobalKey(); Widget w = EasyLoadingProgress( key: _progressKey, value: value, ); - _getInstance()._show( + _instance._show( status: status, maskType: maskType, dismissOnTap: false, w: w, ); - _getInstance()._progressKey = _progressKey; + _instance._progressKey = _progressKey; } // update progress - _getInstance().progressKey?.currentState?.updateProgress(min(1.0, value)); + _instance.progressKey?.currentState?.updateProgress(min(1.0, value)); // update status - if (status != null) _getInstance().key?.currentState?.updateStatus(status); + if (status != null) _instance.key?.currentState?.updateStatus(status); } /// showSuccess [status] [duration] [maskType] static Future showSuccess( String status, { - Duration duration, - EasyLoadingMaskType maskType, - bool dismissOnTap, + Duration? duration, + EasyLoadingMaskType? maskType, + bool? dismissOnTap, }) { - Widget w = _getInstance().successWidget ?? + Widget w = _instance.successWidget ?? Icon( Icons.done, color: EasyLoadingTheme.indicatorColor, size: EasyLoadingTheme.indicatorSize, ); - return _getInstance()._show( + return _instance._show( status: status, duration: duration ?? EasyLoadingTheme.displayDuration, maskType: maskType, @@ -329,17 +323,17 @@ class EasyLoading { /// showError [status] [duration] [maskType] static Future showError( String status, { - Duration duration, - EasyLoadingMaskType maskType, - bool dismissOnTap, + Duration? duration, + EasyLoadingMaskType? maskType, + bool? dismissOnTap, }) { - Widget w = _getInstance().errorWidget ?? + Widget w = _instance.errorWidget ?? Icon( Icons.clear, color: EasyLoadingTheme.indicatorColor, size: EasyLoadingTheme.indicatorSize, ); - return _getInstance()._show( + return _instance._show( status: status, duration: duration ?? EasyLoadingTheme.displayDuration, maskType: maskType, @@ -351,17 +345,17 @@ class EasyLoading { /// showInfo [status] [duration] [maskType] static Future showInfo( String status, { - Duration duration, - EasyLoadingMaskType maskType, - bool dismissOnTap, + Duration? duration, + EasyLoadingMaskType? maskType, + bool? dismissOnTap, }) { - Widget w = _getInstance().infoWidget ?? + Widget w = _instance.infoWidget ?? Icon( Icons.info_outline, color: EasyLoadingTheme.indicatorColor, size: EasyLoadingTheme.indicatorSize, ); - return _getInstance()._show( + return _instance._show( status: status, duration: duration ?? EasyLoadingTheme.displayDuration, maskType: maskType, @@ -373,12 +367,12 @@ class EasyLoading { /// showToast [status] [duration] [toastPosition] [maskType] static Future showToast( String status, { - Duration duration, - EasyLoadingToastPosition toastPosition, - EasyLoadingMaskType maskType, - bool dismissOnTap, + Duration? duration, + EasyLoadingToastPosition? toastPosition, + EasyLoadingMaskType? maskType, + bool? dismissOnTap, }) { - return _getInstance()._show( + return _instance._show( status: status, duration: duration ?? EasyLoadingTheme.displayDuration, toastPosition: toastPosition ?? EasyLoadingTheme.toastPosition, @@ -392,46 +386,41 @@ class EasyLoading { bool animation = true, }) { // cancel timer - _getInstance()._cancelTimer(); - return _getInstance()._dismiss(animation); + _instance._cancelTimer(); + return _instance._dismiss(animation); } /// add loading status callback static void addStatusCallback(EasyLoadingStatusCallback callback) { - if (!_getInstance()._statusCallbacks.contains(callback)) { - _getInstance()._statusCallbacks.add(callback); + if (!_instance._statusCallbacks.contains(callback)) { + _instance._statusCallbacks.add(callback); } } /// remove single loading status callback static void removeCallback(EasyLoadingStatusCallback callback) { - if (_getInstance()._statusCallbacks.contains(callback)) { - _getInstance()._statusCallbacks.remove(callback); + if (_instance._statusCallbacks.contains(callback)) { + _instance._statusCallbacks.remove(callback); } } /// remove all loading status callback static void removeAllCallbacks() { - _getInstance()._statusCallbacks.clear(); + _instance._statusCallbacks.clear(); } /// show [status] [duration] [toastPosition] [maskType] Future _show({ - Widget w, - String status, - Duration duration, - EasyLoadingToastPosition toastPosition = EasyLoadingToastPosition.center, - EasyLoadingMaskType maskType, - bool dismissOnTap, + Widget? w, + String? status, + Duration? duration, + EasyLoadingMaskType? maskType, + bool? dismissOnTap, + EasyLoadingToastPosition? toastPosition, }) async { assert( overlayEntry != null, - 'overlayEntry should not be null', - ); - - assert( - toastPosition != null, - 'toastPosition should not be null', + 'You should call EasyLoading.init() in your MaterialApp', ); if (loadingStyle == EasyLoadingStyle.custom) { @@ -449,12 +438,7 @@ class EasyLoading { ); } - if (maskType == null) maskType = _getInstance().maskType; - assert( - maskType != null, - 'maskType should not be null', - ); - + maskType ??= _instance.maskType; if (maskType == EasyLoadingMaskType.custom) { assert( maskColor != null, @@ -469,6 +453,7 @@ class EasyLoading { ); } + toastPosition ??= EasyLoadingToastPosition.center; bool animation = _w == null; _progressKey = null; if (_key != null) await dismiss(animation: false); @@ -489,9 +474,8 @@ class EasyLoading { _callback(EasyLoadingStatus.show); if (duration != null) { _cancelTimer(); - _timer = Timer.periodic(duration, (timer) async { + _timer = Timer(duration, () async { await dismiss(); - _cancelTimer(); }); } }); @@ -500,12 +484,12 @@ class EasyLoading { } Future _dismiss(bool animation) async { - if (key != null && key.currentState == null) { + if (key != null && key?.currentState == null) { _reset(); return; } - return key?.currentState?.dismiss(animation)?.whenComplete(() { + return key?.currentState?.dismiss(animation).whenComplete(() { _reset(); }); } @@ -521,7 +505,7 @@ class EasyLoading { void _callback(EasyLoadingStatus status) { for (final EasyLoadingStatusCallback callback in _statusCallbacks) { - if (callback != null) callback(status); + callback(status); } } diff --git a/lib/src/theme.dart b/lib/src/theme.dart index 9b5ff6c..316fa03 100644 --- a/lib/src/theme.dart +++ b/lib/src/theme.dart @@ -32,7 +32,7 @@ class EasyLoadingTheme { /// color of indicator static Color get indicatorColor => EasyLoading.instance.loadingStyle == EasyLoadingStyle.custom - ? EasyLoading.instance.indicatorColor + ? EasyLoading.instance.indicatorColor! : EasyLoading.instance.loadingStyle == EasyLoadingStyle.dark ? Colors.white : Colors.black; @@ -40,7 +40,7 @@ class EasyLoadingTheme { /// progress color of loading static Color get progressColor => EasyLoading.instance.loadingStyle == EasyLoadingStyle.custom - ? EasyLoading.instance.progressColor + ? EasyLoading.instance.progressColor! : EasyLoading.instance.loadingStyle == EasyLoadingStyle.dark ? Colors.white : Colors.black; @@ -48,7 +48,7 @@ class EasyLoadingTheme { /// background color of loading static Color get backgroundColor => EasyLoading.instance.loadingStyle == EasyLoadingStyle.custom - ? EasyLoading.instance.backgroundColor + ? EasyLoading.instance.backgroundColor! : EasyLoading.instance.loadingStyle == EasyLoadingStyle.dark ? Colors.black.withOpacity(0.9) : Colors.white; @@ -56,17 +56,17 @@ class EasyLoadingTheme { /// font color of status static Color get textColor => EasyLoading.instance.loadingStyle == EasyLoadingStyle.custom - ? EasyLoading.instance.textColor + ? EasyLoading.instance.textColor! : EasyLoading.instance.loadingStyle == EasyLoadingStyle.dark ? Colors.white : Colors.black; /// mask color of loading - static Color maskColor(EasyLoadingMaskType maskType) { - EasyLoadingMaskType type = maskType ?? EasyLoading.instance.maskType; - return type == EasyLoadingMaskType.custom - ? EasyLoading.instance.maskColor - : type == EasyLoadingMaskType.black + static Color maskColor(EasyLoadingMaskType? maskType) { + maskType ??= EasyLoading.instance.maskType; + return maskType == EasyLoadingMaskType.custom + ? EasyLoading.instance.maskColor! + : maskType == EasyLoadingMaskType.black ? Colors.black.withOpacity(0.5) : Colors.transparent; } @@ -76,7 +76,7 @@ class EasyLoadingTheme { EasyLoadingAnimation _animation; switch (EasyLoading.instance.animationStyle) { case EasyLoadingAnimationStyle.custom: - _animation = EasyLoading.instance.customAnimation; + _animation = EasyLoading.instance.customAnimation!; break; case EasyLoadingAnimationStyle.offset: _animation = OffsetAnimation(); @@ -112,7 +112,7 @@ class EasyLoadingTheme { EasyLoading.instance.toastPosition; /// toast position - static AlignmentGeometry alignment(EasyLoadingToastPosition position) => + static AlignmentGeometry alignment(EasyLoadingToastPosition? position) => position == EasyLoadingToastPosition.bottom ? AlignmentDirectional.bottomCenter : (position == EasyLoadingToastPosition.top @@ -136,17 +136,17 @@ class EasyLoadingTheme { static TextAlign get textAlign => EasyLoading.instance.textAlign; /// textStyle of status - static TextStyle get textStyle => EasyLoading.instance.textStyle; + static TextStyle? get textStyle => EasyLoading.instance.textStyle; /// radius of loading static double get radius => EasyLoading.instance.radius; /// should dismiss on user tap - static bool get dismissOnTap => EasyLoading.instance.dismissOnTap; + static bool? get dismissOnTap => EasyLoading.instance.dismissOnTap; - static bool ignoring(EasyLoadingMaskType maskType) { - EasyLoadingMaskType type = maskType ?? EasyLoading.instance.maskType; + static bool ignoring(EasyLoadingMaskType? maskType) { + maskType ??= EasyLoading.instance.maskType; return EasyLoading.instance.userInteractions ?? - (type == EasyLoadingMaskType.none ? true : false); + (maskType == EasyLoadingMaskType.none ? true : false); } } diff --git a/lib/src/widgets/container.dart b/lib/src/widgets/container.dart index 7381746..239c13a 100644 --- a/lib/src/widgets/container.dart +++ b/lib/src/widgets/container.dart @@ -29,23 +29,23 @@ import '../theme.dart'; import '../easy_loading.dart'; class EasyLoadingContainer extends StatefulWidget { - final Widget indicator; - final String status; + final Widget? indicator; + final String? status; + final bool? dismissOnTap; + final EasyLoadingToastPosition? toastPosition; + final EasyLoadingMaskType? maskType; + final Completer? completer; final bool animation; - final bool dismissOnTap; - final EasyLoadingToastPosition toastPosition; - final EasyLoadingMaskType maskType; - final Completer completer; const EasyLoadingContainer({ - Key key, + Key? key, this.indicator, this.status, - this.animation = true, this.dismissOnTap, this.toastPosition, this.maskType, this.completer, + this.animation = true, }) : super(key: key); @override @@ -54,14 +54,14 @@ class EasyLoadingContainer extends StatefulWidget { class EasyLoadingContainerState extends State with SingleTickerProviderStateMixin { - String _status; - AlignmentGeometry _alignment; - bool _dismissOnTap, _ignoring; - Color _maskColor; - AnimationController _animationController; + String? _status; + Color? _maskColor; + late AnimationController _animationController; + late AlignmentGeometry _alignment; + late bool _dismissOnTap, _ignoring; bool get isPersistentCallbacks => - SchedulerBinding.instance.schedulerPhase == + SchedulerBinding.instance?.schedulerPhase == SchedulerPhase.persistentCallbacks; @override @@ -81,8 +81,8 @@ class EasyLoadingContainerState extends State vsync: this, duration: EasyLoadingTheme.animationDuration, )..addStatusListener((status) { - if (status == AnimationStatus.completed && - !widget.completer.isCompleted) { + bool isCompleted = widget.completer?.isCompleted ?? false; + if (status == AnimationStatus.completed && !isCompleted) { widget.completer?.complete(); } }); @@ -91,30 +91,29 @@ class EasyLoadingContainerState extends State @override void dispose() { - _animationController?.dispose(); - _animationController = null; + _animationController.dispose(); super.dispose(); } Future show(bool animation) { if (isPersistentCallbacks) { Completer completer = Completer(); - SchedulerBinding.instance.addPostFrameCallback((_) => completer - .complete(_animationController?.forward(from: animation ? 0 : 1))); + SchedulerBinding.instance?.addPostFrameCallback((_) => completer + .complete(_animationController.forward(from: animation ? 0 : 1))); return completer.future; } else { - return _animationController?.forward(from: animation ? 0 : 1); + return _animationController.forward(from: animation ? 0 : 1); } } Future dismiss(bool animation) { if (isPersistentCallbacks) { Completer completer = Completer(); - SchedulerBinding.instance.addPostFrameCallback((_) => completer - .complete(_animationController?.reverse(from: animation ? 1 : 0))); + SchedulerBinding.instance?.addPostFrameCallback((_) => completer + .complete(_animationController.reverse(from: animation ? 1 : 0))); return completer.future; } else { - return _animationController?.reverse(from: animation ? 1 : 0); + return _animationController.reverse(from: animation ? 1 : 0); } } @@ -136,9 +135,9 @@ class EasyLoadingContainerState extends State children: [ AnimatedBuilder( animation: _animationController, - builder: (BuildContext context, Widget child) { + builder: (BuildContext context, Widget? child) { return Opacity( - opacity: _animationController?.value ?? 0, + opacity: _animationController.value, child: IgnorePointer( ignoring: _ignoring, child: _dismissOnTap @@ -162,8 +161,8 @@ class EasyLoadingContainerState extends State ), AnimatedBuilder( animation: _animationController, - builder: (BuildContext context, Widget child) { - return EasyLoadingTheme.loadingAnimation?.buildWidget( + builder: (BuildContext context, Widget? child) { + return EasyLoadingTheme.loadingAnimation.buildWidget( _Indicator( status: _status, indicator: widget.indicator, @@ -179,12 +178,12 @@ class EasyLoadingContainerState extends State } class _Indicator extends StatelessWidget { - final Widget indicator; - final String status; + final Widget? indicator; + final String? status; const _Indicator({ - @required this.indicator, - @required this.status, + required this.indicator, + required this.status, }); @override @@ -203,26 +202,24 @@ class _Indicator extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.min, children: [ - indicator != null - ? Container( - margin: status?.isNotEmpty == true - ? EasyLoadingTheme.textPadding - : EdgeInsets.zero, - child: indicator, - ) - : null, - status?.isNotEmpty == true - ? Text( - status, - style: EasyLoadingTheme.textStyle ?? - TextStyle( - color: EasyLoadingTheme.textColor, - fontSize: EasyLoadingTheme.fontSize, - ), - textAlign: EasyLoadingTheme.textAlign, - ) - : null, - ].where((w) => w != null).toList(), + if (indicator != null) + Container( + margin: status?.isNotEmpty == true + ? EasyLoadingTheme.textPadding + : EdgeInsets.zero, + child: indicator, + ), + if (status != null) + Text( + status!, + style: EasyLoadingTheme.textStyle ?? + TextStyle( + color: EasyLoadingTheme.textColor, + fontSize: EasyLoadingTheme.fontSize, + ), + textAlign: EasyLoadingTheme.textAlign, + ), + ], ), ); } diff --git a/lib/src/widgets/indicator.dart b/lib/src/widgets/indicator.dart index 6a9ead5..83367fb 100644 --- a/lib/src/widgets/indicator.dart +++ b/lib/src/widgets/indicator.dart @@ -28,7 +28,7 @@ import '../theme.dart'; class LoadingIndicator extends StatefulWidget { const LoadingIndicator({ - Key key, + Key? key, }) : super(key: key); @override @@ -40,8 +40,7 @@ class _LoadingIndicatorState extends State { /// indicator color of loading final Color _indicatorColor = EasyLoadingTheme.indicatorColor; - - Widget _indicator; + late Widget _indicator; @override void initState() { @@ -50,7 +49,6 @@ class _LoadingIndicatorState extends State { @override void dispose() { - _indicator = null; super.dispose(); } diff --git a/lib/src/widgets/loading.dart b/lib/src/widgets/loading.dart index 919a027..d188555 100644 --- a/lib/src/widgets/loading.dart +++ b/lib/src/widgets/loading.dart @@ -26,12 +26,12 @@ import '../easy_loading.dart'; import './overlay_entry.dart'; class FlutterEasyLoading extends StatefulWidget { - final Widget child; + final Widget? child; const FlutterEasyLoading({ - Key key, - @required this.child, - }) : assert(child != null), + Key? key, + required this.child, + }) : assert(child != null), super(key: key); @override @@ -39,7 +39,7 @@ class FlutterEasyLoading extends StatefulWidget { } class _FlutterEasyLoadingState extends State { - EasyLoadingOverlayEntry _overlayEntry; + late EasyLoadingOverlayEntry _overlayEntry; @override void initState() { @@ -50,19 +50,19 @@ class _FlutterEasyLoadingState extends State { EasyLoading.instance.overlayEntry = _overlayEntry; } - @override - void dispose() { - _overlayEntry = null; - super.dispose(); - } - @override Widget build(BuildContext context) { return Material( child: Overlay( initialEntries: [ EasyLoadingOverlayEntry( - builder: (BuildContext context) => widget.child, + builder: (BuildContext context) { + if (widget.child != null) { + return widget.child!; + } else { + return Container(); + } + }, ), _overlayEntry, ], diff --git a/lib/src/widgets/overlay_entry.dart b/lib/src/widgets/overlay_entry.dart index c6cec58..57cb09a 100644 --- a/lib/src/widgets/overlay_entry.dart +++ b/lib/src/widgets/overlay_entry.dart @@ -27,14 +27,14 @@ class EasyLoadingOverlayEntry extends OverlayEntry { final WidgetBuilder builder; EasyLoadingOverlayEntry({ - @required this.builder, + required this.builder, }) : super(builder: builder); @override void markNeedsBuild() { - if (SchedulerBinding.instance.schedulerPhase == + if (SchedulerBinding.instance?.schedulerPhase == SchedulerPhase.persistentCallbacks) { - SchedulerBinding.instance.addPostFrameCallback((_) { + SchedulerBinding.instance?.addPostFrameCallback((_) { super.markNeedsBuild(); }); } else { diff --git a/lib/src/widgets/progress.dart b/lib/src/widgets/progress.dart index 058a2af..b11fdcd 100644 --- a/lib/src/widgets/progress.dart +++ b/lib/src/widgets/progress.dart @@ -30,8 +30,8 @@ class EasyLoadingProgress extends StatefulWidget { final double value; const EasyLoadingProgress({ - Key key, - @required this.value, + Key? key, + required this.value, }) : super(key: key); @override @@ -78,9 +78,9 @@ class _CircleProgress extends ProgressIndicator { final Color color; _CircleProgress({ - @required this.value, - @required this.width, - @required this.color, + required this.value, + required this.width, + required this.color, }); @override @@ -116,9 +116,9 @@ class _CirclePainter extends CustomPainter { final double width; _CirclePainter({ - @required this.color, - @required this.value, - @required this.width, + required this.color, + required this.value, + required this.width, }); @override diff --git a/pubspec.lock b/pubspec.lock index 3fb4fd7..5091533 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,62 +1,55 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.6.0" async: dependency: transitive description: name: async url: "https://pub.flutter-io.cn" source: hosted - version: "2.4.1" + version: "2.5.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.0" + version: "2.1.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.1.0" charcode: dependency: transitive description: name: charcode url: "https://pub.flutter-io.cn" source: hosted - version: "1.1.3" - collection: + version: "1.2.0" + clock: dependency: transitive description: - name: collection + name: clock url: "https://pub.flutter-io.cn" source: hosted - version: "1.14.12" - convert: + version: "1.1.0" + collection: dependency: transitive description: - name: convert + name: collection url: "https://pub.flutter-io.cn" source: hosted - version: "2.1.1" - crypto: + version: "1.15.0" + fake_async: dependency: transitive description: - name: crypto + name: fake_async url: "https://pub.flutter-io.cn" source: hosted - version: "2.1.4" + version: "1.2.0" flutter: dependency: "direct main" description: flutter @@ -68,54 +61,33 @@ packages: name: flutter_spinkit url: "https://pub.flutter-io.cn" source: hosted - version: "4.1.2+1" + version: "5.0.0" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" - image: - dependency: transitive - description: - name: image - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.1.12" matcher: dependency: transitive description: name: matcher url: "https://pub.flutter-io.cn" source: hosted - version: "0.12.6" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.flutter-io.cn" source: hosted - version: "1.1.8" + version: "1.3.0" path: dependency: transitive description: name: path url: "https://pub.flutter-io.cn" source: hosted - version: "1.6.4" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.1.3" + version: "1.8.0" sky_engine: dependency: transitive description: flutter @@ -127,63 +99,56 @@ packages: name: source_span url: "https://pub.flutter-io.cn" source: hosted - version: "1.7.0" + version: "1.8.0" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.flutter-io.cn" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.0" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.flutter-io.cn" source: hosted - version: "1.0.5" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.flutter-io.cn" source: hosted - version: "1.1.0" + version: "1.2.0" test_api: dependency: transitive description: name: test_api url: "https://pub.flutter-io.cn" source: hosted - version: "0.2.15" + version: "0.2.19" typed_data: dependency: transitive description: name: typed_data url: "https://pub.flutter-io.cn" source: hosted - version: "1.1.6" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.flutter-io.cn" - source: hosted - version: "3.6.1" + version: "2.1.0" sdks: - dart: ">=2.6.0 <3.0.0" - flutter: ">=0.1.4 <2.0.0" + dart: ">=2.12.0 <3.0.0" + flutter: ">=2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 3c8b46f..a059278 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,16 +1,16 @@ name: flutter_easyloading -description: ✨A clean and lightweight loading/toast widget for Flutter, easy to use without context, support iOS、Android and Web -version: 2.2.2 +description: ✨A clean and lightweight loading/toast widget for Flutter, Easy to use without context, Support iOS、Android and Web +version: 3.0.0 homepage: https://github.com/kokohuang/flutter_easyloading environment: - sdk: ">=2.2.2 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: sdk: flutter - flutter_spinkit: ^4.1.2+1 + flutter_spinkit: ^5.0.0 dev_dependencies: flutter_test: