FormField控件是單一表單字段,這個(gè)控件維護(hù)表單字段的當(dāng)前狀態(tài),以便更新和驗(yàn)證錯(cuò)誤能在UI中可見(jiàn)。TextField控件就是在FormField中包裝了一個(gè)Input控件(后面的文章講解),F(xiàn)ormField維護(hù)輸入的當(dāng)前值,使您不需要自己管理它,更容易一次保存,重置或驗(yàn)證多個(gè)字段。

import 'package:flutter/material.dart';
class MyApp extends StatefulWidget {
@override
_MyApp createState() => new _MyApp();
}
class _MyApp extends State<MyApp> {
String _lastName;
String _firstName;
GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
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(
title: new Text('表單輸入')
),
// Form:用于將多個(gè)表單控件組合在一起的容器
body: new Form(
key: _formKey,
child: new Column(
children: <Widget> [
// TextFieldd:包含輸入的表單控件,每個(gè)表單字段都應(yīng)該在FormField控件中
new TextField(
labelText: '姓氏',
// onSaved:當(dāng)通過(guò)Form.save()保存表單時(shí)調(diào)用的方法
onSaved: (InputValue value) {
_lastName = value.text;
}
),
new TextField(
labelText: '名字',
onSaved: (InputValue value) {
_firstName = value.text;
}
),
new Row(
children: <Widget> [
new RaisedButton(
child: new Text('重置'),
onPressed: () {
// reset():將此Form下的每個(gè)TextField重置為初始狀態(tài)
_formKey.currentState.reset();
_showMessage('姓名信息已經(jīng)重置');
}
),
new RaisedButton(
child: new Text('提交'),
onPressed: () {
// save():保存Form下的每個(gè)TextField
_formKey.currentState.save();
_showMessage('你的姓名是'+_lastName+_firstName);
}
)
]
)
]
)
)
);
}
}
void main() {
runApp(new MaterialApp(
title: 'Flutter Demo',
home: new MyApp()
));
}
新聞標(biāo)題:Flutter質(zhì)感設(shè)計(jì)之表單輸入-創(chuàng)新互聯(lián)
鏈接地址:http://chinadenli.net/article36/cocppg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、云服務(wù)器、網(wǎng)站維護(hù)、網(wǎng)頁(yè)設(shè)計(jì)公司、全網(wǎng)營(yíng)銷(xiāo)推廣、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容