Agent SkillsMimicHunterZ/PocketMind › flutter-form

flutter-form

GitHub

指导在Flutter中实现带验证功能的表单,涵盖Form、TextFormField及GlobalKey的使用,处理用户输入校验与状态更新。

.claude/skills/flutter-form/SKILL.md MimicHunterZ/PocketMind

Trigger Scenarios

需要构建Flutter表单 实现表单字段验证逻辑

Install

npx skills add MimicHunterZ/PocketMind --skill flutter-form -g -y
More Options

Non-standard path

npx skills add https://github.com/MimicHunterZ/PocketMind/tree/master/.claude/skills/flutter-form -g -y

Use without installing

npx skills use MimicHunterZ/PocketMind@flutter-form

指定 Agent (Claude Code)

npx skills add MimicHunterZ/PocketMind --skill flutter-form -a claude-code -g -y

安装 repo 全部 skill

npx skills add MimicHunterZ/PocketMind --all -g -y

预览 repo 内 skill

npx skills add MimicHunterZ/PocketMind --list

SKILL.md

Frontmatter
{
    "name": "flutter-form",
    "metadata": {
        "model": "models\/gemini-3.1-pro-preview",
        "last_modified": "Wed, 11 Mar 2026 16:47:50 GMT"
    },
    "description": "Build a form with validation"
}

Flutter Form Validation

Goal

Implements stateful form validation in Flutter using Form, TextFormField, and GlobalKey<FormState>. Manages validation state efficiently without unnecessary key regeneration and handles user input validation workflows. Assumes a pre-existing Flutter environment with Material Design dependencies available.

Decision Logic

When implementing form validation, follow this decision tree to determine the flow of state and UI updates:

  1. User triggers submit action:
    • Call _formKey.currentState!.validate().
  2. Does validate() return true?
    • Yes (Valid): Proceed with data processing (e.g., API call, local storage). Trigger success UI feedback (e.g., SnackBar, navigation).
    • No (Invalid): The FormState automatically rebuilds the TextFormField widgets to display the String error messages returned by their respective validator functions. Halt submission.

Instructions

  1. Initialize the Stateful Form Container Create a StatefulWidget to hold the form. Instantiate a GlobalKey<FormState> exactly once within the State class to prevent resource-expensive key regeneration during build cycles.

    import 'package:flutter/material.dart';
    
    class CustomValidatedForm extends StatefulWidget {
      const CustomValidatedForm({super.key});
    
      @override
      State<CustomValidatedForm> createState() => _CustomValidatedFormState();
    }
    
    class _CustomValidatedFormState extends State<CustomValidatedForm> {
      // Instantiate the GlobalKey once in the State object
      final _formKey = GlobalKey<FormState>();
    
      @override
      Widget build(BuildContext context) {
        return Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              // Form fields will be injected here
            ],
          ),
        );
      }
    }
    
  2. Implement TextFormFields with Validation Logic Inject TextFormField widgets into the Form's widget tree. Provide a validator function for each field.

    TextFormField(
      decoration: const InputDecoration(
        hintText: 'Enter your email',
        labelText: 'Email',
      ),
      validator: (String? value) {
        if (value == null || value.isEmpty) {
          return 'Please enter an email address';
        }
        if (!value.contains('@')) {
          return 'Please enter a valid email address';
        }
        // Return null if the input is valid
        return null; 
      },
      onSaved: (String? value) {
        // Handle save logic here
      },
    )
    
  3. Implement the Submit Action and Validation Trigger Create a button that accesses the FormState via the GlobalKey to trigger validation.

    Padding(
      padding: const EdgeInsets.symmetric(vertical: 16.0),
      child: ElevatedButton(
        onPressed: () {
          // Validate returns true if the form is valid, or false otherwise.
          if (_formKey.currentState!.validate()) {
            // Save the form fields if necessary
            _formKey.currentState!.save();
    
            // Provide success feedback
            ScaffoldMessenger.of(context).showSnackBar(
              const SnackBar(content: Text('Processing Data')),
            );
          }
        },
        child: const Text('Submit'),
      ),
    )
    
  4. STOP AND ASK THE USER: Pause implementation and ask the user for the following context:

    • "What specific fields do you need in this form?"
    • "What are the exact validation rules for each field (e.g., regex patterns, minimum lengths)?"
    • "What action should occur upon successful validation (e.g., API payload submission, navigation)?"
  5. Validate-and-Fix Loop After generating the form, verify the following:

    • Ensure _formKey.currentState!.validate() is null-checked properly using the bang operator (!) or safe calls if the key might be detached.
    • Verify that every validator function explicitly returns null on success. Returning an empty string ("") will trigger an error state with no text.

Constraints

  • DO NOT instantiate the GlobalKey<FormState> inside the build method. It must be a persistent member of the State class.
  • DO NOT use a StatelessWidget for the form container unless the GlobalKey is being passed down from a stateful parent.
  • DO NOT use standard TextField widgets if you require built-in form validation; you must use TextFormField (which wraps TextField in a FormField).
  • ALWAYS return null from a validator function when the input is valid.
  • ALWAYS ensure the Form widget is a common ancestor to all TextFormField widgets that need to be validated together.

Version History

  • c1dc382 Current 2026-08-20 13:17

Same Skill Collection

.claude/skills/asset-image-subsystem/SKILL.md
.claude/skills/baoyu-danger-x-to-markdown/SKILL.md
.claude/skills/flutter-accessibility/SKILL.md
.claude/skills/flutter-app-size/SKILL.md
.claude/skills/flutter-architecture/SKILL.md
.claude/skills/flutter-concurrency/SKILL.md
.claude/skills/flutter-environment-setup-linux/SKILL.md
.claude/skills/flutter-environment-setup-macos/SKILL.md
.claude/skills/flutter-environment-setup-windows/SKILL.md
.claude/skills/flutter-http-and-json/SKILL.md
.claude/skills/flutter-layout/SKILL.md
.claude/skills/flutter-localization/SKILL.md
.claude/skills/flutter-native-interop/SKILL.md
.claude/skills/flutter-plugins/SKILL.md
.claude/skills/flutter-routing-and-navigation/SKILL.md
.claude/skills/flutter-testing/SKILL.md
.claude/skills/flutter-theming/SKILL.md
.claude/skills/headless-scraping/SKILL.md
.claude/skills/moai-lang-flutter/SKILL.md
.claude/skills/mobile-ai-chat-client/SKILL.md
.claude/skills/mobile-note-sync-architecture/SKILL.md
.claude/skills/note-resource-catalog-reliability/SKILL.md
.claude/skills/note-resource-event-driven-architecture/SKILL.md
.claude/skills/pocketmind-context-architecture/SKILL.md
.claude/skills/skill-creator/SKILL.md
backend/.claude/skills/code-reviewer/SKILL.md
.claude/skills/flutter-animation/SKILL.md
.claude/skills/flutter-caching/SKILL.md
.claude/skills/flutter-databases/SKILL.md
.claude/skills/flutter-home-screen-widget/SKILL.md
.claude/skills/flutter-performance/SKILL.md
.claude/skills/flutter-platform-views/SKILL.md
.claude/skills/flutter-state-management/SKILL.md

Metadata

Files
0
Version
c1dc382
Hash
5915b29e
Indexed
2026-08-20 13:17

- 위키
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-25 10:29
浙ICP备14020137号-1 $방문자$