Flutter项目开发笔记一:Form表单使用

376 阅读1分钟

     Flutter基础控件的使用往往参照官网或博客就能实现效果,但如果应用到项目中就会发现将各个控件组合起来潜伏的问题就接踵而来。于此笔记记录并做分享。无它,但手熟尔!争做一个合格的打工人。

一、实现的效果

二、代码示例

import 'package:flutter/material.dart';
// 适应屏幕插件
import 'package:flutter_screenutil/flutter_screenutil.dart';
class LoginFormWidget extends StatefulWidget {  
        LoginFormWidget({Key key}) : super(key: key); 
 @override  
_LoginFormWidgetState createState() => _LoginFormWidgetState();
}
class _LoginFormWidgetState extends State<LoginFormWidget> {  
String _name;  
String _pwd;  
//全局key用来获取From表单组件  
GlobalKey<FormState> _formKey = GlobalKey<FormState>();  
@override  
Widget build(BuildContext context) {    
ScreenUtil.init(context,designSize: Size(750, 1334), allowFontScaling: false);    
return Scaffold(      
body: GestureDetector(        
behavior: HitTestBehavior.translucent,        
onTap: () {          
// 触摸收起键盘          
FocusScope.of(context).requestFocus(FocusNode());        
},        
child: ListView(          
children: [ _loginForm() ],       
 ),      
),    
);  
}  
// 登录表单  
Widget _loginForm() {    
return Container(      
child: Form(        
key: _formKey,        
child: Column(          
children: <Widget>[            
Container( margin:EdgeInsets.only(left: 22, right: 22, top: 10, bottom: 10),
 child: TextFormField( style: TextStyle(fontSize: 20), 
decoration: InputDecoration( 
hintText: '输入用户名', 
 prefixIcon: Icon(                      
Icons.person,                      
color: Colors.blue,                      
size: 26,                    
),                    
enabledBorder: UnderlineInputBorder( 
borderSide: BorderSide(color: Colors.blue),                   
),                  
),                  
onSaved: (value) {                   
 _name = value;                  
},                  
validator: (String value) {
return value.length >= 6 ? null : '账号最少6个字符';                  
},                
)),            
Container(              
margin: EdgeInsets.only(left: 22, right: 22),              
child: TextFormField(                
obscureText: true,                
style: TextStyle(color: Colors.blue, fontSize: 20),                
decoration: InputDecoration(                  
hintText: '输入密码',                  
prefixIcon: Icon(                    
Icons.lock_outline,                    
color: Colors.blue,                    
size: 26,                  
),                  
enabledBorder: UnderlineInputBorder(                    
borderSide: BorderSide(color: Colors.blue),                  
),                
),                
onSaved: (value) {

_pwd = value;                
},                
validator: (String value) {

return value.length < 6 ? '密码长度不够六位' : null;               
 },              
),            
),            
Container(              
margin: EdgeInsets.only(top: 30),             
 width: ScreenUtil().setWidth(660),              
height: ScreenUtil().setHeight(84),             
 child: RaisedButton(               
 // 设置圆角     
           shape: RoundedRectangleBorder(            
      borderRadius: BorderRadius.circular(20),          
      ),                
textColor: Colors.white,                
child: Text(               
   '登录',                  
style: TextStyle(fontSize: 18),                ),                
onPressed: () {                  
var loginForm = _formKey.currentState;                  
print(loginForm);                  
if (loginForm.validate()) {                    
loginForm.save();                    
print("username:" + _name + 'password:' + _pwd);                 
 }                },              ),            )          ],        ),      ),    ); 
 }}

三、说明

1、使用手势GestureDetector实现点击空白区域收起键盘

2、使用ListView进行布局,实现键盘遮挡输入框时,输入框可进行上移

3、注意Form控件的最顶级节点控件一定要是Scaffold控件