flutter 获取错误无法将“CustomAppbar”分配给参数类型“PreferredSizeWidget?”

q0qdq0h2  于 2022-11-26  发布在  Flutter
关注(0)|答案(1)|浏览(112)

创建简单的演示,如果列表的项目被选中,我想显示customappbar的两倍高度。否则默认高度
创建customappbar时出现错误,

**Appbar看起来与其他Widget不同,**这就是它生成错误的原因

这里的另一个问题是如何获得默认appbar的高度,这样我就可以将其加倍

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(),);
  }

自定义应用程序栏类

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,

    );
  }
}
bn31dyow

bn31dyow1#

你的CustomAppbar小工具应该使用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);
}

相关问题