Last active
November 19, 2018 03:52
-
-
Save burhanrashid52/2ce79acd0d7924ea38cf23ab510eedea to your computer and use it in GitHub Desktop.
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:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
final CounterBloc _counterBloc = CounterBloc(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("BLoC"), | |
), | |
body: Center( | |
child: StreamBuilder<int>( | |
initialData: 0, | |
stream: _counterBloc.outCounter, | |
builder: (context, snapshot) { | |
return Text(snapshot.data.toString(), | |
style: Theme.of(context).textTheme.display1); | |
}, | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
_counterBloc.incrementCount(); | |
}, | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} | |
class CounterBloc { | |
int _counter = 0; | |
StreamController<int> _counterController = StreamController<int>(); | |
Stream<int> get outCounter => _counterController.stream; | |
void dispose() { | |
_counterController.close(); | |
} | |
void incrementCount() { | |
_counter++; | |
_counterController.sink.add(_counter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment