Is it possible to pass arguments to PreferredSizeWidget?


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.


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#;

PreferredSizeWidget originAppBar = new PreferredSizeWidget(BuildContext context)

Widget build(BuildContext xontext){ 
    appBar: originAppBar(context)

Thank you.


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 *