我已经在Stack Overflow Flutter getter isn't specified for the class, when it is specified上研究了这个问题。而且我仍然无法理解为什么我的类练习无法访问变量 _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,
),
);
}
}
我要尝试做的是,如果用户未在当前标签中输入文本,则不允许用户在右下角按下“加号”时创建新标签(请参见下图)。换句话说,如果它不为空。因此,我使用变量 final _text = TextEditingController()来检查按下加号按钮时当前标签是否为空。如果不是,则创建一个新标签。
最佳答案
dart将以下划线开头的变量视为私有(private)变量(因为dart中没有private关键字),因此为了解决您的问题,您需要在文本变量之前删除_(下划线)。
这是什么病
1-将_text变量移至由State类插入的TagColumn
类
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类以反射(reflect)这些更改
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,
),
);
}
}
关于flutter - 未在Flutter中为类TagColumn定义Getter _text,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61534880/