Home > Net >  why PreferredSizeWidget still not work after wrapped with PreferredSize in flutter
why PreferredSizeWidget still not work after wrapped with PreferredSize in flutter

Time:11-20

After I wrapped the appBar sub component with PreferredSize in flutter, the appBar still did not work and shows:

The argument type 'Widget' can't be assigned to the parameter type 'PreferredSizeWidget?'.

this is my code which build the appBar and body of flutter page:

@override
  Widget build(BuildContext context) {
    return SafeArea(child: Scaffold(
      appBar: _buildAppBar(context),
      body:_buildBody(context),
    ));
  }

and this is the _buildAppBar implemention:

Widget _buildAppBar(BuildContext context) {
    return PreferredSize(
      child: Container(
        padding: EdgeInsets.only(left: 8, right: 8, top: 0),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ToolbarItemAlwaysOnTop(),
            Expanded(child: Container()),
            // ToolbarItemSponsor(),
            ToolbarItemSettings(
              onSettingsPageDismiss: () {
                setState(() {});
              },
            ),
          ],
        ),
      ),
      preferredSize: Size.fromHeight(34),
    );
  }

what should I do to make it work? I have readed the question and suggest to extend the PreferredSizeWidget, I could not implement PreferredSizeWidget in my code right now(I have tried but not work for this situation).

CodePudding user response:

Replace return type of _buildAppBar() from Widget to PreferredSize

you can check From PreferredSize class

class PreferredSize extends StatelessWidget implements PreferredSizeWidget

CodePudding user response:

Just make the return type of _buildAppBar(context) function return preferredSize

  • Related