Created
June 5, 2019 23:32
-
-
Save Andrious/00afedd85301072f6c8f5e3dbe9c041d to your computer and use it in GitHub Desktop.
Demonstrating the GlobalKey used to display the SnackBar element.
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/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatefulWidget { | |
MyApp({Key key}) : super(key: key); | |
State createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
int _count = 0; | |
final GlobalKey keyScaffold = new GlobalKey(); | |
@override | |
Widget build(BuildContext context) { | |
final title = 'Floating App Bar'; | |
final ThemeData theme = Theme.of(context); | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: title, | |
home: Scaffold( | |
key: keyScaffold, | |
appBar: AppBar( | |
title: Text('Sample Code'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('You have pressed the button $_count times.'), | |
RaisedButton( | |
onPressed: () { | |
final snackBar = SnackBar( | |
content: Text('Button\'s been pressed $_count times.')); | |
ScaffoldState state = keyScaffold.currentState; | |
state.showSnackBar(snackBar); | |
}, | |
elevation: 20.0, | |
textColor: theme.canvasColor, | |
color: theme.primaryColor, | |
child: Text('Display SnackBar'), | |
), | |
], | |
)), | |
bottomNavigationBar: BottomAppBar( | |
child: Container( | |
height: 50.0, | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () => setState(() { | |
_count++; | |
}), | |
tooltip: 'Increment Counter', | |
child: Icon(Icons.add), | |
), | |
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment