-
Notifications
You must be signed in to change notification settings - Fork 19
/
day098_streams.dart
227 lines (208 loc) · 6.17 KB
/
day098_streams.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
enum Using { Transforming, AsyncAsteriskFunction, StreamController }
class Day98Streams extends StatefulWidget {
Day98Streams({Key key}) : super(key: key);
@override
_Day98StreamsState createState() => _Day98StreamsState();
}
class _Day98StreamsState extends State<Day98Streams> {
Using using;
@override
void initState() {
using = Using.Transforming;
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
StreamBuilder<int>(
stream: [
getStreamByPeriodic(),
getStreamByFuture(),
getStreamByStreamController()
][using.index],
initialData: 4,
builder: (context, snapshot) => AnimatedSwitcher(
duration: const Duration(seconds: 1),
transitionBuilder: (child, animation) => FadeTransition(
opacity: animation,
child: child,
),
child: Container(
key: ValueKey(snapshot.data ?? 4),
width: 250,
height: 250,
color: [
Colors.red,
Colors.green,
Colors.blue,
Colors.orange,
Colors.purple
][snapshot.data],
child: Center(
child: Text(
snapshot.data.toString(),
style: TextStyle(fontSize: 25, color: Colors.white),
),
),
),
),
),
[
Padding(
padding: const EdgeInsets.all(20.0),
child: Text('''
Stream<int> getStreamByPeriodic() =>
Stream<int>.periodic(const Duration(seconds: 2), (count) => count % 5);
'''),
),
Padding(
padding: const EdgeInsets.all(20.0),
child: Text('''
Stream<int> getStreamByFuture() async* {
int count = 0;
while (true) {
await Future.delayed(const Duration(seconds: 2));
yield count++ % 5;
}
}
'''),
),
Padding(
padding: const EdgeInsets.all(20.0),
child: Text('''
Stream<int> getStreamByStreamController() {
StreamController<int> controller;
Timer timer;
int counter = 0;
void tick(_) {
counter++;
if (!controller.isClosed)
controller.add(counter); // Ask stream to send counter values as event.
if (counter == 4) {
timer.cancel();
controller.close(); // Ask stream to shut down and tell listeners.
}
}
void startTimer() {
timer = Timer.periodic(Duration(seconds: 2), tick);
}
void stopTimer() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
controller = StreamController<int>(
onListen: startTimer,
onPause: stopTimer,
onResume: startTimer,
onCancel: stopTimer,
);
return controller.stream;
}
'''),
)
][using.index],
Wrap(
children: [
RaisedButton(
onPressed: () {
setState(() {
using = Using.Transforming;
});
},
child: Text('Stream using Transforming'),
),
RaisedButton(
onPressed: () {
setState(() {
using = Using.AsyncAsteriskFunction;
});
},
child: Text('Stream using async*'),
),
RaisedButton(
onPressed: () {
setState(() {
using = Using.StreamController;
});
},
child: Text('Stream using StreamController'),
)
],
)
],
),
),
),
),
appBar: AppBar(
title: Text('Streams'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.help),
onPressed: () async {
const url =
'https://github.com/sanjaysanju618/100-Days-Of-Flutter-Widgets/' +
'blob/master/hundred_days_of_flutter_widget/' +
'lib/day098_streams.dart';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
},
)
],
),
);
}
}
Stream<int> getStreamByPeriodic() =>
Stream<int>.periodic(const Duration(seconds: 2), (count) => count % 5);
Stream<int> getStreamByFuture() async* {
int count = 0;
while (true) {
await Future.delayed(const Duration(seconds: 2));
yield count++ % 5;
}
}
Stream<int> getStreamByStreamController() {
StreamController<int> controller;
Timer timer;
int counter = 0;
void tick(_) {
counter++;
if (!controller.isClosed)
controller.add(counter); // Ask stream to send counter values as event.
if (counter == 4) {
timer.cancel();
controller.close(); // Ask stream to shut down and tell listeners.
}
}
void startTimer() {
timer = Timer.periodic(Duration(seconds: 2), tick);
}
void stopTimer() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
controller = StreamController<int>(
onListen: startTimer,
onPause: stopTimer,
onResume: startTimer,
onCancel: stopTimer,
);
return controller.stream;
}