如何在flutter中将数据从TextFormField传递给List?

时间:2018-06-13 09:42:10

标签: class dart flutter

我有以下工作代码:

class Submit extends StatefulWidget {
  @override
  _SubmitState createState() => new _SubmitState();
}

class _SubmitState extends State<Submit> {
  final scaffoldKey = new GlobalKey<ScaffoldState>();
  final formKey = new GlobalKey<FormState>();
  final myController = new TextEditingController();
  double _w;

  @override
  void dispose() {
    // Clean up the controller when the Widget is removed from the Widget tree
    myController.dispose();
    super.dispose();
  }

  void _submit() {
    final form = formKey.currentState;

    if (form.validate()) {
      form.save();
      _performSubmit();
    }
  }

  void _performSubmit() {

    final double data = double.parse(myController.text)*(2);
    var route = new MaterialPageRoute(builder: (BuildContext context) =>
    new NextPage(w1: data.toStringAsFixed(3)),
    );

    Navigator.of(context).push(route);
  }

  @override
  Widget build(BuildContext context) {

    void _restart() {
      Navigator.of(context).push(new MaterialPageRoute(
          builder: (BuildContext context) => new HomePage()));
    }

    return new Scaffold(
      key: scaffoldKey,
      appBar: new AppBar(
        actions: <Widget>[
        ],
        title: new Text('Next Page'),
      ),
      body:  new Form(
          key: formKey,
          child: new Column(
            children: [
              new TextFormField(
                controller: myController,
                keyboardType: TextInputType.numberWithOptions(decimal: true),
                onSaved: (val) => _w = double.parse(val),
              ),
              new RaisedButton(
                onPressed: _submit,
                child: new Text(
                  'Next Page',
                ),
              )
            ],
          ),
        ),
    );
  }
}



class NextPage extends StatefulWidget {

  final String w1;

  NextPage({Key key, this.w1}) : super (key: key);

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


class _NextPageState extends State<NextPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
          title: new Text("Second page")
      ),
      body: new Text("${widget.w1}")
    );
  }
}

这里我传递一个变量乘以2(称为w1),然后我在NextScreen的下一个屏幕(Text)上显示它。但是我希望结果在List上,这是我在下面展示的其他类定义的。我想显示文本w1所在的变量1.1的值(在List<Entry>内)。我接下来有完整的代码,但我不知道如何传递变量。如果我写new Entry('${widget.w1}',),,我会undefined name widget

我怎样才能将变量传递给该列表?

class Submit extends StatefulWidget {
  @override
  _SubmitState createState() => new _SubmitState();
}

class _SubmitState extends State<Submit> {
  final scaffoldKey = new GlobalKey<ScaffoldState>();
  final formKey = new GlobalKey<FormState>();
  final myController = new TextEditingController();
  double _w;

  @override
  void dispose() {
    // Clean up the controller when the Widget is removed from the Widget tree
    myController.dispose();
    super.dispose();
  }

  void _submit() {
    final form = formKey.currentState;

    if (form.validate()) {
      form.save();
      _performSubmit();
    }
  }

  void _performSubmit() {

    final double data = double.parse(myController.text)*(2);
    var route = new MaterialPageRoute(builder: (BuildContext context) =>
    new NextPage(w1: data.toStringAsFixed(3)),
    );

    Navigator.of(context).push(route);
  }

  @override
  Widget build(BuildContext context) {

    void _restart() {
      Navigator.of(context).push(new MaterialPageRoute(
          builder: (BuildContext context) => new HomePage()));
    }

    return new Scaffold(
      key: scaffoldKey,
      appBar: new AppBar(
        actions: <Widget>[
        ],
        title: new Text('Next Page'),
      ),
      body:  new Form(
          key: formKey,
          child: new Column(
            children: [
              new TextFormField(
                controller: myController,
                keyboardType: TextInputType.numberWithOptions(decimal: true),
                onSaved: (val) => _w = double.parse(val),
              ),
              new RaisedButton(
                onPressed: _submit,
                child: new Text(
                  'Next Page',
                ),
              )
            ],
          ),
        ),
    );
  }
}



