Created
July 3, 2018 00:29
-
-
Save mjohnsullivan/706857facfa8fb53998c17edb452244f to your computer and use it in GitHub Desktop.
The classic Flutter incrementer app with added SnackBar
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'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Snackbars', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Snackbars'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
void _incrementCounter() => setState(() => _counter++); | |
@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:', | |
), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.display1, | |
), | |
], | |
), | |
), | |
// The button is wrapped in a Builder as Scaffold.of(context) cannot | |
// be called in the same build function that creates the Scaffold. | |
floatingActionButton: Builder( | |
builder: (context) => FloatingActionButton( | |
onPressed: () { | |
_incrementCounter(); | |
Scaffold.of(context).showSnackBar(SnackBar( | |
content: Text('Counter is now $_counter'), | |
)); | |
}, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment