form_shield 0.2.2 copy "form_shield: ^0.2.2" to clipboard
form_shield: ^0.2.2 copied to clipboard

A declarative, rule-based form validation library for Flutter apps.

example/lib/example.dart

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

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

  @override
  State<LoginFormExample> createState() => _LoginFormExampleState();
}

class _LoginFormExampleState extends State<LoginFormExample> {
  final _formKey = GlobalKey<FormState>();
  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();
  bool _rememberMe = false;

  @override
  void dispose() {
    _emailController.dispose();
    _passwordController.dispose();
    super.dispose();
  }

  void _submitForm() {
    if (_formKey.currentState!.validate()) {
      // Form is valid, proceed with login
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Processing login...')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Login Form Example')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              TextFormField(
                controller: _emailController,
                decoration: const InputDecoration(
                  labelText: 'Email',
                  hintText: 'Enter your email',
                  prefixIcon: Icon(Icons.email),
                ),
                keyboardType: TextInputType.emailAddress,
                validator: Validator<String>([
                  const RequiredRule(errorMessage: 'Email is required'),
                  EmailRule(errorMessage: 'Please enter a valid email address'),
                ]).call,
              ),
              const SizedBox(height: 16),
              TextFormField(
                controller: _passwordController,
                decoration: const InputDecoration(
                  labelText: 'Password',
                  hintText: 'Enter your password',
                  prefixIcon: Icon(Icons.lock),
                ),
                obscureText: true,
                validator: Validator<String>(const [
                  RequiredRule(errorMessage: 'Password is required'),
                  PasswordRule(
                    options: PasswordOptions(
                      minLength: 8,
                      requireUppercase: true,
                      requireLowercase: true,
                      requireDigit: true,
                      requireSpecialChar: true,
                    ),
                    errorMessage:
                        'Password must be at least 8 characters with uppercase, lowercase, digit, and special character',
                  ),
                ]).call,
              ),
              const SizedBox(height: 8),
              Row(
                children: [
                  Checkbox(
                    value: _rememberMe,
                    onChanged: (value) {
                      setState(() {
                        _rememberMe = value ?? false;
                      });
                    },
                  ),
                  const Text('Remember me'),
                ],
              ),
              const SizedBox(height: 24),
              ElevatedButton(
                onPressed: _submitForm,
                child: const Text('Login'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
15
likes
0
points
379
downloads

Publisher

verified publisherstevenosse.com

Weekly Downloads

A declarative, rule-based form validation library for Flutter apps.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on form_shield