@@ -57,7 +57,7 @@ import 'package:rxdart_ext/rxdart_ext.dart';
5757
5858class CounterBloc extends DisposeCallbackBaseBloc {
5959 /// Inputs
60- final void Function() increment;
60+ final VoidAction increment;
6161
6262 /// Outputs
6363 final DistinctValueStream<int> state;
@@ -73,15 +73,15 @@ class CounterBloc extends DisposeCallbackBaseBloc {
7373 final incrementController = StreamController<void>();
7474
7575 final state = incrementController.stream
76- .scan<int>((acc, _, __) => acc! + 1, 0)
76+ .scan<int>((acc, _, __) => acc + 1, 0)
7777 .publishValueDistinct(0);
7878 final connection = state.connect();
7979
8080 return CounterBloc._(
8181 dispose: () async {
8282 await connection.cancel();
8383 await incrementController.close();
84- print('>>> disposed');
84+ print('CounterBloc:: disposed');
8585 },
8686 increment: () => incrementController.add(null),
8787 state: state,
@@ -97,71 +97,6 @@ import 'package:example/bloc.dart';
9797import 'package:flutter/material.dart';
9898import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart';
9999
100- void main() => runApp(MyApp());
101-
102- class MyApp extends StatelessWidget {
103- @override
104- Widget build(BuildContext context) {
105- return MaterialApp(
106- title: 'Flutter bloc pattern',
107- theme: ThemeData(
108- primarySwatch: Colors.blue,
109- ),
110- home: StartPage(),
111- );
112- }
113- }
114-
115- class StartPage extends StatelessWidget {
116- @override
117- Widget build(BuildContext context) {
118- return Container(
119- child: Center(
120- child: TextButton(
121- onPressed: () => Navigator.of(context).push(
122- MaterialPageRoute(
123- builder: (context) {
124- return BlocProvider<CounterBloc>(
125- child: MyHomePage(),
126- initBloc: () => CounterBloc(),
127- );
128- },
129- ),
130- ),
131- child: Text('GO TO HOME'),
132- ),
133- ),
134- );
135- }
136- }
137-
138- class MyHomePage extends StatefulWidget {
139- @override
140- _MyHomePageState createState() => _MyHomePageState();
141- }
142-
143- class _MyHomePageState extends State<MyHomePage> {
144- @override
145- Widget build(BuildContext context) {
146- return Scaffold(
147- appBar: AppBar(
148- title: Text('Home page'),
149- ),
150- body: Center(
151- child: Column(
152- mainAxisAlignment: MainAxisAlignment.center,
153- children: const <Widget>[
154- Text('You have pushed the button this many times:'),
155- TextCounter1(),
156- TextCounter2(),
157- ],
158- ),
159- ),
160- floatingActionButton: const IncrementButton(),
161- );
162- }
163- }
164-
165100class TextCounter1 extends StatelessWidget {
166101 const TextCounter1({Key? key}) : super(key: key);
167102
@@ -171,29 +106,10 @@ class TextCounter1 extends StatelessWidget {
171106
172107 return RxStreamBuilder<int>(
173108 stream: bloc.state,
174- builder: (context, data) {
175- return Text(
176- 'COUNTER 1: $data',
177- style: Theme.of(context).textTheme.headline4,
178- );
179- },
180- );
181- }
182- }
183-
184- class TextCounter2 extends StatelessWidget {
185- const TextCounter2({Key? key}) : super(key: key);
186-
187- @override
188- Widget build(BuildContext context) {
189- final bloc = context.bloc<CounterBloc>();
190-
191- return RxStreamBuilder<int>(
192- stream: bloc.state,
193- builder: (context, data) {
109+ builder: (context, state) {
194110 return Text(
195- 'COUNTER 2 : $data ',
196- style: Theme.of(context).textTheme.headline4 ,
111+ 'COUNTER 1 : $state ',
112+ style: Theme.of(context).textTheme.headline6 ,
197113 );
198114 },
199115 );
@@ -214,4 +130,5 @@ class IncrementButton extends StatelessWidget {
214130 );
215131 }
216132}
133+
217134```
0 commit comments