Issue
I’m trying to add the CupertinoNavigationBar
with endDrawer, I try to add Gesture Detector on trailing, but isn’t work, shows this:
The following assertion was thrown while handling a gesture:
flutter: `Scaffold.of()` called with a context that does not contain a Scaffold.
flutter: No Scaffold ancestor could be found starting from the context that was passed to `Scaffold.of()`. This
flutter: usually happens when the context provided is from the same StatefulWidget as that whose build
I already tried to add the key to scaffold and try opened with the key, I try too with scaffold context in appbar
AppBar:
Scaffold(
appBar: CupertinoNavigationBar(
transitionBetweenRoutes: true,
trailing: IconButton(
icon: Icon(Icons.menu),
onPressed: () {
Scaffold.of(context).openEndDrawer();
},),
actionsForegroundColor: Colors.white,
middle: Text('Lejour', style: TextStyle(color: Colors.white)),
backgroundColor: Theme.of(context).primaryColor),
endDrawer: DrawerMenu() // my own class,
body: // ...body
I expect my trailing icon from CupertinoNavigationBar open the endDrawer with
Scaffold.of(context).openEndDrawer();
Solution
This is a common issue when you try to use Scaffold.of(context)
. You should read the error log.
No Scaffold ancestor could be found starting from the context that was
passed toScaffold.of()
To solve your issue, use the Builder
widget to generate a new a context.
trailing: Builder(
builder: (context) {
return IconButton(
icon: Icon(Icons.menu),
onPressed: () {
Scaffold.of(context).openEndDrawer();
},
);
},
),
Answered By – diegoveloper
Answer Checked By – Pedro (FlutterFixes Volunteer)