如何从文本字段的每一行获取值?

时间:2019-06-03 09:07:08

标签: flutter line textfield

我输入的文本字段为:Foo / line1                               栏/ line2

当我单击按钮提交时,我使用提交的值(名称= foo和名称= bar)创建小部件“名称:”,如何创建文本字段并从中获取值?我必须首先创建一个列表视图构建器吗?

任何人都可以举个例子

  String txt = "";
  TextEditingController controllerTxt = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text('Create'),
        actions: <Widget>[
          FlatButton(
            child: Text('Submit'),
            textColor: Colors.white,
            onPressed: () {
              Navigator.pushNamed(context, '/ResultPage',
                  arguments: (controllerTxt.text));
            },
          ),
        ],
      ),
      body: new ListView(
        children: <Widget>[
          new Container(
            child: new Column(
              children: <Widget>[
                  new TextField(
                    controller: controllerTxt,
                    maxLines: 25,
                  ),
              ],
            ),
          ),
        ],
      ),
    );
  }

class _ResultPageState extends State<ResultPage> {
  String result;

  @override
  Widget build(BuildContext context) {
    RouteSettings settings = ModalRoute.of(context).settings;
    result = settings.arguments;

    return new Scaffold(
      appBar: new AppBar(
        title: Text('Create'),
        actions: <Widget>[
        ],
      ),
      body: new Container(
        padding: EdgeInsets.all(10.0),
        child: new Row(
          children: <Widget>[
            new Text('Name : '),
            Expanded(
              child: new TextField(
              enabled: false,
              decoration: new InputDecoration(
                border: new OutlineInputBorder(
                    borderSide: new BorderSide(color: Colors.black)),
                labelText: ('${result}'),
              ),
            )),
          ],
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

根据我的理解,这是我的解决方案

所以我假设您有多行文本字段

enter image description here

您想要这样的东西

enter image description here

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  TextEditingController controllerTxt = new TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  String txt = "";

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text('Create'),
        actions: <Widget>[
          FlatButton(
            child: Text('Submit'),
            textColor: Colors.white,
            onPressed: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => ResultPage(
                            result: controllerTxt.text,
                          )));
            },
          ),
        ],
      ),
      body: new ListView(
        children: <Widget>[
          new Container(
            child: new Column(
              children: <Widget>[
                new TextField(
                  controller: controllerTxt,
                  maxLines: 25,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class ResultPage extends StatefulWidget {
  final String result;

  const ResultPage({Key key, this.result}) : super(key: key);

  @override
  _ResultPageState createState() => _ResultPageState();
}

class _ResultPageState extends State<ResultPage> {
  List<String> lines;

  @override
  void initState() {
    super.initState();

    lines = widget.result.split('\n');
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: Text('Create'),
          actions: <Widget>[],
        ),
        body: Column(
          children: lines
              .map((line) => Container(
                    padding: EdgeInsets.all(10.0),
                    child: new Row(
                      children: <Widget>[
                        new Text('Name : '),
                        Expanded(
                            child: new TextField(
                          enabled: false,
                          decoration: new InputDecoration(
                            border: new OutlineInputBorder(
                                borderSide:
                                    new BorderSide(color: Colors.black)),
                            labelText: (line),
                          ),
                        )),
                      ],
                    ),
                  ))
              .toList(),
        ));
  }
}