floading.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/cupertino.dart';
  4. /// 便捷的创建 loading,支持设置全局样式,也支持设置单次样式。能够自动消失。
  5. ///
  6. /// It is convenient to create loading, support setting global style, and also support setting single style. Can disappear automatically.
  7. class FLoading {
  8. static bool _isShow = false;
  9. static BuildContext _cacheContext;
  10. static Widget _loading;
  11. static Color _backgroundColor;
  12. static Timer _timer;
  13. /// 是否正在显示 loading
  14. ///
  15. /// Whether loading is being displayed
  16. static bool get isShow {
  17. return _isShow;
  18. }
  19. /// 初始化默认的 loading 视图
  20. ///
  21. /// Initialize the default loading view
  22. static init(Widget widget, {Color backgroundColor}) {
  23. _loading = widget;
  24. if (backgroundColor != null) {
  25. _backgroundColor = backgroundColor;
  26. }
  27. }
  28. /// 显示 Loading
  29. /// [loading] - 自定义的 Loading 视图
  30. /// [duration] - 指定毫秒后,自动隐藏。如果为 null,则不自动隐藏
  31. /// [color] - loading 时的背景颜色,默认为 [Colors.black54]
  32. /// [closable] - 是否可以通过返回按钮关闭 loading
  33. ///
  34. /// Display Loading
  35. /// [loading]-custom loading view
  36. /// [duration]-automatically hide after specified milliseconds. If null, do not hide automatically
  37. /// [color]-background color when loading, default is [Colors.black54]
  38. /// [closable]-Is it possible to close loading via the back button
  39. static show(BuildContext context,
  40. {Widget loading, int duration, Color color, bool closable = false}) {
  41. if (!_isShow) {
  42. _isShow = true;
  43. showGeneralDialog(
  44. context: context,
  45. pageBuilder: (BuildContext buildContext, Animation<double> animation,
  46. Animation<double> secondaryAnimation) {
  47. final Widget pageChild = Builder(builder: (context) {
  48. _cacheContext = context;
  49. Widget widget = loading ?? _loading ?? CupertinoActivityIndicator();
  50. return WillPopScope(
  51. onWillPop: () async {
  52. if (closable) {
  53. hide();
  54. }
  55. return Future.value(false);
  56. },
  57. child: Column(
  58. mainAxisAlignment: MainAxisAlignment.center,
  59. children: [widget],
  60. ),
  61. );
  62. });
  63. return pageChild;
  64. },
  65. barrierDismissible: false,
  66. barrierLabel:
  67. MaterialLocalizations.of(context).modalBarrierDismissLabel,
  68. barrierColor: color ?? _backgroundColor,
  69. transitionDuration: const Duration(milliseconds: 150),
  70. transitionBuilder: (BuildContext context, Animation<double> animation,
  71. Animation<double> secondaryAnimation, Widget child) {
  72. return FadeTransition(
  73. opacity: CurvedAnimation(
  74. parent: animation,
  75. curve: Curves.easeOut,
  76. ),
  77. child: child,
  78. );
  79. },
  80. useRootNavigator: true,
  81. routeSettings: null,
  82. );
  83. if (duration != null) {
  84. _timer = Timer(Duration(milliseconds: duration), () {
  85. hide();
  86. });
  87. }
  88. }
  89. }
  90. /// 隐藏 loading。
  91. /// [context] 有时,开发者可能需要自行传入当前 [context]。
  92. ///
  93. /// Hide loading
  94. /// [context] Sometimes, developers may need to pass in the current [context] by themselves.
  95. static hide({BuildContext context}) {
  96. _timer?.cancel();
  97. _timer = null;
  98. if(context != null){
  99. _cacheContext = context;
  100. }
  101. if (_isShow && _cacheContext != null) {
  102. _isShow = false;
  103. Navigator.pop(_cacheContext);
  104. _cacheContext = null;
  105. }
  106. }
  107. /// 出现任何异常时可调用尝试修复
  108. ///
  109. /// When any exception occurs, you can call to try to repair
  110. static fix() {
  111. hide();
  112. _cacheContext = null;
  113. _isShow = false;
  114. }
  115. }