forked from vandadnp/flutter-tips-and-tricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncsnapshotbuilder-in-flutter.dart
187 lines (168 loc) Ā· 4.67 KB
/
asyncsnapshotbuilder-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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// š¦ 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
// š¤ Want to support my work? https://buymeacoffee.com/vandad
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.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(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
extension Unwrap<T> on Subject<T?> {
Stream<T> unwrap() => switchMap(
(value) {
if (value == null) {
return Rx.never();
} else {
return Stream<T>.value(value);
}
},
);
}
@immutable
class Bloc {
final Sink<String?> setFirstName;
final Sink<String?> setLastName;
final Stream<String> fullName;
const Bloc._({
required this.setFirstName,
required this.setLastName,
required this.fullName,
});
factory Bloc() {
final firstNameSubject = BehaviorSubject<String?>();
final lastNameSubject = BehaviorSubject<String?>();
final fullName = Rx.combineLatest2(
firstNameSubject.startWith(null), lastNameSubject.startWith(null),
(String? firstName, String? lastName) {
if (firstName != null &&
firstName.isNotEmpty &&
lastName != null &&
lastName.isNotEmpty) {
return '$firstName $lastName';
} else {
return 'Both first and last name must be provided';
}
});
return Bloc._(
setFirstName: firstNameSubject.sink,
setLastName: lastNameSubject.sink,
fullName: fullName,
);
}
void dispose() {
setFirstName.close();
setLastName.close();
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
typedef AsyncSnapshotCallback<T> = Widget Function(
BuildContext context,
T? value,
);
class AsyncSnapshotBuilder<T> extends StatelessWidget {
final Stream<T> stream;
final AsyncSnapshotCallback<T>? onNone;
final AsyncSnapshotCallback<T>? onWaiting;
final AsyncSnapshotCallback<T>? onActive;
final AsyncSnapshotCallback<T>? onDone;
const AsyncSnapshotBuilder({
Key? key,
required this.stream,
this.onNone,
this.onWaiting,
this.onActive,
this.onDone,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return StreamBuilder<T>(
stream: stream,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
final callback = onNone ?? (_, __) => const SizedBox();
return callback(context, snapshot.data);
case ConnectionState.waiting:
final callback =
onWaiting ?? (_, __) => const CircularProgressIndicator();
return callback(context, snapshot.data);
case ConnectionState.active:
final callback = onActive ?? (_, __) => const SizedBox();
return callback(context, snapshot.data);
case ConnectionState.done:
final callback = onDone ?? (_, __) => const SizedBox();
return callback(context, snapshot.data);
}
},
);
}
}
class _HomePageState extends State<HomePage> {
late final Bloc bloc;
@override
void initState() {
super.initState();
bloc = Bloc();
}
@override
void dispose() {
bloc.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
'CombineLatest with RxDart',
),
),
body: Column(
children: [
TextField(
decoration: const InputDecoration(
hintText: 'Enter first name here...',
),
onChanged: bloc.setFirstName.add,
),
TextField(
decoration: const InputDecoration(
hintText: 'Enter last name here...',
),
onChanged: bloc.setLastName.add,
),
AsyncSnapshotBuilder<String>(
stream: bloc.fullName,
onActive: (context, String? value) {
return Text(value ?? '');
},
),
],
),
);
}
}