【问题标题】:Validate a non-visible field in Flutter Form验证 Flutter Form 中的不可见字段
【发布时间】:2021-09-13 20:41:56
【问题描述】:

我有一个带有许多 dropdownformfield 和 textformfield 小部件的 Flutter 表单,使用 validate: 方法验证这些小部件是微不足道的。对视野的验证是显而易见的。

但是,在许多形式中,可能需要验证不可见元素。例如,如果在表单中拍照,则只有一个拍照按钮,该按钮会将生成的文件名输入到 String var 中。在这种情况下,我需要验证 String var 并将验证结果返回给按钮(即在按钮下方显示“必填字段”),但当然 String var 不会保存在任何 formfield 小部件中。

话虽如此,我如何将按钮“包装”在包含验证器的小部件中:方法,或者如何将验证器添加到按钮本身,然后在 UI 中向用户显示适当的验证消息?

谢谢!

【问题讨论】:

  • 如果我正确理解了您的问题,那么您不需要验证器,您需要向加载图像的小部件添加验证功能。打开其源代码并添加此功能

标签: flutter validation button


【解决方案1】:

您可以创建自己的FormField

class TakePictureFormField extends FormField<String> {
  /// Creates a [FormField] that contains an [ElevatedButton] to take a picture
  /// with the phone camera.
  ///
  /// The [String] value corresponds to the path of the picture taken.
  TakePictureFormField({
    Key? key,
    String? initialValue,
    FormFieldSetter<String>? onSaved,
    FormFieldValidator<String>? validator,
    bool enabled = true,
    AutovalidateMode? autovalidateMode,
    ButtonStyle? buttonStyle,
    void Function(String)? onChanged,
  }) : super(
          key: key,
          initialValue: initialValue,
          onSaved: onSaved,
          validator: validator,
          enabled: enabled,
          autovalidateMode: autovalidateMode,
          builder: (FormFieldState<String> field) {
            final currentValue = field.value;
            return InputDecorator(
              decoration: InputDecoration(
                border: InputBorder.none,
                errorText: field.errorText,
                errorBorder: OutlineInputBorder(
                  borderSide: BorderSide(
                    color: Theme.of(field.context).errorColor,
                  ),
                ),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  ElevatedButton(
                    style: buttonStyle,
                    onPressed: () async {
                      // Fake implementation to take a picture.
                      final value = await Future<String>.delayed(
                          const Duration(microseconds: 300),
                          () => 'my_path/to/image');

                      field.didChange(value);
                      if (onChanged != null) {
                        onChanged(value);
                      }
                    },
                    child: const Text('Take a Picture'),
                  ),
                  if (currentValue != null) Text(currentValue),
                ],
              ),
            );
          },
        );
}

然后在 Form 中使用它,就像对任何其他 FormField 小部件一样:

TakePictureFormField(
  validator: (val) =>
    val == null || val.isEmpty ? 'Error invalid picture' : null,
)

Try the complete example on DartPad

【讨论】:

  • 感谢您,我使用了这个示例并创建了一个 onChange 方法来使用图像的文件名更新 UI。效果很好,而且肯定是正确的方法!
猜你喜欢
  • 2014-03-04
  • 1970-01-01
  • 2020-10-20
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 2022-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多