Created
March 11, 2019 10:59
-
-
Save brianegan/c4c97d2a0f0f65dc1158119b2cc81f0e to your computer and use it in GitHub Desktop.
Shows how to filter events from a Stream based on events from another Stream
This file contains hidden or 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:flutter/material.dart'; | |
import 'package:rxdart/rxdart.dart'; | |
import 'package:tuple/tuple.dart'; | |
void main() => runApp(MyApp()); | |
class CounterBloc { | |
final _counterSubject = BehaviorSubject.seeded(0); | |
final _isActive = BehaviorSubject.seeded(true); | |
Stream<int> counter; | |
CounterBloc() { | |
counter = Observable.combineLatest2( | |
_counterSubject, | |
_isActive, | |
(count, isActive) => Tuple2(count, isActive), | |
).where((tuple) => tuple.item2).map((tuple) => tuple.item1); | |
} | |
ValueObservable<bool> get active => _isActive; | |
void activate() => _isActive.value = true; | |
void deactivate() => _isActive.value = false; | |
void increment() => _counterSubject.value++; | |
void dispose() { | |
_counterSubject.close(); | |
_isActive.close(); | |
} | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@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 | |
void dispose() { | |
super.dispose(); | |
_bloc.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'You have pushed the button this many times:', | |
), | |
StreamBuilder<int>( | |
stream: _bloc.counter, | |
builder: (context, snapshot) { | |
return Text( | |
'${snapshot.data ?? 0}', | |
style: Theme.of(context).textTheme.display1, | |
); | |
}, | |
), | |
StreamBuilder<bool>( | |
initialData: _bloc.active.value, | |
stream: _bloc.active, | |
builder: (context, snapshot) { | |
var isActive = snapshot.data; | |
return RaisedButton( | |
child: Text(isActive ? 'Deactivate' : 'Activate'), | |
onPressed: () { | |
if (isActive) { | |
_bloc.deactivate(); | |
} else { | |
_bloc.activate(); | |
} | |
}, | |
); | |
}, | |
) | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _bloc.increment, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), // This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment