Skip to content

Instantly share code, notes, and snippets.

@Andrious
Created June 5, 2019 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andrious/00afedd85301072f6c8f5e3dbe9c041d to your computer and use it in GitHub Desktop.
Save Andrious/00afedd85301072f6c8f5e3dbe9c041d to your computer and use it in GitHub Desktop.
Demonstrating the GlobalKey used to display the SnackBar element.
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