gpt4 book ai didi

flutter - 没有为 Flutter 中的类 TagColumn 定义 Getter _text

转载 作者:行者123 更新时间:2023-12-03 03:35:11 26 4
gpt4 key购买 nike

我在 Stack Overflow Flutter getter isn't specified for the class, when it is specified 上查看过这个问题.我仍然无法理解为什么我的类(class) 练习 无权访问变量 _文本 中的元素访问类型为 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() , 在按下加号按钮时检查当前标签是否为空。如果不是,则创建一个新标签。

enter image description here

最佳答案

dart 将以下划线开头的变量视为私有(private)变量(因为 dart 中没有 private 关键字),因此为了解决您的问题,您需要删除文本变量之前的 _(下划线)。

这是怎么回事

1- 将 _text 变量移动到 TagColumn类 insted 的 State 类

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/

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