dart - 访问超类变量时出错
问题描述
我有以下飞镖继承结构:
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。但是,不会生成文本字段。应用栏仍然显示标题。我没有进行热重载或热重启。我实际上做了一个完全重启。
解决方案
这是你需要做的一种巧妙的方式
import 'package:flutter/material.dart';
class MySuperList extends StatefulWidget{
final category_name;
MySuperList({this.category_name});
@override
State<StatefulWidget> createState() {
return _MySuperListState();
}
}
class _MySuperListState extends State<MySuperList>{
bool _isSearching=false;
TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: buildAppBar(),
);
}
Widget buildAppBar(){
return AppBar(
centerTitle: false,
title: getAppBarTitle(),
actions: <Widget>[
getAction()
],
);
}
Widget getAction(){
if(_isSearching){
return IconButton(
icon: Icon(
Icons.close
),
onPressed: (){
setState(() {
_controller.clear();
_isSearching = false;
});
},
);
}else{
return IconButton(
icon: Icon(
Icons.search
),
onPressed: (){
setState(() {
_isSearching = true;
});
},
);
}
}
Widget getAppBarTitle(){
if(_isSearching){
return 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{
return Text(
widget.category_name,
style: new TextStyle(color: Colors.white)
);
}
}
searchOperation(String value){
//do what you need to onChanged
}
}
推荐阅读
- r - 在R中结合嵌套()和聚合()?
- html - 使用Bootstrap将div中的项目对齐到较大设备上的水平方向和较小设备上的垂直方向?
- r-markdown - 从 HTML 转换为 PDF 时用 Rmd 编写的幻灯片缺少页面
- php - 使用 ajax 将值从一个 PHP 页面传递到另一个页面
- css - 跨多个元素的连续边框
- json - 我应该通过 JSON 发送图像吗
- python - 如何在tensorflow中使用预批处理数据和不同形状的数据
- javascript - 修改 Selectize.js 库以允许编辑选定的选项
- typescript - 扩展类型的原型
- python - Python - OSError:[WinError 10013] 试图以访问权限禁止的方式访问套接字