11import 'dart:async' ;
22
33import 'package:flutter/material.dart' ;
4+ import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart' ;
5+ import 'package:flutter_disposebag/flutter_disposebag.dart' ;
46import 'package:listenable_stream/listenable_stream.dart' ;
5- import 'package:rxdart/rxdart .dart' ;
7+ import 'package:rxdart_ext/state_stream .dart' ;
68
79void main () => runApp (MyApp ());
810
@@ -47,27 +49,28 @@ class MainPage extends StatefulWidget {
4749 _MainPageState createState () => _MainPageState ();
4850}
4951
50- class _MainPageState extends State <MainPage > {
52+ class _MainPageState extends State <MainPage > with DisposeBagMixin {
5153 final controller = TextEditingController ();
52- late final StreamSubscription <String > subscription ;
54+ late final StateStream <String > stateStream ;
5355
5456 @override
5557 void initState () {
5658 super .initState ();
57- subscription = controller
59+
60+ stateStream = controller
5861 .toValueStream (replayValue: true )
5962 .map ((event) => event.text)
6063 .debounceTime (const Duration (milliseconds: 500 ))
6164 .where ((s) => s.isNotEmpty)
6265 .distinct ()
6366 .switchMap ((value) => Stream .periodic (
6467 const Duration (milliseconds: 500 ), (i) => '$value ..$i ' ))
65- .listen (print);
68+ .publishState ('initial' )
69+ ..connect ().disposedBy (bag);
6670 }
6771
6872 @override
6973 void dispose () {
70- subscription.cancel ();
7174 controller.dispose ();
7275 super .dispose ();
7376 }
@@ -87,6 +90,19 @@ class _MainPageState extends State<MainPage> {
8790 controller: controller,
8891 decoration: const InputDecoration (filled: true ),
8992 ),
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+ ),
90106 ],
91107 ),
92108 ),
0 commit comments