Flutter Form正确使用方法【可正确获取提交的表单数据】
import 'package:flutter/material.dart'; void main() => runApp(new HomePage()); class HomePage extends StatefulWidget { @override _HomePageState createState() => new _HomePageState(); } class _HomePageState extends State<HomePage> { GlobalKey<FormState> _formKey = new GlobalKey<FormState>(); String _name; String _password; void _forSubmitted() { var _form = _formKey.currentState; if (_form.validate()) { _form.save(); print(_name); print(_password); } } @override Widget build(BuildContext context) { // TODO: implement build return new MaterialApp( title: 'Flutter data', home: new Scaffold( appBar: new AppBar( title: new Text('Flutter Form正确使用方法'), ), floatingActionButton: new FloatingActionButton( onPressed: _forSubmitted, child: new Text('提交'), ), body: new Container( padding: const EdgeInsets.all(16.0), child: new Form( key: _formKey, child: new Column( children: <Widget>[ new TextFormField( decoration: new InputDecoration( labelText: 'Your Name', ), onSaved: (val) { _name = val; }, ), new TextFormField( decoration: new InputDecoration( labelText: 'Password', ), obscureText: true, validator: (val) { return val.length < 4 ? "密码长度错误" : null; }, onSaved: (val) { _password = val; }, ), Padding( padding: const EdgeInsets.only(top: 28.0), child: Row( children: <Widget>[ Expanded( child: RaisedButton( padding: EdgeInsets.all(15.0), child: Text('登录'), color: Colors.blue, textColor: Colors.white, onPressed:this._forSubmitted, ) ) ] ), ) ], ), ), ), ), ); } }