TextFormField数据处理

Posted braveheart007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(‘点我哦‘),
),
),
],
),
);
}
}

以上是关于TextFormField数据处理的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:添加动态 TextFormField 以将数据上传到列表

“非活动InputConnection上的beginBatchEdit”,在编辑TextFormField时

Flutter:如何以编程方式添加 TextField 或 TextFormField

TextFormField 在验证时不显示错误

Flutter组件TextFormField详解

在 Flutter 中的 TextFormField 中,用户输入一个 url 或网站链接以将其保存到 firebase 数据库,并且应该使用浏览器打开该链接