Skip to content

Instantly share code, notes, and snippets.

@Andrious
Created June 5, 2019 23:34
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/bba436376810be62b121599d1bdc3304 to your computer and use it in GitHub Desktop.
Save Andrious/bba436376810be62b121599d1bdc3304 to your computer and use it in GitHub Desktop.
Demonstrating the Scaffold's State object
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();
String hasAppBar = '';
String hasDrawer = '';
String hasEndDrawer = '';
String hasFloatingActionButton = '';
String appBarMaxHeight = '';
String isDrawerOpen = '';
String isEndDrawerOpen = '';
@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: Builder(
builder: (context) => 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;
ScaffoldState state = Scaffold.of(context);
state.showSnackBar(snackBar);
hasAppBar = "Is there an App bar?: ${state.hasAppBar}";
hasDrawer = "Is there an Drawer?: ${state.hasDrawer}";
hasEndDrawer =
"Is there an End drawer?: ${state.hasEndDrawer}";
hasFloatingActionButton =
"Is there an Floating Action button?: ${state.hasFloatingActionButton}";
appBarMaxHeight =
"Max height of the app bar is ${state.appBarMaxHeight}";
isDrawerOpen =
"Is the drawer open?: ${state.isDrawerOpen}";
isEndDrawerOpen =
"Is the end drawer open?: ${state.isEndDrawerOpen}";
setState(() {});
},
elevation: 20.0,
textColor: theme.canvasColor,
color: theme.primaryColor,
child: Text('Display SnackBar'),
),
Text('$hasAppBar'),
Text('$hasDrawer'),
Text('$hasEndDrawer'),
Text('$hasFloatingActionButton'),
Text('$appBarMaxHeight'),
Text('$isDrawerOpen'),
Text('$isEndDrawerOpen'),
],
))),
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