shadcn_ui-form
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseShadcn UI — Form
Shadcn UI — 表单
Instructions
使用说明
ShadFormMap<String, dynamic>GlobalKey<ShadFormState>saveAndValidate()valueidShad*FormFieldShadInputFormFieldShadCheckboxFormFieldShadSelectFormFieldShadDatePickerFormFieldShadTimePickerFormFieldShadRadioGroupFormFieldShadFormShadFormMap<String, dynamic>GlobalKey<ShadFormState>saveAndValidate()valueidShadFormShad*FormFieldShadInputFormFieldShadCheckboxFormFieldShadSelectFormFieldShadDatePickerFormFieldShadTimePickerFormFieldShadRadioGroupFormFieldBasic form
基础表单
dart
final formKey = GlobalKey<ShadFormState>();
ShadForm(
key: formKey,
child: Column(
children: [
ShadInputFormField(
id: 'username',
label: const Text('Username'),
placeholder: const Text('Enter your username'),
description: const Text('This is your public display name.'),
validator: (v) {
if (v.length < 2) return 'Username must be at least 2 characters.';
return null;
},
),
const SizedBox(height: 16),
ShadButton(
child: const Text('Submit'),
onPressed: () {
if (formKey.currentState!.saveAndValidate()) {
print('value: ${formKey.currentState!.value}');
} else {
print('validation failed');
}
},
),
],
),
)dart
final formKey = GlobalKey<ShadFormState>();
ShadForm(
key: formKey,
child: Column(
children: [
ShadInputFormField(
id: 'username',
label: const Text('Username'),
placeholder: const Text('Enter your username'),
description: const Text('This is your public display name.'),
validator: (v) {
if (v.length < 2) return 'Username must be at least 2 characters.';
return null;
},
),
const SizedBox(height: 16),
ShadButton(
child: const Text('Submit'),
onPressed: () {
if (formKey.currentState!.saveAndValidate()) {
print('value: ${formKey.currentState!.value}');
} else {
print('validation failed');
}
},
),
],
),
)Submit and get value
提交并获取值
dart
ShadButton(
child: const Text('Submit'),
onPressed: () {
final formState = formKey.currentState!;
if (!formState.saveAndValidate()) return;
print('Form value: ${formState.value}');
},
),dart
ShadButton(
child: const Text('Submit'),
onPressed: () {
final formState = formKey.currentState!;
if (!formState.saveAndValidate()) return;
print('Form value: ${formState.value}');
},
),Initial value
初始值
Set default values with on :
initialValueShadFormdart
ShadForm(
initialValue: {
'username': 'john_doe',
'email': 'john_doe@example.com',
},
child: // form fields with matching ids
)通过 的 设置默认值:
ShadForminitialValuedart
ShadForm(
initialValue: {
'username': 'john_doe',
'email': 'john_doe@example.com',
},
child: // 对应id的表单字段
)Set field or full value
设置单个字段或完整表单值
- Single field: (use
formKey.currentState!.setFieldValue('username', 'new_username');to skip updating the field UI).notifyField: false - Entire form: (use
formKey.currentState!.setValue({...});to skip updating fields).notifyFields: false
- 单个字段:(使用
formKey.currentState!.setFieldValue('username', 'new_username');可跳过更新字段UI)。notifyField: false - 完整表单:(使用
formKey.currentState!.setValue({...});可跳过更新所有字段)。notifyFields: false
Value transformers
值转换器
Use and on form fields when the form value type differs from the field type (e.g. form stores string, field uses ):
fromValueTransformertoValueTransformerDateTimedart
ShadDatePickerFormField(
id: 'date',
fromValueTransformer: (value) => DateTime.tryParse(value ?? ''),
toValueTransformer: (date) =>
date == null ? null : DateFormat('yyyy-MM-dd').format(date),
...
)当表单值类型与字段类型不同时(例如表单存储字符串,字段使用 ),可在表单字段上使用 和 :
DateTimefromValueTransformertoValueTransformerdart
ShadDatePickerFormField(
id: 'date',
fromValueTransformer: (value) => DateTime.tryParse(value ?? ''),
toValueTransformer: (date) =>
date == null ? null : DateFormat('yyyy-MM-dd').format(date),
...
)Dot notation for nested values
嵌套值的点符号用法
Field IDs like or produce nested maps in , e.g. . Provide as a nested map (not dot-notation keys). Customize separator with (e.g. ); set to disable nesting and keep flat keys.
user.nameprofile.settings.themeformKey.currentState!.value{'user': {'name': '...', 'email': '...'}}initialValuefieldIdSeparator'/'fieldIdSeparator: null类似 或 的字段ID会在 中生成嵌套映射,例如 。需以嵌套映射形式提供 (而非点符号键)。可通过 自定义分隔符(例如 );设置 可禁用嵌套,保持扁平键。
user.nameprofile.settings.themeformKey.currentState!.value{'user': {'name': '...', 'email': '...'}}initialValuefieldIdSeparator'/'fieldIdSeparator: nullAdditional resources
额外资源
- Full details on initial value, setFieldValue/setValue, value transformers, and dot notation: reference.md
- Form field examples: Checkbox, Switch, Input, Select, RadioGroup, DatePicker, TimePicker component docs/skills.
- 关于初始值、setFieldValue/setValue、值转换器和点符号的详细说明:reference.md
- 表单字段示例:Checkbox、Switch、Input、Select、RadioGroup、DatePicker、TimePicker组件文档/技能。