-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
presenting-streams-in-flutter.dart
102 lines (93 loc) · 3.09 KB
/
presenting-streams-in-flutter.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
98
99
100
101
102
// 🐦 Twitter https://twitter.com/vandadnp
// 🔵 LinkedIn https://linkedin.com/in/vandadnp
// 🎥 YouTube https://youtube.com/c/vandadnp
// 💙 Free Flutter Course https://linktr.ee/vandadnp
// 📦 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
// 🔶 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI
// 🦄 8+ Hours RxSwift Coursde https://youtu.be/xBFWMYmm9ro
// 🤝 Want to support my work? https://buymeacoffee.com/vandad
import 'package:flutter/material.dart';
void main() {
runApp(
const App(),
);
}
class App extends StatelessWidget {
const App({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
extension PresentAsyncSnapshot<E> on AsyncSnapshot<E> {
Widget present({
required BuildContext context,
Widget Function(BuildContext context)? onNone,
Widget Function(BuildContext context, E data)? onData,
Widget Function(BuildContext, Object error, StackTrace stackTrace)? onError,
Widget Function(BuildContext context)? onDoneWitNeitherDataNorError,
Widget Function(BuildContext context)? onWaiting,
}) {
switch (connectionState) {
case ConnectionState.none:
return onNone?.call(context) ?? const SizedBox.shrink();
case ConnectionState.waiting:
return onWaiting?.call(context) ?? const CircularProgressIndicator();
case ConnectionState.active:
case ConnectionState.done:
if (hasError) {
return onError?.call(context, error!, stackTrace!) ??
const SizedBox.shrink();
} else if (hasData) {
return onData?.call(context, data as E) ?? const SizedBox.shrink();
} else {
return onDoneWitNeitherDataNorError?.call(context) ??
const SizedBox.shrink();
}
}
}
}
Stream<String> getDateTime() => Stream.periodic(
const Duration(seconds: 1),
(_) => DateTime.now().toIso8601String(),
);
extension PresentStream<E> on Stream<E> {
Widget present({
Widget Function(BuildContext context)? onNone,
Widget Function(BuildContext context, E data)? onData,
Widget Function(BuildContext, Object error, StackTrace stackTrace)? onError,
Widget Function(BuildContext context)? onDoneWitNeitherDataNorError,
Widget Function(BuildContext context)? onWaiting,
}) {
return StreamBuilder<E>(
stream: this,
builder: (context, snapshot) => snapshot.present(
context: context,
onNone: onNone,
onData: onData,
onError: onError,
onDoneWitNeitherDataNorError: onDoneWitNeitherDataNorError,
onWaiting: onWaiting,
),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: getDateTime().present(
onData: (_, dateTime) => Text(dateTime),
),
),
);
}
}