flutter_form_validation 0.1.0
flutter_form_validation: ^0.1.0 copied to clipboard
A collection of reusable form widgets for Flutter — validated text fields with presets (email, password, phone, name), built-in validators, and loading button with error display.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_form_validation/flutter_form_validation.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Form Validation Demo',
theme: ThemeData(primarySwatch: Colors.teal),
home: const LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _formKey = GlobalKey<FormState>();
final _emailC = TextEditingController();
final _passC = TextEditingController();
final _nameC = TextEditingController();
final _phoneC = TextEditingController();
bool _isLoading = false;
bool _isSuccess = false;
String? _error;
@override
void dispose() {
_emailC.dispose();
_passC.dispose();
_nameC.dispose();
_phoneC.dispose();
super.dispose();
}
Future<void> _submit() async {
if (!_formKey.currentState!.validate()) return;
setState(() {
_isLoading = true;
_error = null;
});
// Simulate API call
await Future.delayed(const Duration(seconds: 2));
if (_emailC.text == 'error@test.com') {
setState(() {
_error = 'Email atau password salah';
_isLoading = false;
});
} else {
setState(() {
_isLoading = false;
_isSuccess = true;
});
await Future.delayed(const Duration(seconds: 1));
setState(() => _isSuccess = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Form Demo')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ValidatedTextField.name(
controller: _nameC,
label: 'Nama Lengkap',
),
const SizedBox(height: 16),
ValidatedTextField.email(
controller: _emailC,
label: 'Email',
),
const SizedBox(height: 16),
ValidatedTextField.phone(
controller: _phoneC,
label: 'No. Telepon',
),
const SizedBox(height: 16),
ValidatedTextField.password(
controller: _passC,
label: 'Password',
requireNumber: true,
),
const SizedBox(height: 16),
// Custom field with multiple validators
ValidatedTextField(
controller: TextEditingController(),
label: 'Kode (custom)',
hint: 'Min 4 karakter, huruf besar & angka',
validators: [
const RequiredValidator(),
const MinLengthValidator(4),
PatternValidator(
RegExp(r'^[A-Z0-9]+$'),
message: 'Hanya huruf besar & angka',
),
],
),
const SizedBox(height: 24),
LoadingButton(
isLoading: _isLoading,
isSuccess: _isSuccess,
label: 'Daftar',
loadingLabel: 'Mendaftarkan...',
successLabel: 'Pendaftaran berhasil!',
onPressed: _submit,
errorMessage: _error,
icon: const Icon(Icons.person_add),
),
const SizedBox(height: 16),
const Text(
'💡 Tips: coba email "error@test.com" untuk lihat error state',
style: TextStyle(fontSize: 12, color: Colors.grey),
textAlign: TextAlign.center,
),
],
),
),
),
);
}
}