|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_form_builder/flutter_form_builder.dart'; |
| 3 | +import 'package:flutter_localizations/flutter_localizations.dart'; |
| 4 | +import 'package:form_builder_validators/form_builder_validators.dart'; |
| 5 | + |
| 6 | +void main() => runApp(const MyApp()); |
| 7 | + |
| 8 | +class MyApp extends StatelessWidget { |
| 9 | + const MyApp({super.key}); |
| 10 | + |
| 11 | + @override |
| 12 | + Widget build(BuildContext context) { |
| 13 | + return MaterialApp( |
| 14 | + title: 'Flutter FormBuilder Example', |
| 15 | + debugShowCheckedModeBanner: false, |
| 16 | + localizationsDelegates: const [ |
| 17 | + FormBuilderLocalizations.delegate, |
| 18 | + ...GlobalMaterialLocalizations.delegates, |
| 19 | + GlobalWidgetsLocalizations.delegate, |
| 20 | + ], |
| 21 | + supportedLocales: FormBuilderLocalizations.supportedLocales, |
| 22 | + home: const _ExamplePage(), |
| 23 | + ); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class _ExamplePage extends StatefulWidget { |
| 28 | + const _ExamplePage(); |
| 29 | + |
| 30 | + @override |
| 31 | + State<_ExamplePage> createState() => _ExamplePageState(); |
| 32 | +} |
| 33 | + |
| 34 | +class _ExamplePageState extends State<_ExamplePage> { |
| 35 | + final _formKey = GlobalKey<FormBuilderState>(); |
| 36 | + |
| 37 | + @override |
| 38 | + Widget build(BuildContext context) { |
| 39 | + return Scaffold( |
| 40 | + body: FormBuilder( |
| 41 | + key: _formKey, |
| 42 | + child: Column( |
| 43 | + children: [ |
| 44 | + FormBuilderTextField( |
| 45 | + name: 'full_name', |
| 46 | + decoration: const InputDecoration(labelText: 'Full Name'), |
| 47 | + validator: FormBuilderValidators.compose([ |
| 48 | + FormBuilderValidators.required(), |
| 49 | + ]), |
| 50 | + ), |
| 51 | + const SizedBox(height: 10), |
| 52 | + ElevatedButton( |
| 53 | + onPressed: () { |
| 54 | + _formKey.currentState?.saveAndValidate(); |
| 55 | + debugPrint(_formKey.currentState?.value.toString()); |
| 56 | + }, |
| 57 | + child: const Text('Print'), |
| 58 | + ) |
| 59 | + ], |
| 60 | + ), |
| 61 | + ), |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
0 commit comments