如何在Flutter中创建TextFormField函数

vxqlmq5t  于 2022-12-19  发布在  Flutter
关注(0)|答案(1)|浏览(127)

一个包含一个文本字段的表单字段。这是一个方便的小部件,它将一个文本字段小部件 Package 在一个表单字段中。

static formField({
    required String label,
    String? initialValue,
    FormFieldSetter? onSaved,
    ValueChanged? onChanged,
    Icon? icon,
    int? maxLines,
    TextEditingController? controller,
    TextInputType? keyboard,
  }) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 8),
      child: TextFormField(
        validator: (value) {
          if (value!.isEmpty) {
            return 'please type the $label above';
          }
        },
        initialValue: initialValue,
        controller: controller,
        maxLines: maxLines,
        onChanged: onChanged,
        onSaved: onSaved,
        keyboardType: keyboard,
        decoration: InputDecoration(
          label: Text(label),
          prefixIcon: icon,
          hintText: 'Type $label here',
          border: const OutlineInputBorder(),
        ),
      ),
    );
  }
deyfvvtc

deyfvvtc1#

return Padding(
  padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 8),
  child: Container(
    child: TextFormField(
      validator: (value) {
        if (value!.isEmpty) {
          return 'please type the $label above';
        }
      },
      initialValue: initialValue,
      controller: controller,
      maxLines: maxLines,
      onChanged: onChanged,
      onSaved: onSaved,
      keyboardType: keyboard,
      decoration: InputDecoration(
        label: Text(label),
        prefixIcon: icon,
        hintText: 'Type $label here',
        border: const OutlineInputBorder(),
      ),
    ),
  ),
);

我认为这将工作... Package 容器内的文本表单字段..如果有任何错误的语法更改它

相关问题