Skip to main content

flutter_form_schema 1.0.1 copy "flutter_form_schema: ^1.0.1" to clipboard
flutter_form_schema: ^1.0.1 copied to clipboard

A production-ready, schema-driven form generation and validation package for Flutter. Define schemas once, generate validated forms automatically.

example/lib/main.dart

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Form Schema Demo',
      theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
      home: const FormDemoPage(),
    );
  }
}

class FormDemoPage extends StatefulWidget {
  const FormDemoPage({super.key});

  @override
  State<FormDemoPage> createState() => _FormDemoPageState();
}

class _FormDemoPageState extends State<FormDemoPage> {
  bool _useJson = false;

  final dartSchema = const FormSchema(
    id: 'user_registration',
    fields: [
      StringFieldSchema(
        key: 'username',
        label: 'Username',
        hint: 'Enter your preferred username',
        required: true,
        validators: [MinLengthValidator(4)],
      ),
      StringFieldSchema(
        key: 'email',
        label: 'Email',
        type: StringFieldType.email,
        required: true,
        validators: [EmailValidator()],
      ),
      StringFieldSchema(
        key: 'password',
        label: 'Password',
        type: StringFieldType.password,
        required: true,
        validators: [MinLengthValidator(8)],
      ),
      StringFieldSchema(
        key: 'confirmPassword',
        label: 'Confirm Password',
        type: StringFieldType.password,
        required: true,
      ),
      NumberFieldSchema(
        key: 'age',
        label: 'Age',
        min: 18,
        required: true,
        isInteger: true,
      ),
      BoolFieldSchema(
        key: 'terms',
        label: 'Accept Terms & Conditions',
        required: true,
        widgetType: BoolWidget.checkbox,
      ),
    ],
  );

  final jsonRaw = {
    "id": "profile_update",
    "fields": [
      {
        "key": "fullName",
        "type": "string",
        "label": "Full Name",
        "required": true,
      },
      {
        "key": "role",
        "type": "select",
        "label": "User Role",
        "options": [
          {"label": "Administrator", "value": "admin"},
          {"label": "Standard User", "value": "user"},
          {"label": "Guest", "value": "guest"},
        ],
      },
      {
        "key": "bio",
        "type": "string",
        "label": "Biography",
        "stringType": "multiline",
      },
      {
        "key": "birthDate",
        "type": "date",
        "label": "Birth Date",
        "dateType": "date",
      },
    ],
  };

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Form Schema Demo'),
        actions: [
          Row(
            children: [
              const Text('JSON Schema'),
              Switch(
                value: _useJson,
                onChanged: (val) => setState(() => _useJson = val),
              ),
            ],
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            SchemaForm(
              schema: _useJson ? FormSchema.fromJson(jsonRaw) : dartSchema,
              crossFieldValidators: _useJson
                  ? []
                  : [
                      CrossFieldValidator(
                        fields: ['password', 'confirmPassword'],
                        validate: (values) {
                          if (values['password'] != values['confirmPassword']) {
                            return 'Passwords must match';
                          }
                          return null;
                        },
                      ),
                    ],
              onSubmit: (values) async {
                showDialog(
                  context: context,
                  builder: (context) => AlertDialog(
                    title: const Text('Success'),
                    content: Text('Form submitted with values:\n$values'),
                    actions: [
                      TextButton(
                        onPressed: () => Navigator.pop(context),
                        child: const Text('OK'),
                      ),
                    ],
                  ),
                );
              },
              onValidationError: (errors) {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Validation failed: $errors')),
                );
              },
              submitButtonLabel: 'Register',
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
140
points
40
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A production-ready, schema-driven form generation and validation package for Flutter. Define schemas once, generate validated forms automatically.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, intl, meta

More

Packages that depend on flutter_form_schema