Skip to content

Commit 315002e

Browse files
feat: add conditonal fields example
1 parent c0694b7 commit 315002e

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

example/lib/main.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:example/sources/conditional_fields.dart';
12
import 'package:example/sources/dynamic_fields.dart';
23
import 'package:flutter/material.dart';
34
import 'package:flutter_localizations/flutter_localizations.dart';
@@ -105,6 +106,23 @@ class _HomePage extends StatelessWidget {
105106
);
106107
},
107108
),
109+
const Divider(),
110+
ListTile(
111+
title: const Text('Conditional Form'),
112+
trailing: const Icon(Icons.arrow_right_sharp),
113+
onTap: () {
114+
Navigator.of(context).push(
115+
MaterialPageRoute(
116+
builder: (context) {
117+
return const CodePage(
118+
title: 'Conditional Form',
119+
child: ConditionalFields(),
120+
);
121+
},
122+
),
123+
);
124+
},
125+
),
108126
],
109127
),
110128
);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_form_builder/flutter_form_builder.dart';
3+
import 'package:form_builder_validators/form_builder_validators.dart';
4+
5+
class ConditionalFields extends StatefulWidget {
6+
const ConditionalFields({Key? key}) : super(key: key);
7+
8+
@override
9+
State<ConditionalFields> createState() => _ConditionalFieldsState();
10+
}
11+
12+
class _ConditionalFieldsState extends State<ConditionalFields> {
13+
final _formKey = GlobalKey<FormBuilderState>();
14+
int? option;
15+
16+
@override
17+
Widget build(BuildContext context) {
18+
return FormBuilder(
19+
key: _formKey,
20+
child: Column(
21+
children: <Widget>[
22+
const SizedBox(height: 20),
23+
FormBuilderDropdown<int>(
24+
name: 'options',
25+
validator: FormBuilderValidators.required(),
26+
decoration: const InputDecoration(
27+
label: Text('Select the option'),
28+
),
29+
onChanged: (value) {
30+
setState(() {
31+
option = value;
32+
});
33+
},
34+
items: const [
35+
DropdownMenuItem(value: 0, child: Text('Show textfield')),
36+
DropdownMenuItem(value: 1, child: Text('Show info text')),
37+
],
38+
),
39+
const SizedBox(height: 10),
40+
Visibility(
41+
visible: option == 0,
42+
// Can use to recreate completely the field
43+
// maintainState: false,
44+
child: FormBuilderTextField(
45+
name: 'textfield',
46+
validator: FormBuilderValidators.minLength(4),
47+
decoration: const InputDecoration(
48+
label: Text('Magic field'),
49+
),
50+
),
51+
),
52+
Visibility(
53+
visible: option == 1,
54+
child: const Text('Magic info'),
55+
),
56+
const SizedBox(height: 10),
57+
MaterialButton(
58+
color: Theme.of(context).colorScheme.secondary,
59+
child: const Text(
60+
"Submit",
61+
style: TextStyle(color: Colors.white),
62+
),
63+
onPressed: () {
64+
_formKey.currentState!.saveAndValidate();
65+
debugPrint(_formKey.currentState?.instantValue.toString() ?? '');
66+
},
67+
),
68+
],
69+
),
70+
);
71+
}
72+
}

example/lib/sources/dynamic_fields.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class _DynamicFieldsState extends State<DynamicFields> {
8181
],
8282
),
8383
const Divider(height: 40),
84-
Text('Saved value: ${_formKey.currentState?.value.toString() ?? ''}'),
84+
Text('Saved value: $savedValue'),
8585
],
8686
),
8787
);

0 commit comments

Comments
 (0)