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

getting an error 'CustomAppbar' can't be assigned to the parameter type 'PreferredSizeWidget?'

Creating simple demo regarding if list’s item is selected than I want to show customappbar with double height.otherwise default height

I am getting an error while creating a customappbar,

it looks like Appbar is not like other Widget, and thats why it is generating an error

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

here another question is how to get the height of default appbar so that I can double it

class _Stack13State extends State<Stack13> {
  bool islongpressed = false;
  List<Movie> selectedmovies = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: islongpressed == true
            ? CustomAppbar(title: Text('Select Any'), height: /*default height*/)
                : CustomAppbar(title: Text('Selected'),
        height: /* double than default height*/),
        body: showlistview(),);
  }

CustomAppbar class


class CustomAppbar extends StatelessWidget {

  final Widget title;
  final double height;

  const CustomAppbar({Key? key,required this.title,required this.height}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AppBar(

      height://how to set height of appbar
     title: title,

    );
  }
}

>Solution :

Your CustomAppbar widget should use mixin PreferredSizeWidget.

class CustomAppbar extends StatelessWidget with PreferredSizeWidget {
  final Widget title;
  final double height;

  const CustomAppbar({Key? key,required this.title,required this.height}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: title,
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(height);
}
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