首页 > 解决方案 > 使用从 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,),

                    ]
                )
            )
        )
    );
  }
}

标签: flutterdartflutter-layout

解决方案


更新:

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,
          ),
        ]))));
  }
}

推荐阅读