搜索插件、代码仓与 APP 分类

插件详情v0.8.1

formz

一种统一的 Dart 表单表示形式,旨在以通用方式简化表单表示和验证。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
24.2万
喜欢数
919
Pub 分数
160

安装插件

flutter pub add formz

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Formz 是一个 Dart 包,提供了一种统一且通用的表单表示和验证方法。它通过提供一致的结构来处理输入、验证规则和错误消息,简化了表单状态的管理。Formz 旨在简单性和可重用性,使开发者能够以最少的样板代码创建表单,同时在跨平台环境中保持类型安全和可扩展性。它可无缝集成到 Flutter 应用中,支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 构建可重用的表单组件
  • 管理复杂的表单验证逻辑
  • 创建一致的表单状态处理
  • 简化表单提交工作流程
  • 与状态管理解决方案集成
  • 支持国际化错误消息

主要特点

  • 通用表单状态表示
  • 内置验证支持
  • 可扩展的验证规则
  • 类型安全的表单字段
  • 最小化样板代码
  • 跨平台兼容性

适合对象

  • 寻求简洁表单模式的 Flutter 开发人员
  • 需要强大表单验证的项目
  • 希望复用表单逻辑的团队
  • 面向多个平台的应用程序
  • 使用函数式或响应式状态管理的开发者

使用建议

  • 需要理解 Dart 泛型
  • 可能需要为自定义验证器进行额外设置
  • 最佳搭配状态管理,如 Bloc 或 Provider
  • 不是完整的UI解决方案——专注于表单逻辑
  • 有限的内置UI组件

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

📝 Formz

Very Good Ventures Very Good Ventures

Developed with 💙 by Very Good Ventures 🦄

ci coverage pub package License: MIT style: very good analysis


A unified form representation in Dart. Formz aims to simplify form representation and validation in a generic way.

Create a FormzInput

import 'package:formz/formz.dart';

// Define input validation errors
enum NameInputError { empty }

// Extend FormzInput and provide the input type and error type.
class NameInput extends FormzInput<String, NameInputError> {
  // Call super.pure to represent an unmodified form input.
  const NameInput.pure() : super.pure('');

  // Call super.dirty to represent a modified form input.
  const NameInput.dirty({String value = ''}) : super.dirty(value);

  // Override validator to handle validating a given input value.
  @override
  NameInputError? validator(String value) {
    return value.isEmpty ? NameInputError.empty : null;
  }
}

Interact with a FormzInput

const name = NameInput.pure();
print(name.value); // ''
print(name.isValid); // false
print(name.error); // NameInputError.empty
print(name.displayError); // null

const joe = NameInput.dirty(value: 'joe');
print(joe.value); // 'joe'
print(joe.isValid); // true
print(joe.error); // null
print(name.displayError); // null

Validate Multiple FormzInput Items

const validInputs = <FormzInput>[
  NameInput.dirty(value: 'jan'),
  NameInput.dirty(value: 'jen'),
  NameInput.dirty(value: 'joe'),
];

print(Formz.validate(validInputs)); // true

const invalidInputs = <FormzInput>[
  NameInput.dirty(),
  NameInput.dirty(),
  NameInput.dirty(),
];

print(Formz.validate(invalidInputs)); // false

Automatic Validation

class LoginForm with FormzMixin {
  LoginForm({
    this.username = const Username.pure(),
    this.password = const Password.pure(),
  });

  final Username username;
  final Password password;

  @override
  List<FormzInput> get inputs => [username, password];
}

void main() {
  print(LoginForm().isValid); // false
}

Caching validation results

For cases where the validator method has an expensive implementation, consider using the FormzInputErrorCacheMixin mixin to cache the error result and improve performance.

import 'package:formz/formz.dart';

enum EmailValidationError { invalid }

class Email extends FormzInput<String, EmailValidationError>
    with FormzInputErrorCacheMixin {
  Email.pure([super.value = '']) : super.pure();

  Email.dirty([super.value = '']) : super.dirty();

  static final _emailRegExp = RegExp(
    r'^[a-zA-Z\d.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z\d-]+(?:\.[a-zA-Z\d-]+)*$',
  );

  @override
  EmailValidationError? validator(String value) {
    return _emailRegExp.hasMatch(value) ? null : EmailValidationError.invalid;
  }
}

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

所在榜单