【发布时间】:2020-05-01 00:08:40
【问题描述】:
我在 Stack Overflow Flutter getter isn't specified for the class, when it is specified 上查看过这个问题。而且我仍然无法理解为什么我的班级 Practice 无法访问变量 _text,该变量是从 List 中类型为 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() 来检查按下加号按钮时当前标签是否为空。如果不是,则创建一个新标签。
【问题讨论】:
标签: flutter dart mobile-development