zoukankan      html  css  js  c++  java
  • 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,
                              ),
                            ),
                          )
                        ],
                      )
                    ]),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
  • 相关阅读:
    golang学习笔记 ---接口
    golang学习笔记 --类与方法
    golang学习笔记--面向对象编程
    golang学习笔记---错误处理
    golang学习笔记---defer[延迟函数]
    golang学习笔记--闭包
    golang学习笔记---函数
    SSD技术扫盲之:什么是NVMe? NVMe SSD有什么特点?
    云原生存储系列文章:云原生应用的基石
    发财树的养殖方法
  • 原文地址:https://www.cnblogs.com/bruce-gou/p/13434485.html
Copyright © 2011-2022 走看看