class NextPage extends StatefulWidget {

  final String w1;

  NextPage({Key key, this.w1}) : super (key: key);

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


class _NextPageState extends State<NextPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
          title: new Text("Second page")
      ),
      body: new ListView.builder(
        itemBuilder: (BuildContext context, int index) =>
        new EntryItem(data[index]),
        itemCount: data.length,
      ),
    );
  }
}


// One entry in the multilevel list displayed by this app.
class Entry {
  Entry(this.title, [this.children = const <Entry>[]]);

  final String title;
  final List<Entry> children;
}

// The entire multilevel list displayed by this app.

List<Entry> data = <Entry>[
  new Entry(
    'First',
    <Entry>[
      new Entry(
        '1.1',
      ),
    ],
  ),
];

// Displays one Entry. If the entry has children then it's displayed
// with an ExpansionTile.
class EntryItem extends StatelessWidget {
  const EntryItem(this.entry);

  final Entry entry;

  Widget _buildTiles(Entry root) {
    if (root.children.isEmpty)
      return new ListTile(
          title: new Text(root.title));

    return new ExpansionTile(
      key: new PageStorageKey<Entry>(root),
      title: new Text(root.title,),
      children: root.children.map(_buildTiles).toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildTiles(entry);
  }
}

1 个答案:

答案 0 :(得分:0)

试试这个,

class Submit extends StatefulWidget {
  @override
  _SubmitState createState() => new _SubmitState();
}

class _SubmitState extends State<Submit> {
  final scaffoldKey = new GlobalKey<ScaffoldState>();
  final formKey = new GlobalKey<FormState>();
  final myController = new TextEditingController();
  double _w;

  @override
  void dispose() {
    // Clean up the controller when the Widget is removed from the Widget tree
    myController.dispose();
    super.dispose();
  }

  void _submit() {
    final form = formKey.currentState;

    if (form.validate()) {
      form.save();
      _performSubmit();
    }
  }

  void _performSubmit() {
    final double data = double.parse(myController.text) * (2);
    var route = new MaterialPageRoute(
      builder: (BuildContext context) => new NextPage(
            w1: new Entry(
              'First',
              <Entry>[new Entry('$data')],
            ),
          ),
    );

    Navigator.of(context).push(route);
  }

  @override
  Widget build(BuildContext context) {
    void _restart() {
//      Navigator.of(context).push(new MaterialPageRoute(
//          builder: (BuildContext context) => new HomePage()));
    }

    return new Scaffold(
      key: scaffoldKey,
      appBar: new AppBar(
        actions: <Widget>[],
        title: new Text('Next Page'),
      ),
      body: new Form(
        key: formKey,
        child: new Column(
          children: [
            new TextFormField(
              controller: myController,
              keyboardType: TextInputType.numberWithOptions(decimal: true),
              onSaved: (val) => _w = double.parse(val),
            ),
            new RaisedButton(
              onPressed: _submit,
              child: new Text(
                'Next Page',
              ),
            )
          ],
        ),
      ),
    );
  }
}

class NextPage extends StatefulWidget {
  final Entry w1;

  NextPage({Key key, this.w1}) : super(key: key);

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

class _NextPageState extends State<NextPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(title: new Text("Second page")),
        body: new EntryItem(widget.w1),
    );
  }
}

class Entry {
  Entry(this.title, [this.children = const <Entry>[]]);

  final String title;
  final List<Entry> children;
}

class EntryItem extends StatelessWidget {
  const EntryItem(this.entry);

  final Entry entry;

  Widget _buildTiles(Entry root) {
    if (root.children.isEmpty)
      return new ListTile(
          title: new Text(root.title));

    return new ExpansionTile(
      key: new PageStorageKey<Entry>(root),
      title: new Text(root.title,),
      children: root.children.map(_buildTiles).toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildTiles(entry);
  }
}