输入框控件TextField
TextEditingController userNameController = TextEditingController();
,userNameController.text获取对应的TextField控件输入的内容。text,, number, phone, datetime, emailAddress, url
等。done、go、search、send、next
等。还需要设置onEditingComplete来指定点击改过类型的回车键的响应事件。如指定为next,方法指定跳转到密码输入框onEditingComplete: () => FocusScope.of(context).requestFocus(pwdNode)
,其中FocusNode pwdNode = FocusNode();
在密码的输入控件中指定focusNode: pwdNode
。icon: Icon(Icons.account_box)
表示在输入框左边显示系统自带的账号图标。输入框激活状态下图标也会颜色变更。prefix: Text("用户名")
。prefixText: "+86"
。过程中解决的bug:
1、有输入框,用SingleChildScrollView嵌套Column。否则弹出输入键盘时有溢出bug:bottom overflowed by X PIXELS。
2、运行安卓报错:Flutter SocketException: Failed to create server socket (OS Error: Permission denied, errno = 13), address = 127.0.0.1, port = 0。在android工程AndroidManifest.xml配置
3、AppBar中标题默认靠左显示,设置属性让标题水平居中。
centerTitle: true, //文字title居中
实现登录界面的代码如下:
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
class Login extends StatefulWidget {
@override
State createState() => LoginState();
}
class LoginState extends State {
// 用户名的控制器
final TextEditingController userNameCtrl = TextEditingController();
//密码的控制器
final TextEditingController passwordCtrl = TextEditingController();
// 光标跳转的密码输入框对象
FocusNode pwdNode = FocusNode();
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text("登录"),
centerTitle: true, //文字title居中
leading: new BackButton()),
// 有输入框,用SingleChildScrollView嵌套Column。否则弹出输入键盘时有溢出bug。bottom overflowed by X PIXELS
body: SingleChildScrollView(
child: Column(
children: [
new Padding(
padding: EdgeInsets.fromLTRB(40, 24, 40, 0),
child: TextField(
controller: userNameCtrl,
// 指定该输入框是输入手机号码的
keyboardType: TextInputType.phone,
// 回车键用途,输入下一项。还需要具体实现。
textInputAction: TextInputAction.next,
// 键盘动作按钮点击之后执行的代码:光标切换到指定的输入框
onEditingComplete: () =>
FocusScope.of(context).requestFocus(pwdNode),
maxLength: 11,
decoration: InputDecoration(
icon: Icon(Icons.account_circle),
labelText: "请输入用户名",
// helperText: "一般为手机号码", // 一般不设置
hintText: "11位手机号码",
// prefixIcon: Icon(Icons.account_circle), // 输入框线内
// prefix: Text("用户名"),
// prefixText: "+86",
),
),
),
new Padding(
padding: EdgeInsets.fromLTRB(40, 4, 40, 0),
child: TextField(
controller: passwordCtrl,
focusNode: pwdNode,
keyboardType: TextInputType.text,
// 回车键用途,完成输入
textInputAction: TextInputAction.done,
maxLength: 11,
obscureText: true,
// 密码输入,隐藏
decoration: InputDecoration(
icon: Icon(Icons.lock_outline),
labelText: "请输入密码",
// suffixIcon: Icon(Icons.visibility_off),
// suffixText: "(元)",
),
),
),
Padding(
padding: EdgeInsets.fromLTRB(40, 36, 40, 0),
child: Row(children: [
new Expanded(
child: RaisedButton(
color: Colors.blue,
onPressed: () {
doLogin();
},
child: new Text(
"登录",
style: TextStyle(
color: Colors.white,
),
),
))
]),
),
],
)));
}
void doLogin() {
if (userNameCtrl.text.isEmpty || passwordCtrl.text.isEmpty) {
Fluttertoast.showToast(msg: "请输入用户名和密码");
} else {
Fluttertoast.showToast(
msg: "用户名: ${userNameCtrl.text},密码: ${passwordCtrl.text}");
}
}
}
实现效果:
本文源码:TAG,下载源码需指定tag。