gpt4 book ai didi

dart - 访问父类(super class)变量时出错

转载 作者:IT王子 更新时间:2023-10-29 07:23:05 24 4
gpt4 key购买 nike

我有以下 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 访问父类(super class)中变量的值时,出现以下编译器错误:

Only static members can be accessed in initializers

我还能如何访问此值?

更新按照建议的答案进行操作后,文本现在卡在应用栏中。它确实会根据以下代码进行更改:

 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。但是,不会生成文本字段。 appbar 仍然显示标题。我没有进行热重载或热重启。我实际上完全重启了。

最佳答案

这是你需要做的一个巧妙的方法

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
}

}

关于dart - 访问父类(super class)变量时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54548327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com