【问题标题】:How to pass data from TextFormField to List in flutter?如何在颤动中将数据从 TextFormField 传递到 List?
【发布时间】:2018-06-13 09:42:10
【问题描述】:

我有以下工作代码:

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。但我想要一个列表中的结果,该列表与我接下来展示的其他类一起定义。我想显示变量w1 的值,其中文本1.1 是(在List&lt;Entry&gt; 内)。接下来我有完整的代码,但我不知道如何传递变量。如果我写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);
  }
}

【问题讨论】:

  • 这里的代码太多了。你能提供一个minimal功能的例子吗?
  • @RémiRousselet 我已经编辑了帖子,并在不破坏它的情况下尽可能地最小化了代码。请注意,第二个代码是第一个带有可扩展列表的代码

标签: class dart flutter


【解决方案1】:

试试这个,

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

【讨论】:

    猜你喜欢
    • 2020-02-16
    • 2022-06-10
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 2021-11-02
    相关资源
    最近更新 更多