Skip to content

Instantly share code, notes, and snippets.

@burhanrashid52
Last active November 19, 2018 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burhanrashid52/2ce79acd0d7924ea38cf23ab510eedea to your computer and use it in GitHub Desktop.
Save burhanrashid52/2ce79acd0d7924ea38cf23ab510eedea to your computer and use it in GitHub Desktop.
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