1
1
import 'dart:async' ;
2
2
3
3
import 'package:flutter/material.dart' ;
4
+ import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart' ;
5
+ import 'package:flutter_disposebag/flutter_disposebag.dart' ;
4
6
import 'package:listenable_stream/listenable_stream.dart' ;
5
- import 'package:rxdart/rxdart .dart' ;
7
+ import 'package:rxdart_ext/state_stream .dart' ;
6
8
7
9
void main () => runApp (MyApp ());
8
10
@@ -47,27 +49,28 @@ class MainPage extends StatefulWidget {
47
49
_MainPageState createState () => _MainPageState ();
48
50
}
49
51
50
- class _MainPageState extends State <MainPage > {
52
+ class _MainPageState extends State <MainPage > with DisposeBagMixin {
51
53
final controller = TextEditingController ();
52
- late final StreamSubscription <String > subscription ;
54
+ late final StateStream <String > stateStream ;
53
55
54
56
@override
55
57
void initState () {
56
58
super .initState ();
57
- subscription = controller
59
+
60
+ stateStream = controller
58
61
.toValueStream (replayValue: true )
59
62
.map ((event) => event.text)
60
63
.debounceTime (const Duration (milliseconds: 500 ))
61
64
.where ((s) => s.isNotEmpty)
62
65
.distinct ()
63
66
.switchMap ((value) => Stream .periodic (
64
67
const Duration (milliseconds: 500 ), (i) => '$value ..$i ' ))
65
- .listen (print);
68
+ .publishState ('initial' )
69
+ ..connect ().disposedBy (bag);
66
70
}
67
71
68
72
@override
69
73
void dispose () {
70
- subscription.cancel ();
71
74
controller.dispose ();
72
75
super .dispose ();
73
76
}
@@ -87,6 +90,19 @@ class _MainPageState extends State<MainPage> {
87
90
controller: controller,
88
91
decoration: const InputDecoration (filled: true ),
89
92
),
93
+ Expanded (
94
+ child: RxStreamBuilder <String >(
95
+ stream: stateStream,
96
+ builder: (context, state) {
97
+ return Center (
98
+ child: Text (
99
+ state,
100
+ style: Theme .of (context).textTheme.titleMedium,
101
+ ),
102
+ );
103
+ },
104
+ ),
105
+ ),
90
106
],
91
107
),
92
108
),
0 commit comments