Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why PreferredSizeWidget still not work after wrapped with PreferredSize in flutter

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Replace return type of _buildAppBar() from Widget to PreferredSize

you can check From PreferredSize class

class PreferredSize extends StatelessWidget implements PreferredSizeWidget

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading