【问题标题】:How to Get values from each line in text field?如何从文本字段中的每一行获取值?
【发布时间】:2019-10-18 19:42:57
【问题描述】:

我有输入文本字段: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}'),
              ),
            )),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

  • 你为什么不使用两个文本字段?
  • @PraneethDhanushkaFernando,我尝试从每一行制作循环小部件
  • 你能再解释一下吗。所以想象提交的文本是,Foo /line1 Bar /line2 你想从这两行中做什么

标签: flutter line textfield


【解决方案1】:

据我了解这里是我的解决方案

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

你想要这样的东西

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


【讨论】:

  • 欢迎您.. 很高兴为您提供帮助。也可以标记我的回答有用
  • 啊,我误会了。我会删除我的答案
猜你喜欢
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
  • 2021-08-17
相关资源
最近更新 更多