-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhud_cancelable.dart
97 lines (88 loc) · 2.46 KB
/
hud_cancelable.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import 'package:example/prime_number.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hud/flutter_hud.dart';
class HUDWithCancelable extends StatefulWidget {
static const String title = 'HUD with Cancelable';
const HUDWithCancelable({Key? key}) : super(key: key);
@override
State<HUDWithCancelable> createState() => _HUDWithCancelableState();
}
class _HUDWithCancelableState extends State<HUDWithCancelable> {
bool showHUD = true;
bool canceled = false;
String? resultPrimes;
@override
void initState() {
super.initState();
_reload();
}
_reload() async {
if (!showHUD) {
setState(() {
showHUD = true;
canceled = false;
});
}
final number = await getPrimes(delayedSeconds: 5);
if (mounted && !canceled) {
setState(() {
showHUD = false;
resultPrimes = number;
});
}
}
@override
Widget build(BuildContext context) {
return WidgetHUD(
hud: HUD(
label: 'Generating Primes',
detailLabel: 'Tap the modal for canceling',
),
onCancel: () {
setState(() {
canceled = true;
showHUD = false;
resultPrimes = null;
});
},
builder: (context, child) => child!,
showHUD: showHUD && !canceled,
child: Scaffold(
appBar: AppBar(
title: const Text(HUDWithCancelable.title),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
if (!showHUD && !canceled)
Text(
'The first 10 primes :',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.titleLarge,
),
if (resultPrimes != null)
Text(
resultPrimes!,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.titleSmall,
),
if (!showHUD && canceled)
Text(
'Process canceled',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: !showHUD || canceled
? FloatingActionButton(
onPressed: _reload,
child: const Icon(Icons.refresh),
)
: null,
),
);
}
}