Is it possible to pass arguments to PreferredSizeWidget?

Issue

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.

Solution

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),

Answered By – Beginner_

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *