Created
June 27, 2017 14:44
-
-
Save collinjackson/85530d110ce4d20630a00e77ab6bf825 to your computer and use it in GitHub Desktop.
Managing state with streams - answer to a question https://stackoverflow.com/questions/44769472/flutter-generate-in-app-interactivity/44769847
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(new KeyboardApp()); | |
} | |
class KeyboardApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
home: new HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
HomePageState createState() => new HomePageState(); | |
} | |
class HomePageState extends State<HomePage> { | |
final StreamController<List<int>> controller = new StreamController.broadcast(); | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
body: new ListView ( | |
children: <Widget>[ | |
new NumberDisplay(numbers: controller.stream), | |
new Keyboard(controller: controller), | |
], | |
), | |
); | |
} | |
} | |
class NumberDisplay extends StatelessWidget { | |
NumberDisplay({ this.numbers }); | |
final Stream<List<int>> numbers; | |
final Color _color = new Color(0xFFE57373); | |
@override | |
Widget build(BuildContext context) { | |
return new Container( | |
height: 145.0, | |
child: new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
new Container( | |
padding: new EdgeInsets.only(right: 16.0, top: 35.0), | |
child: new Row( | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: <Widget>[ | |
new Text( | |
'\$ ', | |
style: new TextStyle( | |
fontFamily: 'Roboto', | |
color: new Color(0xFFFFFFFF), | |
fontSize: 20.0 | |
) | |
), | |
new StreamBuilder( | |
stream: numbers, | |
builder: (BuildContext context, AsyncSnapshot<List<int>> snapshot) { | |
return new Text( | |
snapshot.data?.join('') ?? '', | |
style: new TextStyle( | |
fontFamily: 'Roboto', | |
color: new Color(0xFFFFFFFF), | |
fontSize: 45.0 | |
), | |
); | |
}, | |
) | |
], | |
), | |
), | |
], | |
), | |
color: _color, | |
); | |
} | |
} | |
class Keyboard extends StatefulWidget { | |
Keyboard({ this.controller }); | |
final StreamController<List<int>> controller; | |
State createState() => new KeyboardState(); | |
} | |
class KeyboardState extends State<Keyboard> { | |
List<int> numbers = <int>[]; | |
final Color _color = new Color(0xFFE57373); | |
@override | |
Widget build(BuildContext context) { | |
return new Container( | |
padding: new EdgeInsets.only(right: 15.0, left: 15.0, top: 47.0, bottom: 20.0), | |
child: new Column( | |
children: <Widget>[ | |
new Row( | |
mainAxisAlignment: MainAxisAlignment.spaceAround, | |
children: <Widget>[ | |
new FlatButton( | |
textColor: _color, | |
child: new Text( | |
'1', | |
style: new TextStyle( | |
fontSize: 35.0 | |
), | |
), | |
onPressed: () { | |
widget.controller.add(numbers..add(1)); | |
}, | |
), | |
new FlatButton( | |
textColor: _color, | |
child: new Text( | |
'2', | |
style: new TextStyle( | |
fontSize: 35.0 | |
), | |
), | |
onPressed: () { | |
widget.controller.add(numbers..add(2)); | |
}, | |
), | |
new FlatButton( | |
textColor: _color, | |
child: new Icon( | |
Icons.backspace, | |
size: 35.0 | |
), | |
onPressed: () { | |
widget.controller.add(numbers..removeLast()); | |
}, | |
), | |
], | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment