【发布时间】:2019-06-30 02:14:36
【问题描述】:
我有以下飞镖继承结构:
class MySuperList extends StatefulWidget {
final category_name;
MySuperList({this.category_name});
@override
_MySuperListState createState() => new _MySuperListState();
}
class _MySuperListState extends State<MySuperList> {
Widget appBarTitle = new Text(
widget.category_name, <== Only static members can be accessed in initializers
style: new TextStyle(color: Colors.white);
正如你所看到的,当我尝试使用 widget.category_name 访问超类中变量的值时,我收到以下编译器错误:
在初始化器中只能访问静态成员
我还能如何访问这个值?
更新 遵循建议的答案后,文本现在卡在 appbar 中。它确实根据以下代码进行了更改:
Widget buildAppBar(BuildContext context) {
return new AppBar(centerTitle: false, title: getAppBarTitle(), actions: <Widget>[
new IconButton(
icon: icon,
onPressed: () {
this.appBarTitle = null;
setState(() {
if (this.icon.icon == Icons.search) {
this.icon = new Icon(
Icons.close,
color: Colors.white,
);
this.appBarTitle = new TextField(
controller: _controller,
autofocus: true,
style: new TextStyle(
color: Colors.white,
),
decoration: new InputDecoration(
prefixIcon: new Icon(Icons.search, color: Colors.white),
hintText: "Search...",
hintStyle: new TextStyle(color: Colors.white)),
onChanged: searchOperation,
);
} else {
_handleSearchEnd();
}
});
},
),
]);
}
void _handleSearchEnd() {
setState(() {
this.icon = new Icon(
Icons.search,
color: Colors.white,
);
_isSearching = false;
this.appBarTitle = new Text(
widget.category_name,
style: new TextStyle(color: Colors.white),
);
_controller.clear();
});
}
如您所见,当单击搜索图标时,我将 appBarTitle 设置为 TextField。但是,不会生成文本字段。应用栏仍然显示标题。我没有进行热重载或热重启。我实际上做了一个完全重启。
【问题讨论】: