如何从用户的文本字段中获取数组?

时间:2018-04-26 16:44:02

标签: dart flutter

小部件构建(BuildContext context){

TextField XnumField = new TextField(
  keyboardType: TextInputType.numberWithOptions(),
  decoration: new InputDecoration(labelText: "X array"),
  onSubmitted: (String text){
      for(i = 0; i < 4; i++){
        X[i] = int.parse(text);
      }
  },
);

2 个答案:

答案 0 :(得分:0)

像...一样的东西。

import 'dart:core';

List<int> BuildIntArray(String input) {
  var outList = new List<int>();
  final _delimiter = ',';
  final _values = input.split(_delimiter);
  _values.forEach((item) {
    outList.add(int.parse(item));
    });
  return outList;
}

哪个会...

import 'package:IntegerArray/IntegerArray.dart' as IntegerArray;

main(List<String> arguments) {

  final input = "1,2,3,4,5";
  final intInputValues = IntegerArray.BuildIntArray(input);
  print (intInputValues);

  int sum = 0;
  intInputValues.forEach((item) {
    sum+=item;
  });

  print (sum);

}

......做这个......

Observatory listening on http://127.0.0.1:64499/

[1, 2, 3, 4, 5]
15

Process finished with exit code 0

我没有看到&#34; tryParse&#34;过滤掉非数字值......但是通过一些验证/错误检查,您可以添加到此...

答案 1 :(得分:0)

demo

您好, 如果你正在寻找这样的东西,你可以去。

    import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new DemoScreen(),
  ));
}

class DemoScreen extends StatefulWidget {
  @override
  _DemoScreenState createState() => new _DemoScreenState();
}

class _DemoScreenState extends State<DemoScreen> {
  List<int> _myList = new List();
  TextEditingController _myController = new TextEditingController();
  String _result = "";
  String _inputList = "";

  setSum() {
    int sum = 0;

    for (int i = 0; i < _myList.length; i++) {
      sum += _myList[i];
      if (i == 0)
        _inputList = "${_myList[i]}";
      else
        _inputList = _inputList + " + ${_myList[i]}";
    }
    _result = "$sum";
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Demo App"),
      ),
      body: new Column(
        children: <Widget>[
          new Container(
            margin: new EdgeInsets.symmetric(vertical: 10.0),
            child: new Text(
              _inputList,
              style: new TextStyle(fontSize: 40.0),
            ),
          ),
          new Container(
            margin: new EdgeInsets.symmetric(vertical: 25.0),
            child: new Text(
              _result,
              style: new TextStyle(fontSize: 70.0),
            ),
          ),
          new Container(
            margin: new EdgeInsets.symmetric(horizontal: 50.0),
            child: new TextField(
              controller: _myController,
              keyboardType: TextInputType.number,
              onSubmitted: (text) {
                setState(() {
                  _myList.add(int.parse(text));
                  setSum();
                  _myController.clear();
                });
              },
            ),
          )
        ],
      ),
    );
  }
}

希望有所帮助:)