Flutter Form表单

目录

参数详解

代码示例

效果图

完整代码


参数详解

Flutter中的Form是一个容器,里面包含一个或多个TextFormField。TextFormField是表单中使用的Input输入框。

TextFormField的属性基本与TextField 相同。

属性作用
onSaved监听输入变化
  

代码示例

body: Container(
    padding: EdgeInsets.all(20),
    child: Form(
      key: _formKey,//绑定状态属性
      child: Column(
	children: <Widget>[
	  TextFormField(
	    onSaved: (val) {
	      _name = 'Name: '+val;
	    },
	    decoration: InputDecoration(labelText: 'Name',),
	  ),
	  TextFormField(
	    onSaved: (val) {
	      _surname = 'Surname:'+val;
	    },
	    decoration: InputDecoration(labelText: 'Surname',),
	  ),
	  TextFormField(
	    onSaved: (val) {
	      _telephone = 'Telephone:'+val;
	    },
	    keyboardType: TextInputType.number,
	    decoration: InputDecoration(labelText: 'Telephone',),
	  ),
	  TextFormField(
	    onSaved: (val) {
	      _mobile = 'Mobile:'+val;
	    },
	    keyboardType: TextInputType.number,
	    decoration: InputDecoration(labelText: 'Mobile',),
	  ),
	  TextFormField(
	    onSaved: (val) {
	      _email = 'E-Mail:'+val;
	    },
	    keyboardType: TextInputType.emailAddress,
	    decoration: InputDecoration(labelText: 'E-Mail',),
	  ),
	  TextFormField(
	    onSaved: (val) {
	      _website = 'Website:'+val;
	    },
	    keyboardType: TextInputType.url,
	    decoration: InputDecoration(labelText: 'Website',),
	  ),

	  Text('',style: TextStyle(height: 2,),),
	  Text('$_name'),
	  Text('$_surname'),
	  Text('$_telephone'),
	  Text('$_mobile'),
	  Text('$_email'),
	  Text('$_website'),
	],
      ),
    ),
  ),

 

效果图

 

                   

完整代码

查看完整代码

 

Flutter动态表单实现的一般步骤如下: 1. 定义表单字段数据模型:定义一个类来表示每个表单字段,包括字段类型、名称、值、是否必填等属性。 2. 构建表单UI:使用Flutter提供的表单控件,如TextFormField、Checkbox、Radio等来构建表单的UI。 3. 根据字段数据模型动态生成表单控件:根据表单字段数据模型动态生成相应的表单控件,可以使用Flutter的Widget库中的工厂方法来实现。 4. 收集表单数据:根据表单字段数据模型收集用户填写的表单数据,并进行校验处理。 5. 提交表单数据:将收集到的表单数据提交到服务器进行处理。 下面是一个简单的Flutter动态表单实现的示例代码: ```dart class FormField { final String label; final String type; final bool required; String value; FormField({ required this.label, required this.type, this.required = false, this.value = '', }); } class DynamicFormScreen extends StatefulWidget { @override _DynamicFormScreenState createState() => _DynamicFormScreenState(); } class _DynamicFormScreenState extends State<DynamicFormScreen> { final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); List<FormField> _fields = [ FormField(label: 'Name', type: 'text', required: true), FormField(label: 'Email', type: 'email', required: true), FormField(label: 'Phone', type: 'tel', required: true), FormField(label: 'Message', type: 'textarea', required: false), ]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Dynamic Form'), ), body: Form( key: _formKey, child: ListView.builder( itemCount: _fields.length, itemBuilder: (BuildContext context, int index) { FormField field = _fields[index]; Widget widget; switch (field.type) { case 'text': case 'email': case 'tel': widget = TextFormField( decoration: InputDecoration( labelText: field.label, ), keyboardType: TextInputType.text, validator: (value) { if (field.required && value!.isEmpty) { return 'This field is required'; } return null; }, onSaved: (value) { field.value = value!; }, ); break; case 'textarea': widget = TextFormField( decoration: InputDecoration( labelText: field.label, ), keyboardType: TextInputType.multiline, maxLines: 4, validator: (value) { if (field.required && value!.isEmpty) { return 'This field is required'; } return null; }, onSaved: (value) { field.value = value!; }, ); break; default: widget = Container(); } return widget; }, ), ), floatingActionButton: FloatingActionButton( onPressed: () { if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); // Submit form data } }, child: Icon(Icons.send), ), ); } } ``` 在上面的示例代码中,我们定义了一个FormField类来表示表单字段,包括字段名称、类型、是否必填以及字段值等属性。然后我们在StatefulWidget的状态类中定义了一个_fields列表来存储表单字段数据模型。在build方法中,我们使用ListView.builder来构建表单UI,根据表单字段数据模型动态生成相应的表单控件。最后,在提交按钮的点击事件中,我们根据表单字段数据模型收集用户填写的表单数据,并进行校验处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马志武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值