TextFormField数据处理

重点:TextFormField这个Widget是由TextField封装而来,继承了TextField的特性:
数据传递依靠:
GlobalKey<FormState>(),
RegisterKey.currentState.save();
加上Form里面有key选项,这三项复合起来,可以达到数据传递的目的。具体请看如下示例:


import 'package:flutter/material.dart';


void main()=>runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: HomePage(),
),
);
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
final RegisterKey=GlobalKey<FormState>();
String username, password;

void submitForm(){
RegisterKey.currentState.save();
debugPrint('username:$username');
debugPrint('password:$password');
}

@override
Widget build(BuildContext context) {
return Form(
key:RegisterKey,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: TextFormField(
decoration: InputDecoration(
labelText: 'Please input username',
contentPadding: EdgeInsets.all(20),
),
onSaved: (value){
username=value;
},
),
),
Container(
padding: EdgeInsets.all(10.0),
child: TextFormField(
obscureText: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(20),
labelText: 'Please input password',
),
onSaved: (value){password=value;},
),
),
SizedBox(height: 10,),
Container(
padding: EdgeInsets.all(20.0),
width: double.infinity,
child: RaisedButton(
onPressed:submitForm,
color: Colors.lightGreen,
child: Text('点我哦'),
),
),
],
),
);
}
}

转载于:https://www.cnblogs.com/braveheart007/p/10738170.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值