I declared Scaffold
's appBar as a variable out of scope.
PreferredSizeWidget originAppBar = AppBar(
centerTitle: true,
backgroundColor: Colors.white,
title: ....
This allows me to use it by assigning originAppBar
to appBar:
.
appBar: originAppBar,
However, I have included the following code in originAppBar
.
Navigator.of(context).pushNamed(Profile.route);
This is a function that requires a BuildContext
and is not in the originAppBar
.
Is it possible to have a PreferredSizeWidget with an argument? Like this C#;
//C#
PreferredSizeWidget originAppBar = new PreferredSizeWidget(BuildContext context)
Widget build(BuildContext xontext){
....
appBar: originAppBar(context)
....
}
Thank you.
CodePudding user response:
Use GlobalKey - assign it to your widget
Global keys uniquely identify elements. Global keys provide access to other objects that are associated with those elements, such as BuildContext.
https://api.flutter.dev/flutter/widgets/GlobalKey-class.html
CodePudding user response:
It seems that the definition was wrong. The following is correct.
PreferredSizeWidget originAppBar(BuildContext context) {
return AppBar(....
}
....
Widget build(BuildContext context) {
return Scaffold(
appBar: originAppBar(context),