-
Notifications
You must be signed in to change notification settings - Fork 4
/
loading_overlay.dart
65 lines (56 loc) · 1.5 KB
/
loading_overlay.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
65
import 'dart:ui';
import 'package:flutter/material.dart';
class LoadingOverlay extends StatefulWidget {
const LoadingOverlay({
Key? key,
required this.child,
this.delay = const Duration(milliseconds: 500),
}) : super(key: key);
final Widget child;
final Duration delay;
static _LoadingOverlayState of(BuildContext context) {
return context.findAncestorStateOfType<_LoadingOverlayState>()!;
}
@override
State<LoadingOverlay> createState() => _LoadingOverlayState();
}
class _LoadingOverlayState extends State<LoadingOverlay> {
bool _isLoading = false;
void show() {
setState(() {
_isLoading = true;
});
}
void hide() {
setState(() {
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
widget.child,
if (_isLoading)
BackdropFilter(
filter: ImageFilter.blur(sigmaX: 4.0, sigmaY: 4.0),
child: const Opacity(
opacity: 0.8,
child: ModalBarrier(dismissible: false, color: Colors.black),
),
),
if (_isLoading)
Center(
child: FutureBuilder(
future: Future.delayed(widget.delay),
builder: (context, snapshot) {
return snapshot.connectionState == ConnectionState.done
? const CircularProgressIndicator()
: const SizedBox();
},
),
),
],
);
}
}