Input控件是质感设计的文本输入控件,它在用户每次输入时都会调用 d回调时,都会更新字段值,还可以实时的对用户输入进行响应。
import 'package:flutter/material.dart';
class MyApp extends StatefulWidget {
@override
_MyApp createState() => new _MyApp();
}
class _MyApp extends State<MyApp> {
// InputValue:文本输入字段的配置信息
InputValue _phoneValue = const InputValue();
InputValue _passwordValue = const InputValue();
void _showMessage(String name) {
showDialog<Null>(
context: context,
child: new AlertDialog(
content: new Text(name),
actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: new Text('确定')
)
]
)
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
: new Text('直接输入')
),
body: new Column(
children: <Widget> [
new Input(
// value:文本输入字段的当前状态
value: _phoneValue,
// keyboardType:用于编辑文本的键盘类型
keyboardType: TextInputType.number,
// icon:在输入字段旁边显示的图标
icon: new Icon(Icons.account_circle),
// labelText:显示在输入字段上方的文本
labelText: '手机',
// hintText:要在输入字段中内嵌显示的文本
hintText: '请输入手机号码',
// d:正在编辑的文本更改时调用
d: (InputValue value) {
setState((){
_phoneValue = value;
});
}
),
new Input(
value: _passwordValue,
// obscureText:是否隐藏正在编辑的文本
obscureText: true,
labelText: '密码',
d: (InputValue value) {
setState((){
_passwordValue = value;
});
},
// ted:当用户在键盘上点击完成编辑时调用
ted: (InputValue value) {
if(value.text.length<6){
_showMessage('密码不少于6位');
}
}
),
new RaisedButton(
child: new Text('提交'),
onPressed: () {
_showMessage(_phoneValue.text+'/'+_passwordValue.text);
}
)
]
)
);
}
}
void main() {
runApp(new MaterialApp(
: 'Flutter Demo',
home: new MyApp()
));
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
下一篇 :
从Y = X到构建完整的人工神经网络
-
开发者云阿拉丁神灯计划参与规则
2026-05-18栏目: 教程
-
什么?还没听说过Prometheus,或许你需要了解这些知识点
2026-05-18栏目: 教程
-
Knative 实战:三步走!基于 Knative Serverless 技术实现一个短网址服务
2026-05-18栏目: 教程
-
开篇 | 揭秘 Flink 1.9 新架构,Blink Planner 你会用了吗? | 9月20号栖夜读
2026-05-18栏目: 教程
-
精彩回放|《安全说道》节目合集
2026-05-18栏目: 教程
