11import 'package:flutter/material.dart' ;
22import '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' ;
53
64void main () => runApp (const MyApp ());
75
@@ -13,12 +11,6 @@ class MyApp extends StatelessWidget {
1311 return MaterialApp (
1412 title: 'Flutter FormBuilder Example' ,
1513 debugShowCheckedModeBanner: false ,
16- localizationsDelegates: const [
17- FormBuilderLocalizations .delegate,
18- ...GlobalMaterialLocalizations .delegates,
19- GlobalWidgetsLocalizations .delegate,
20- ],
21- supportedLocales: FormBuilderLocalizations .supportedLocales,
2214 home: const _ExamplePage (),
2315 );
2416 }
@@ -37,56 +29,47 @@ class _ExamplePageState extends State<_ExamplePage> {
3729 @override
3830 Widget build (BuildContext context) {
3931 return Scaffold (
40- appBar: AppBar (title: const Text ('Minimal code example' )),
41- body: Padding (
42- padding: const EdgeInsets .all (16 ),
43- child: FormBuilder (
44- key: _formKey,
45- child: Column (
46- children: [
47- FormBuilderFilterChips <String >(
48- decoration: const InputDecoration (
49- labelText: 'The language of my people' ,
50- ),
51- name: 'languages_filter' ,
52- selectedColor: Colors .red,
53- options: const [
54- FormBuilderChipOption (
55- value: 'Dart' ,
56- avatar: CircleAvatar (child: Text ('D' )),
32+ body: SafeArea (
33+ child: Column (
34+ children: [
35+ FormBuilder (
36+ key: _formKey,
37+ child: Column (
38+ children: [
39+ FormBuilderTextField (
40+ name: 'full_name' ,
41+ decoration: const InputDecoration (labelText: 'Full Name' ),
42+ validator: (value) {
43+ if (value == null || value.isEmpty) {
44+ return 'Please enter your full name' ;
45+ }
46+ return null ;
47+ },
5748 ),
58- FormBuilderChipOption (
59- value: 'Kotlin' ,
60- avatar: CircleAvatar (child: Text ('K' )),
61- ),
62- FormBuilderChipOption (
63- value: 'Java' ,
64- avatar: CircleAvatar (child: Text ('J' )),
65- ),
66- FormBuilderChipOption (
67- value: 'Swift' ,
68- avatar: CircleAvatar (child: Text ('S' )),
69- ),
70- FormBuilderChipOption (
71- value: 'Objective-C' ,
72- avatar: CircleAvatar (child: Text ('O' )),
49+ Builder (
50+ builder: (innerContext) {
51+ return Text (
52+ FormBuilder .of (innerContext).isValid
53+ ? 'OK Valid'
54+ : 'X Invalid' ,
55+ );
56+ },
7357 ),
58+ const SizedBox (height: 10 ),
59+ Builder (builder: (innerContext) {
60+ return ElevatedButton (
61+ onPressed: () {
62+ FormBuilder .of (innerContext).saveAndValidate ();
63+ debugPrint (
64+ FormBuilder .of (innerContext).value.toString ());
65+ },
66+ child: const Text ('Print' ),
67+ );
68+ }),
7469 ],
75- validator: FormBuilderValidators .compose ([
76- FormBuilderValidators .minLength (1 ),
77- FormBuilderValidators .maxLength (3 ),
78- ]),
7970 ),
80- const SizedBox (height: 10 ),
81- ElevatedButton (
82- onPressed: () {
83- _formKey.currentState? .saveAndValidate ();
84- debugPrint (_formKey.currentState? .value.toString ());
85- },
86- child: const Text ('Print' ),
87- )
88- ],
89- ),
71+ ),
72+ ],
9073 ),
9174 ),
9275 );
0 commit comments