You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AppBar action needs a key to see its own Scaffold #4581
[This is an instance of a general issue and it could equally well be titled: 'Patterns for referring (using .of) to widgets introduced in the same build function']
Apps commonly have a single widget that contains a Scaffold and an AppBar. It's inconvenient that the AppBar's actions can't refer to the Scaffold directly. This can be overcome by providing the Scaffold with a key as in the example below.
It would be convenient if Scaffold.of(context) would return the Scaffold contained by context if there was no Scaffold ancestor.
Creating a GlobalKey to refer to the Scaffold can be fraught, see #4579.
import 'package:flutter/material.dart';
class Home extends StatelessWidget {
// The AppBar's action needs this key to find its own Scaffold.
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
appBar: new AppBar(
title: new Text('Home'),
actions: <Widget>[
new IconButton(
icon: Icons.shopping_cart,
onPressed: () {
// Scaffold.of(context) == null because our context contains
// Scaffold. So we have to give the Scaffold a GlobalKey
// and use it here.
_scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text('$_scaffoldKey')
));
}
)
]
),
body: new Center(child: new Text('Click the shopping cart'))
);
}
}
void main() {
runApp(new MaterialApp(home: new Home()));
}
changed the title [-]AppBar action needs a key to see its own Scaffold[/-][+]Patterns for referring (using .of) to widgets introduced in the same build function[/+]on Dec 29, 2016
This is a general issue, not really specific to AppBar. GlobalKey and Builder are the current state of the art solutions. If GlobalKey and Builder are not sufficient, please reopen this bug with more information explaining why.
changed the title [-]Patterns for referring (using .of) to widgets introduced in the same build function[/-][+] AppBar action needs a key to see its own Scaffold [/+]on Jan 6, 2017
changed the title [-] AppBar action needs a key to see its own Scaffold [/-][+]Scaffold.appBar doesn't accept widgets that aren't AppBars.[/+]on Feb 17, 2017
changed the title [-]Scaffold.appBar doesn't accept widgets that aren't AppBars.[/-][+]AppBar action needs a key to see its own Scaffold [/+]on Apr 12, 2017
Can you post your actual code? (Maybe file a new bug.)
In general appBar needs the widget to implement PreferredSizeWidget (e.g. it could be a PreferredSize widget or an AppBar), but exactly what you do to work around that depends on what you're trying to do.
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.
Activity
vy0592 commentedon Oct 14, 2016
I'm running into the same issue. The workaround works but I'd still prefer snackbar can find its own scaffold without a global key.
Hixie commentedon Oct 14, 2016
One workaround is to put a Builder widget in the tree and use its context instead.
Hixie commentedon Oct 14, 2016
something like:
[-]AppBar action needs a key to see its own Scaffold[/-][+]Patterns for referring (using .of) to widgets introduced in the same build function[/+]Hixie commentedon Dec 29, 2016
This is a general issue, not really specific to AppBar. GlobalKey and Builder are the current state of the art solutions. If GlobalKey and Builder are not sufficient, please reopen this bug with more information explaining why.
HansMuller commentedon Jan 6, 2017
This is in fact an AppBar issue because the type of Scaffold's AppBar is AppBar, not Widget. The suggested workaround doesn't compile.
Hopefully the "great Scrollable refactor" will eliminate this issue.
[-]Patterns for referring (using .of) to widgets introduced in the same build function[/-][+] AppBar action needs a key to see its own Scaffold [/+]abarth commentedon Feb 10, 2017
Still a problem but likely a trivial fix now.
[-] AppBar action needs a key to see its own Scaffold [/-][+]Scaffold.appBar doesn't accept widgets that aren't AppBars.[/+][-]Scaffold.appBar doesn't accept widgets that aren't AppBars.[/-][+]AppBar action needs a key to see its own Scaffold [/+]HansMuller commentedon Apr 12, 2017
I'm closing the original issue, since we're not planning to do anything about it.
I've opened a new issue, #9349, which covers the problem this issue had morphed into (the type of Scaffold's appBar property is AppBar not Widget).
Update dart in engine to version 8d9d68751a505426eb5f59a9d29f103fde6b…
fabiomotiws commentedon Sep 13, 2018
Hi, I dit it, work for me :)
kszyrvercobrador commentedon Sep 18, 2018
@Hixie Putting builder on appBar triggers this error.
Hixie commentedon Oct 2, 2018
Can you post your actual code? (Maybe file a new bug.)
In general
appBarneeds the widget to implementPreferredSizeWidget(e.g. it could be aPreferredSizewidget or anAppBar), but exactly what you do to work around that depends on what you're trying to do.eddiegroves commentedon Dec 10, 2018
Here's how I solved showing a
ShackBarviaAppBar, hopefully this is the correct use ofPreferredSizeWidget.gazialankus commentedon Dec 31, 2018
Another alternative is to wrap things inside
AppBarwith aBuildereach. This works for me:github-actions commentedon Aug 31, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.