首页 > 解决方案 > 构造继承的小部件时如何设置基类的属性?

问题描述

我想创建一个继承的AppBar称为HomeAppBar,它继承自CommonAppBar,它继承自AppBarCommonAppBar将背景设置为紫色并将HomeAppBar标题设置为Hourlogger

class CommonAppBar extends AppBar {
  CommonAppBar() : super(backgroundColor: Colors.purple);
}

class HomeAppBar extends CommonAppBar {
  HomeAppBar() : super(title: Text("Hourlogger"));
}

但是编译器说

未定义命名参数“title”。尝试将名称更正为现有命名参数的名称,或使用名称“title”定义命名参数。

我的印象是CommonAppBar应该titleAppBar. 我应该如何title在我的自定义中设置AppBar

标签: flutterdartflutter-layout

解决方案


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


推荐阅读