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

 

posted on 2020-08-04 17:14  浅唱年华1920  阅读(640)  评论(0编辑  收藏  举报