搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

所在榜單