-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathshow_loading.dart
More file actions
88 lines (77 loc) · 2.16 KB
/
show_loading.dart
File metadata and controls
88 lines (77 loc) · 2.16 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
import 'dart:async';
import 'package:flutter/material.dart';
import '../themes/stack_colors.dart';
import '../widgets/custom_loading_overlay.dart';
import 'logger.dart';
Future<T> minWaitFuture<T>(Future<T> future, {required Duration delay}) async {
final results = await Future.wait([future, Future<dynamic>.delayed(delay)]);
return results.first as T;
}
Future<T?> showLoading<T>({
Future<T>? whileFuture,
Future<T> Function()? whileFutureAlt,
required BuildContext context,
required String message,
String? subMessage,
bool rootNavigator = false,
bool opaqueBG = false,
void Function(Exception)? onException,
Duration? delay,
}) async {
assert(
(whileFuture != null || whileFutureAlt != null) &&
!(whileFuture != null && whileFutureAlt != null) &&
!(whileFuture == null && whileFutureAlt == null),
);
unawaited(
showDialog<void>(
context: context,
useRootNavigator: rootNavigator,
barrierDismissible: false,
builder: (_) => WillPopScope(
onWillPop: () async => false,
child: Container(
color: Theme.of(
context,
).extension<StackColors>()!.overlay.withOpacity(opaqueBG ? 1.0 : 0.6),
child: CustomLoadingOverlay(
message: message,
subMessage: subMessage,
eventBus: null,
),
),
),
),
);
Exception? ex;
T? result;
try {
if (delay != null) {
result = await minWaitFuture(
whileFutureAlt?.call() ?? whileFuture!,
delay: delay,
);
} else {
result = await (whileFutureAlt?.call() ?? whileFuture!);
}
} catch (e, s) {
Logging.instance.w("showLoading caught: ", error: e, stackTrace: s);
ex = e is Exception ? e : Exception(e.toString());
}
if (context.mounted) {
Navigator.of(context, rootNavigator: rootNavigator).pop();
if (ex != null) {
onException?.call(ex);
}
}
return result;
}