Home > Back-end >  getting an error 'CustomAppbar' can't be assigned to the parameter type 'Preferr
getting an error 'CustomAppbar' can't be assigned to the parameter type 'Preferr

Time:11-21

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

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,

    );
  }
}

CodePudding user response:

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);
}
  • Related