如何解决 PreferredSize flutter 中的零参数构造函数错误? [英] How to solve zero Argument constructor error in PreferredSize flutter?

查看:64
本文介绍了如何解决 PreferredSize flutter 中的零参数构造函数错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a project where I am using a custom AppBar and hence using PreferredSize but I am getting a error stating that:

超类PreferredSize"没有零参数构造函数.尝试在PreferredSize"中声明零参数构造函数,或在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 后出现此错误:

Edit : After removing PreferredSize got this error:

未定义命名参数child".尝试将名称更正为现有命名参数的名称,或使用名称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,
          ),
        ],
      )

推荐答案

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

You may not even have to extend PreferredSize you may just wrap an appBar or any other widget inside a PreferredSize and just specify its height.

PreferredSize(
            preferredSize: Size.fromHeight(72),
            child: AppBar(
              title: Text(title, style: TextStyles.h1),
              centerTitle: false,
              elevation: 0,
              brightness: Brightness.light,
              backwardsCompatibility: false,
              bottom: bottomAppBarWidget,
            ))

这篇关于如何解决 PreferredSize flutter 中的零参数构造函数错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