Created
January 8, 2020 05:17
-
-
Save shakir915/f34c62063b1775cbfad767fb5636c187 to your computer and use it in GitHub Desktop.
BLOC pattern flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:bloc_vanilla_tut/counter_event.dart'; | |
class CounterBloc { | |
int _counter = 0; | |
final _counterStateController = StreamController<int>(); | |
StreamSink<int> get _inCounter => _counterStateController.sink; | |
// For state, exposing only a stream which outputs data | |
Stream<int> get counter => _counterStateController.stream; | |
final _counterEventController = StreamController<CounterEvent>(); | |
// For events, exposing only a sink which is an input | |
Sink<CounterEvent> get counterEventSink => _counterEventController.sink; | |
CounterBloc() { | |
// Whenever there is a new event, we want to map it to a new state | |
_counterEventController.stream.listen(_mapEventToState); | |
} | |
void _mapEventToState(CounterEvent event) { | |
if (event is IncrementEvent) | |
_counter++; | |
else | |
_counter--; | |
_inCounter.add(_counter); | |
} | |
void dispose() { | |
_counterStateController.close(); | |
_counterEventController.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class CounterEvent {} | |
class IncrementEvent extends CounterEvent {} | |
class DecrementEvent extends CounterEvent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:bloc_vanilla_tut/counter_bloc.dart'; | |
import 'package:bloc_vanilla_tut/counter_event.dart'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final _bloc = CounterBloc(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: StreamBuilder( | |
stream: _bloc.counter, | |
initialData: 0, | |
builder: (BuildContext context, AsyncSnapshot<int> snapshot) { | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'You have pushed the button this many times:', | |
), | |
Text( | |
'${snapshot.data}', | |
style: Theme.of(context).textTheme.display1, | |
), | |
], | |
); | |
}, | |
), | |
), | |
floatingActionButton: Row( | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: <Widget>[ | |
FloatingActionButton( | |
onPressed: () => _bloc.counterEventSink.add(IncrementEvent()), | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
SizedBox(width: 10), | |
FloatingActionButton( | |
onPressed: () => _bloc.counterEventSink.add(DecrementEvent()), | |
tooltip: 'Decrement', | |
child: Icon(Icons.remove), | |
), | |
], | |
), | |
); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
_bloc.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment