Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ffeu
Created August 3, 2018 00:18
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ffeu/e77664dd322089649d9b6f0f8fb04d42 to your computer and use it in GitHub Desktop.
Save ffeu/e77664dd322089649d9b6f0f8fb04d42 to your computer and use it in GitHub Desktop.
Flutter Main Example with StreamBuilder
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
StreamController<int> _events;
@override
initState() {
super.initState();
_events = new StreamController<int>();
_events.add(0);
}
void _incrementCounter() {
_counter++;
_events.add(_counter);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("StreamBuilder test"),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'You have pushed the button this many times:',
),
StreamBuilder(
stream: _events.stream,
builder: (BuildContext context, snapshot) {
return new Text(
snapshot.data.toString(),
style: Theme.of(context).textTheme.display1,
);
},
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new 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