android表单设计,Flutter质感设计之表单输入

本文介绍了Flutter中的FormField和TextField控件在表单处理中的应用。FormField用于维护表单字段的状态,而TextField是输入控件,通常在FormField内使用。文章展示了如何使用这两个控件创建表单,包括保存、重置和验证用户输入,并通过示例代码详细解释了其工作原理。
摘要由CSDN通过智能技术生成

FormField控件是单一表单字段,这个控件维护表单字段的当前状态,以便更新和验证错误能在UI中可见。TextField控件就是在FormField中包装了一个Input控件(后面的文章讲解),FormField维护输入的当前值,使您不需要自己管理它,更容易一次保存,重置或验证多个字段。

import 'package:flutter/material.dart';

class MyApp extends StatefulWidget {

@override

_MyApp createState() => new _MyApp();

}

class _MyApp extends State {

String _lastName;

String _firstName;

GlobalKey _formKey = new GlobalKey();

void _showMessage(String name) {

showDialog(

context: context,

child: new AlertDialog(

content: new Text(name),

actions: [

new FlatButton(

onPressed: () {

Navigator.pop(context);

},

child: new Text('确定')

)

]

)

);

}

@override

Widget build(BuildContext context) {

return new Scaffold(

appBar: new AppBar(

title: new Text('表单输入')

),

// Form:用于将多个表单控件组合在一起的容器

body: new Form(

key: _formKey,

child: new Column(

children: [

// TextFieldd:包含输入的表单控件,每个表单字段都应该在FormField控件中

new TextField(

labelText: '姓氏',

// onSaved:当通过Form.save()保存表单时调用的方法

onSaved: (InputValue value) {

_lastName = value.text;

}

),

new TextField(

labelText: '名字',

onSaved: (InputValue value) {

_firstName = value.text;

}

),

new Row(

children: [

new RaisedButton(

child: new Text('重置'),

onPressed: () {

// reset():将此Form下的每个TextField重置为初始状态

_formKey.currentState.reset();

_showMessage('姓名信息已经重置');

}

),

new RaisedButton(

child: new Text('提交'),

onPressed: () {

// save():保存Form下的每个TextField

_formKey.currentState.save();

_showMessage('你的姓名是'+_lastName+_firstName);

}

)

]

)

]

)

)

);

}

}

void main() {

runApp(new MaterialApp(

title: 'Flutter Demo',

home: new MyApp()

));

}

6e9eedec06449e5af2ac383b3b76c90a.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值