flutter - 使用从 textField 存储的数据操作字符串 - 颤振/飞镖
问题描述
我对编程很陌生,此时我几乎复制和粘贴代码,看看什么是有效的。但我相信我可能需要使用一种叫做构造函数的东西,但我不确定如何使用。
我有 3 个文本字段,它们接收数字并将它们存储在日、月和年中。
然后我想把这 3 个变量加在一起,然后将它们存储在另一个变量 pDay 中。
当我尝试这样做时,我得到了错误
错误:在初始化程序中只能访问静态成员。
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main(){
runApp(new MaterialApp(
home: new MyTextInput()
));
}
class MyTextInput extends StatefulWidget {
@override
MyTextInputState createState() => MyTextInputState();
}
class MyTextInputState extends State<MyTextInput> {
static String day = "";
static String month = "";
static String year = "";
static String pDay = day + month + year;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text("Julian Date")),
body: new Container(
child: new Center(
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
SizedBox(width: 20.0,),
new Flexible(
child: new TextField(
inputFormatters: [
LengthLimitingTextInputFormatter(2),
],
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: new InputDecoration(
contentPadding: EdgeInsets.all(10),
hintText: "DAY"
),
onSubmitted: (String str) {
setState(() {
day = str;
});
}
),
),
SizedBox(width: 20.0,),
new Flexible(
child: new TextField(
inputFormatters: [
LengthLimitingTextInputFormatter(2),
],
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: new InputDecoration(
contentPadding: EdgeInsets.all(10),
hintText: "MONTH",
),
onSubmitted: (String str) {
setState(() {
month = str;
});
}
),
),
new Text(pDay),
SizedBox(width: 20.0,),
new Flexible(
child: new TextField(
inputFormatters: [
LengthLimitingTextInputFormatter(2),
],
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: new InputDecoration(
contentPadding: EdgeInsets.all(10),
hintText: "YEAR"
),
onSubmitted: (String str) {
setState(() {
year = str;
});
}
),
),
SizedBox(width: 20.0,),
]
)
)
)
);
}
}
解决方案
更新:
void main() {
runApp(new MaterialApp(home: new MyTextInput(), theme: ThemeData.dark(),));
}
class MyTextInput extends StatefulWidget {
@override
MyTextInputState createState() => MyTextInputState();
}
class MyTextInputState extends State<MyTextInput> {
String day = "";
String month = "";
String year = "";
String pDay = "";
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text("Julian Date")),
body: new Container(
child: new Center(
child: new Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: <Widget>[
SizedBox(
width: 20.0,
),
new Flexible(
child: new TextField(
inputFormatters: [
LengthLimitingTextInputFormatter(2),
],
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: new InputDecoration(contentPadding: EdgeInsets.all(10), hintText: "DAY"),
onChanged: (String str) { // use onChanged
setState(() {
day = str;
pDay = day + month + year; // add this
});
}),
),
SizedBox(
width: 20.0,
),
new Flexible(
child: new TextField(
inputFormatters: [
LengthLimitingTextInputFormatter(2),
],
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: new InputDecoration(
contentPadding: EdgeInsets.all(10),
hintText: "MONTH",
),
onChanged: (String str) { // use onChanged
setState(() {
month = str;
pDay = day + month + year; // add this
});
}),
),
new Text(pDay),
SizedBox(
width: 20.0,
),
new Flexible(
child: new TextField(
inputFormatters: [
LengthLimitingTextInputFormatter(2),
],
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
decoration: new InputDecoration(contentPadding: EdgeInsets.all(10), hintText: "YEAR"),
onChanged: (String str) { // use onChanged
setState(() {
year = str;
pDay = day + month + year; // add this
});
}),
),
SizedBox(
width: 20.0,
),
]))));
}
}
推荐阅读
- c++ - 连接到 Internet 时由 Windows 发送的事件
- api - 在 RxJs 中获取更多数据
- php - phppresentation溢出表问题
- neo4j - 为标签/关系添加描述或评论
- wolfram-mathematica - 在 Mathematica 中为 ListStreamPlot 创建数据
- sql-server - Hangfire 重复性工作中断
- python - 终止(结束,杀死)python中的特定线程?
- windows - 调用 Microsoft Exchange 命令
- php - 在 Laravel 8 中下载数据库的 Excel 电子表格
- hadoop - 如何通过 hdfs 完成 tez 作业