-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
95 lines (80 loc) · 2.18 KB
/
main.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
import 'package:flutter/material.dart';
import 'package:async_phase_notifier/async_phase_notifier.dart';
import 'package:grab/grab.dart';
import 'package:async_phase_notifier_example/api.dart';
import 'package:async_phase_notifier_example/theme.dart';
import 'package:async_phase_notifier_example/widgets.dart';
class FactNotifier extends AsyncPhaseNotifier<Fact> {
FactNotifier() {
fetch();
}
final _api = RandomFactApi();
void fetch() {
final enabled = switchNotifier.value;
runAsync((_) => _api.fetch(enabled: enabled));
}
}
final factNotifier = FactNotifier();
final switchNotifier = ValueNotifier(true);
//======================================================================
void main() {
runApp(
const Grab(child: App()),
);
}
class App extends StatefulWidget {
const App();
@override
State<App> createState() => _AppState();
}
class _AppState extends State<App> {
@override
void dispose() {
factNotifier.dispose();
switchNotifier.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AsyncPhaseNotifier Demo',
theme: AppTheme.data,
home: Scaffold(
appBar: AppBar(
title: const Text('Useless Facts'),
centerTitle: true,
),
body: const _Body(),
floatingActionButton: const Fab(),
bottomNavigationBar: const Footer(),
),
);
}
}
class _Body extends StatelessWidget {
const _Body();
@override
Widget build(BuildContext context) {
final factPhase = factNotifier.grab(context);
final enabled = switchNotifier.grab(context);
return FilledList(
children: [
Expanded(
child: Center(
child: factPhase.when(
waiting: (fact) => const CircularProgressIndicator(),
error: (fact, e, s) => ErrorText(message: '$e'),
complete: (fact) => FactView(fact: fact),
),
),
),
const SizedBox(height: 24.0),
Switch(
value: enabled,
onChanged: (v) => switchNotifier.value = v,
),
Text('Web API ${enabled ? 'enabled' : 'disabled'}'),
],
);
}
}