Getter _text 没有为 Flutter 中的 class TagColumn 定义
Getter _text isn't defined for class TagColumn in Flutter
我在 Stack Overflow 上看过这个问题 。而且我仍然不明白为什么我的 class Practice 无法访问变量 _text ,它是从 类型为 TagColumn 的列表。
class Practice extends StatefulWidget {
@override
_PracticeState createState() => _PracticeState();
}
class _PracticeState extends State<Practice>{
int count = 0;
@override
Widget build(BuildContext context){
List<TagColumn> ok = List.generate(count, (int i) => new TagColumn());
return Scaffold(
backgroundColor: Colors.black,
body: new LayoutBuilder(builder: (context, constraint){
return new Stack(
children: <Widget>[
SingleChildScrollView(
child: SafeArea(
child: new Wrap(
direction: Axis.horizontal,
children: ok,
)
),
),
new Positioned(
child: new Align(
alignment: FractionalOffset.bottomRight,
child: Container(
margin: EdgeInsets.only(bottom: 50.0, right: 40.0),
child: RawMaterialButton(
onPressed: (){
setState(() {
if(count != 0 && ok[count]._text.text.isEmpty){
}
else{
count +=1;
}
});
},
shape: CircleBorder(),
child: Icon(
Icons.add_circle,
size: 100.0,
color: Color(0xffd3d3d3),
),
)
)
)
)
],
);
}),
);
}
}
class TagColumn extends StatefulWidget{
@override
State<StatefulWidget> createState() => new _TagColumn();
}
class _TagColumn extends State<TagColumn>{
final _text = TextEditingController();
bool _validate = false;
@override
Widget build(BuildContext context){
final tagField = TextField(
controller: _text,
obscureText: false,
style: TextStyle(fontFamily: 'Play', color: Colors.white, fontSize: 20),
maxLines: null,
keyboardType: TextInputType.text,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Tag",
errorText: _validate ? 'Value Can\'t be Empty': null,
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
return Container(
width: MediaQuery.of(context).size.width/2 - 40,
margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(32.0),
),
child: Theme(
data: ThemeData(
hintColor: Colors.white,
),
child: tagField,
),
);
}
}
我想做的是不允许用户在按下右下角的"Plus,"时创建新标签(见下图)如果用户没有在当前文本中输入文本。换句话说,如果它不是空的。因此,我使用变量 final _text = TextEditingController(),在按下加号按钮时检查当前标签是否为空。如果不是,则创建一个新标签。
dart 将以下划线开头的变量视为私有变量(因为 dart 中没有 private 关键字)所以为了解决您的问题,您需要删除文本变量前的 _(下划线)。
这是什么病
1- 将 _text 变量移动到 TagColumn
class 状态 class
class TagColumn extends StatefulWidget{
final text = TextEditingController(); // removed the _ so that to access it inside the Practise class
@override
State<StatefulWidget> createState() => new _TagColumn();
}
并更新 TagColumn class 以反映这些更改
class _TagColumn extends State<TagColumn>{
// final _text = TextEditingController(); <---- since the text is now in the TagColumn class not the state class
bool _validate = false;
@override
Widget build(BuildContext context){
final tagField = TextField(
controller: widget.text,
obscureText: false,
style: TextStyle(fontFamily: 'Play', color: Colors.white, fontSize: 20),
maxLines: null,
keyboardType: TextInputType.text,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Tag",
errorText: _validate ? 'Value Can\'t be Empty': null,
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
return Container(
width: MediaQuery.of(context).size.width/2 - 40,
margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(32.0),
),
child: Theme(
data: ThemeData(
hintColor: Colors.white,
),
child: tagField,
),
);
}
}
我在 Stack Overflow 上看过这个问题
class Practice extends StatefulWidget {
@override
_PracticeState createState() => _PracticeState();
}
class _PracticeState extends State<Practice>{
int count = 0;
@override
Widget build(BuildContext context){
List<TagColumn> ok = List.generate(count, (int i) => new TagColumn());
return Scaffold(
backgroundColor: Colors.black,
body: new LayoutBuilder(builder: (context, constraint){
return new Stack(
children: <Widget>[
SingleChildScrollView(
child: SafeArea(
child: new Wrap(
direction: Axis.horizontal,
children: ok,
)
),
),
new Positioned(
child: new Align(
alignment: FractionalOffset.bottomRight,
child: Container(
margin: EdgeInsets.only(bottom: 50.0, right: 40.0),
child: RawMaterialButton(
onPressed: (){
setState(() {
if(count != 0 && ok[count]._text.text.isEmpty){
}
else{
count +=1;
}
});
},
shape: CircleBorder(),
child: Icon(
Icons.add_circle,
size: 100.0,
color: Color(0xffd3d3d3),
),
)
)
)
)
],
);
}),
);
}
}
class TagColumn extends StatefulWidget{
@override
State<StatefulWidget> createState() => new _TagColumn();
}
class _TagColumn extends State<TagColumn>{
final _text = TextEditingController();
bool _validate = false;
@override
Widget build(BuildContext context){
final tagField = TextField(
controller: _text,
obscureText: false,
style: TextStyle(fontFamily: 'Play', color: Colors.white, fontSize: 20),
maxLines: null,
keyboardType: TextInputType.text,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Tag",
errorText: _validate ? 'Value Can\'t be Empty': null,
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
return Container(
width: MediaQuery.of(context).size.width/2 - 40,
margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(32.0),
),
child: Theme(
data: ThemeData(
hintColor: Colors.white,
),
child: tagField,
),
);
}
}
我想做的是不允许用户在按下右下角的"Plus,"时创建新标签(见下图)如果用户没有在当前文本中输入文本。换句话说,如果它不是空的。因此,我使用变量 final _text = TextEditingController(),在按下加号按钮时检查当前标签是否为空。如果不是,则创建一个新标签。
dart 将以下划线开头的变量视为私有变量(因为 dart 中没有 private 关键字)所以为了解决您的问题,您需要删除文本变量前的 _(下划线)。
这是什么病
1- 将 _text 变量移动到 TagColumn
class 状态 class
class TagColumn extends StatefulWidget{
final text = TextEditingController(); // removed the _ so that to access it inside the Practise class
@override
State<StatefulWidget> createState() => new _TagColumn();
}
并更新 TagColumn class 以反映这些更改
class _TagColumn extends State<TagColumn>{
// final _text = TextEditingController(); <---- since the text is now in the TagColumn class not the state class
bool _validate = false;
@override
Widget build(BuildContext context){
final tagField = TextField(
controller: widget.text,
obscureText: false,
style: TextStyle(fontFamily: 'Play', color: Colors.white, fontSize: 20),
maxLines: null,
keyboardType: TextInputType.text,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Tag",
errorText: _validate ? 'Value Can\'t be Empty': null,
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
return Container(
width: MediaQuery.of(context).size.width/2 - 40,
margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(32.0),
),
child: Theme(
data: ThemeData(
hintColor: Colors.white,
),
child: tagField,
),
);
}
}