flutter_ai_forms

A provider-agnostic, schema-driven Flutter dynamic form engine designed for AI-generated interfaces, dynamic validation, dynamic conditional fields, schema repair, and custom component extensions.

pub package License: MIT


🌟 Key Features

  • πŸ€– AI Provider Agnostic: Works with OpenAI (GPT-4o), Gemini, Claude, Ollama, OpenRouter, or Hugging Face via the AiFormGenerator contract.
  • 🩺 AI Form Repair (SchemaNormalizer): Automatically repairs fuzzy LLM output ("type": "emal" -> "email", "required": "yes" -> true, markdown code blocks) without crashing.
  • ⚑ Dynamic Conditional Fields: Real-time evaluation of visibleWhen, enabledWhen, and requiredWhen rules.
  • πŸ› οΈ Extensible Custom Registry: Register custom widgets (Star Ratings, Color Pickers, Signatures) via AiFormFieldRegistry.
  • 🎨 13 Standard v1.0 Field Types: Text, Email, Password, Number, Phone, Dropdown, Radio, Checkbox, Multi-Select, Date, Time, Slider, File.

πŸš€ Getting Started

Add flutter_ai_forms to your pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  flutter_ai_forms: ^1.0.0

πŸ’‘ Quick Usage Example

import 'package:flutter/material.dart';
import 'package:flutter_ai_forms/flutter_ai_forms.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    // 1. Define or parse your FormSchema JSON
    final formSchema = FormSchema.fromJson({
      "title": "Customer Registration",
      "fields": [
        {
          "id": "full_name",
          "type": "text",
          "label": "Full Name",
          "required": true
        },
        {
          "id": "email",
          "type": "email",
          "label": "Email Address",
          "required": true
        },
        {
          "id": "employment_status",
          "type": "dropdown",
          "label": "Employment Status",
          "options": ["Employed", "Student", "Unemployed"]
        },
        {
          "id": "company",
          "type": "text",
          "label": "Company Name",
          "visibleWhen": { "field": "employment_status", "equals": "Employed" }
        }
      ]
    });

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('flutter_ai_forms')),
        body: SingleChildScrollView(
          child: AiForm(
            schema: formSchema,
            onSubmit: (FormResult result) {
              if (result.isValid) {
                print('Form Values: ${result.values}');
              }
            },
          ),
        ),
      ),
    );
  }
}

🩺 AI Form Repair Engine Example

// Raw string payload returned by LLMs
String rawLlmString = '''
{
  "title": "User Form",
  "fields": [
    { "type": "emal", "label": "Email", "required": "yes" },
    { "type": "numb", "label": "Age", "min": "18" }
  ]
}
''';

// Normalize and repair output safely
FormParseResult parseResult = SchemaNormalizer.normalize(rawLlmString);

// Access cleaned schema
FormSchema schema = parseResult.schema;

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

Libraries

flutter_ai_forms