Skip to content

Instantly share code, notes, and snippets.

@machinescream
Last active March 31, 2019 13:44
Show Gist options
  • Save machinescream/c5d2542d328bb0f0604471582b31b5b4 to your computer and use it in GitHub Desktop.
Save machinescream/c5d2542d328bb0f0604471582b31b5b4 to your computer and use it in GitHub Desktop.
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