• flutter 表单form 使用


    https://www.jianshu.com/p/9bec3d14df7f

    大致用法如下

    import 'package:flutter/material.dart';
    
    class Login extends StatefulWidget {
      @override
      _LoginState createState() => _LoginState();
    }
    
    class _LoginState extends State<Login> {
      GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
      bool loading = false;
      String _username = '';
      String _password = '';
    
      // 验证数据 登录
      void _forSubmitted() async {
        var _form = _formKey.currentState;
        if (_form.validate()) {
          _form.save();
          print(_username);
          print(_password);
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('登录'),
          ),
          body: GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () {
              // 触摸收起键盘
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: ListView(
              children: <Widget>[
                Form(
                  key: _formKey,
                  child: Container(
                    margin: EdgeInsets.fromLTRB(10, 10, 10, 10),
                    child: Column(children: <Widget>[
                      Container(
                        margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                        child: TextFormField(
                          cursorColor: Theme.of(context).primaryColor,
                          decoration: InputDecoration(
                            hintText: '请输入账号',
                            prefixIcon: Icon(Icons.person),
                          ),
                          validator: (val) {
                            return val.length <= 0 ? "请输入账号" : null;
                          },
                          onSaved: (val) {
                            _username = val;
                          },
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                        child: TextFormField(
                          cursorColor: Theme.of(context).primaryColor,
                          obscureText: true,
                          decoration: InputDecoration(
                            hintText: '请输入密码',
                            prefixIcon: Icon(Icons.lock),
                          ),
                          validator: (val) {
                            if (val.length <= 0) {
                              return '请输入密码';
                            } else {
                              return val.length < 6 ? "密码长度错误" : null;
                            }
                          },
                          onSaved: (val) {
                            _password = val;
                          },
                        ),
                      ),
                      Row(
                        children: <Widget>[
                          Expanded(
                            child: Container(
                              height: 40,
                              margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                              child: RaisedButton(
                                onPressed: this._forSubmitted,
                                child: Text(
                                  '登 录',
                                  style: TextStyle(
                                    fontSize: 20,
                                  ),
                                ),
                                textColor: Colors.white,
                                color: Theme.of(context).primaryColor,
                              ),
                            ),
                          )
                        ],
                      )
                    ]),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
  • 相关阅读:
    7.Perfect Number
    6.Hamming Distance
    5.Palindrome Number
    4.String to Integer (atoi)
    3.Reverse Integer
    [HP SIM] Systems Insight Manager stopped working, sqlserver error code 0x80090302。
    [HP SIM] Systems Insight Manager 不能正常工作,数据库错误0x80090302。
    [Outlook] 用powershell做outlook中的delegate.
    [Outlook] Use powershell to do delegates like outlook.
    [Outlook] profile在注册表里的秘密。
  • 原文地址:https://www.cnblogs.com/bruce-gou/p/13434485.html
Copyright © 2020-2023  润新知