Как получить значения из каждой строки в текстовом поле? - PullRequest
0 голосов
/ 03 июня 2019

У меня есть текстовое поле с вводом: Foo / line1 Bar / line2

, когда я нажимаю кнопку submit, я создаю виджеты 'Name:' со значением, которое было отправлено (name = foo и name = 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 Ответ

1 голос
/ 03 июня 2019

Согласно тому, что я понял, вот мое решение

Итак, я предполагаю, что у вас есть многоканальное текстовое поле

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


...