flutter - 构造继承的小部件时如何设置基类的属性?
问题描述
我想创建一个继承的AppBar
称为HomeAppBar
,它继承自CommonAppBar
,它继承自AppBar
。CommonAppBar
将背景设置为紫色并将HomeAppBar
标题设置为Hourlogger
class CommonAppBar extends AppBar {
CommonAppBar() : super(backgroundColor: Colors.purple);
}
class HomeAppBar extends CommonAppBar {
HomeAppBar() : super(title: Text("Hourlogger"));
}
但是编译器说
未定义命名参数“title”。尝试将名称更正为现有命名参数的名称,或使用名称“title”定义命名参数。
我的印象是CommonAppBar
应该title
从AppBar
. 我应该如何title
在我的自定义中设置AppBar
?
解决方案
class CommonAppBar extends AppBar {
final Widget title;
CommonAppBar({this.title}) : super(backgroundColor: Colors.purple, title: title);
}
class HomeAppBar extends CommonAppBar {
HomeAppBar() : super(title: Text("Hourlogger"));
}
这应该可以在您首先从构造函数中获取 title 参数并将其传递给 super
推荐阅读
- python - 如何在 python 中对 tensor2tensor 模型进行推理(无需解码二进制文件和 TensorFlow Serving)
- python-3.7 - 如何在 Python 的 Dataclasses 中使用 __post_init__ 方法
- html - 期望日期/时间值的 HTML 属性是什么?
- java - Java 方法在使用硬编码对象时正确执行,但在使用 ArrayList 时不正确
- reactjs - ReactJS:有没有办法查看用于列表所需的键属性的值
- python - Python 类型提示 Dict 语法错误 不允许使用可变默认值。使用“默认工厂”
- python - Pyspark RDD 以不同方式聚合不同的值字段
- linux - 我上一个 linux 期中考试的 PATH 问题,我需要有人解释
- python - 在 docker 容器中安装 tensorflow
- node.js - 数组显示为未定义,即使我能够记录它的内容