微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解决 PreferredSize flutter 中的零参数构造函数错误?

如何解决如何解决 PreferredSize flutter 中的零参数构造函数错误?

我正在开发一个项目,在该项目中我使用了自定义 AppBar,因此使用了 PreferredSize,但我收到一条错误消息:

The superclass 'PreferredSize' doesn't have a zero argument constructor. Try declaring a zero argument constructor in 'PreferredSize',or explicitly invoking a different constructor in 'PreferredSize'.

这是我的代码部分:

class CustomAppBar extends PreferredSize {
  final Widget child;
  final double height;


  CustomAppBar({required this.child,required this.height}); //getting error on this line

  @override
  Size get preferredSize => Size.fromHeight(height);

  @override
  Widget build(BuildContext context) {
    return Container()
}
}

编辑: 删除 PreferredSize 后出现此错误

The named parameter 'child' isn't defined. Try correcting the name to an existing named parameter's name,or defining a named parameter with the name 'child'.

appBar: 
PreferredSize(
preferredSize:Size.fromHeight(95),child:
AppBar(
  
  child: Column(
    children: <Widget>[
      SizedBox(height: 40),Row(
        children: <Widget>[
          Builder(
            builder: (context) => FlatButton.icon(
                onpressed: () => Scaffold.of(context).openDrawer(),icon: Icon(Icons.menu),label: Text('')),),SizedBox(width: 60),Text('Times ',style: GoogleFonts.blackHanSans(
                  textStyle:
                      TextStyle(color: Colors.black,fontSize: 20))),Image.asset(
            'assets/newss.png',height: 50,],)

解决方法

您甚至可能不必扩展 /,您只需将 appBar 或任何其他小部件包装在 PreferredSize 内并指定其高度即可。

PreferredSize

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。