Flutter 表单组件 登录界面

import 'package:flutter/material.dart';

void main() => runApp(new Login());

class Login extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new LoginState();
  }
}

class LoginState extends State<Login> {
  //全局key 用来获取表单控件
  GlobalKey<FormState> loginKey = new GlobalKey<FormState>();
  String username;
  String password;

  void login() {
    var loginfrom = loginKey.currentState;
    if (loginfrom.validate()) {
      loginfrom.save();
      print("username" + username + "password" + password);
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      title: "form表单实例",
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text("form表单实例"),
        ),
        body: new Column(
          children: <Widget>[
            new Container(
              padding: const EdgeInsets.all(16.0),
              child: new Form(
                key: loginKey,
                child: new Column(
                  children: <Widget>[
                    new TextFormField(
                      keyboardType: TextInputType.phone, //限制输入类型
                      decoration: new InputDecoration(labelText: "请输入用户名:"),
                      onSaved: (value) {
                        username = value;
                      },
                      onFieldSubmitted: (value) {},
                    ),
                    new TextFormField(
                      decoration: new InputDecoration(labelText: "请输入密码:"),
                      obscureText: true, //是否显示字符
                      autovalidate: false,  //是否自动验证
                      validator:(value){
                        return value.length<6 ? "密码长度不够6位":null;
                      } ,
                      onSaved: (v) {
                        password = v;
                      },
                    ),
                    new Container(
                      child:   new SizedBox(
                        width:340 ,
                        height: 42,
                        child: new RaisedButton(onPressed:() {
                          login();
                        },
                          child: new Text("登录"
                            ,style: new TextStyle(
                                fontSize: 18.0
                            ),
                          ),
                        ),
                      ) ,
                      margin: EdgeInsets.only(top: 50), //设置margin 距离顶部
                    )
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

注意
  GlobalKey<FormState> loginKey = new GlobalKey<FormState>();

这个key要在From中key位置的添加

     child: new Form(
                key: loginKey,
                child: new Column

之前没有添加报了如下错误:

I/flutter (28191): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter (28191): The method 'validate' was called on null.
I/flutter (28191): Receiver: null
I/flutter (28191): Tried calling: validate()
I/flutter (28191): 
I/flutter (28191): When the exception was thrown, this was the stack:
I/flutter (28191): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
I/flutter (28191): #1      LoginState.login (package:flutter_app/login.dart:21:19)
I/flutter (28191): #2      LoginState.build.<anonymous closure> (package:flutter_app/login.dart:67:27)
I/flutter (28191): #3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:511:14)

参考:Flutter 技术入门与实战

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值