Flutter表单基础字段插件forme_base_fields的使用
Flutter表单基础字段插件forme_base_fields的使用
在本教程中,我们将探讨如何使用forme_base_fields插件来创建和管理表单。该插件提供了多种字段类型,如文本输入、日期时间选择器、数字输入等。
当前支持的字段
下表列出了当前支持的字段及其返回值和是否允许为空:
| 名称 | 返回值 | 可为空 |
|---|---|---|
| FormeTextField | string | false |
| FormeDateTimeField | DateTime | true |
| FormeNumberField | num | true |
| FormeTimeField | TimeOfDay | true |
| FormeDateRangeField | DateTimeRange | true |
| FormeSlider | double | false |
| FormeRangeSlider | RangeValues | false |
| FormeFilterChip | List<T> | false |
| FormeChoiceChip | T | true |
| FormeCheckbox | bool | true |
| FormeCheckboxListTile | bool | true |
| FormeSwitch | bool | false |
| FormeSwitchTile | bool | false |
| FormeDropdownButton | T | true |
| FormeListTile | List<T> | false |
| FormeRadioGroup | T | true |
| FormeCupertinoTextField | string | false |
| FormeCupertinoDateTimeField | DateTime | true |
| FormeCupertinoNumberField | num | true |
| FormeCupertinoPicker | int | false |
| FormeCupertinoSegmentedControl | T | true |
| FormeCupertinoSlidingSegmentedControl | T | true |
| FormeCupertinoSlider | double | false |
| FormeCupertinoSwitch | bool | false |
| FormeCupertinoTimerField | Duration | true |
| FormeExpansionListTile | List<T> | false |
TextEditingController
为了简化表单控制,某些字段不支持直接设置TextEditingController,例如FormeTextField、FormeNumberField、FormeDateTimeField等。
如果你需要访问底层的TextEditingController,可以尝试将FormeFieldController转换为FormeTextFieldController或其他控制器。
示例代码
以下是一个简单的示例,展示了如何使用forme_base_fields插件创建一个包含多个字段的表单。
import 'package:flutter/material.dart';
import 'package:forme_base_fields/forme_base_fields.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Forme Base Fields Example'),
),
body: MyForm(),
),
);
}
}
class MyForm extends StatefulWidget {
[@override](/user/override)
_MyFormState createState() => _MyFormState();
}
class _MyFormState extends State<MyForm> {
final _formKey = GlobalKey<FormState>();
final _textEditingController = TextEditingController();
[@override](/user/override)
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: [
// 文本输入框
FormeTextField(
controller: _textEditingController,
decoration: InputDecoration(labelText: '请输入文本'),
),
SizedBox(height: 20),
// 数字输入框
FormeNumberField(
decoration: InputDecoration(labelText: '请输入数字'),
),
SizedBox(height: 20),
// 日期时间选择器
FormeDateTimeField(
decoration: InputDecoration(labelText: '请选择日期时间'),
),
SizedBox(height: 20),
// 提交按钮
ElevatedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
print('表单验证通过');
} else {
print('表单验证未通过');
}
},
child: Text('提交'),
),
],
),
),
);
}
[@override](/user/override)
void dispose() {
_textEditingController.dispose();
super.dispose();
}
}
更多关于Flutter表单基础字段插件forme_base_fields的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复


