-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathdebouncing-streams-in-flutter.dart
146 lines (129 loc) Β· 3.42 KB
/
debouncing-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
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
// π¦ 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 'dart:async';
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(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
class DebounceTransformer<T> extends StreamTransformerBase<T, T> {
final Duration duration;
final _controller = StreamController<T>.broadcast();
DebounceTransformer(this.duration);
@override
Stream<T> bind(Stream<T> stream) {
Timer? timer;
final sub = stream.listen(
(value) {
timer?.cancel();
timer = Timer(duration, () {
_controller.sink.add(value);
timer = null;
});
},
);
_controller.onCancel = () {
sub.cancel();
};
return _controller.stream;
}
}
extension Debounce<T> on Stream<T> {
Stream<T> debounce(
Duration duration,
) =>
transform(DebounceTransformer<T>(duration));
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class DateTimeWidget extends StatelessWidget {
final Stream<DateTime> stream;
const DateTimeWidget({super.key, required this.stream});
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: stream.debounce(const Duration(seconds: 1)),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(
snapshot.data.toString(),
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
);
} else {
return const SizedBox();
}
},
);
}
}
class _HomePageState extends State<HomePage> {
late StreamController<DateTime> tapController;
@override
void initState() {
super.initState();
tapController = StreamController<DateTime>.broadcast();
}
@override
void dispose() {
tapController.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
DateTimeWidget(
stream: tapController.stream,
),
TextButton(
onPressed: () => tapController.sink.add(
DateTime.now(),
),
child: const Text(
'Press me',
),
)
],
),
),
),
);
}
}