• 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,
                            )
                          )
                        ]
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
  • 相关阅读:
    2019.9.21 Tomcat基于端口的虚拟主机
    shell脚本作业
    DNS原理及其解析过程
    用户管理系统脚本
    pxe批量装机
    磁盘分区挂载脚本
    安装apache脚本
    linux远程拷贝命令及not a regular file 解决方案
    卸载虚拟网卡的方法
    watch的用法
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/11700726.html
Copyright © 2020-2023  润新知