Last active
March 31, 2019 13:44
-
-
Save dsrenesanse/c5d2542d328bb0f0604471582b31b5b4 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 'package:flutter/material.dart'; | |
import 'dart:async'; | |
main() { | |
final state = State(); | |
final mutator = Mutator(state); | |
runApp(MyApp(state: state, mutator: mutator)); | |
} | |
class State { | |
var currentValue = 0; | |
final broadcaster = StreamController.broadcast(); | |
get valueStream => broadcaster.stream; | |
} | |
class Mutator { | |
final State state; | |
Mutator(this.state); | |
increment() { | |
state.broadcaster.add(++state.currentValue); | |
} | |
} | |
class MyApp extends StatelessWidget { | |
final State state; | |
final Mutator mutator; | |
const MyApp({Key key, this.state, this.mutator}) : super(key: key); | |
build(context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: StreamBuilder( | |
stream: state.valueStream, | |
builder: (context, snapshot) { | |
return Text(snapshot.data?.toString() ?? state.currentValue.toString()); | |
}, | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
mutator.increment(); | |
}, | |
child: Icon(Icons.add), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment