-
-
Notifications
You must be signed in to change notification settings - Fork 562
Expand file tree
/
Copy pathcustom_fields.dart
More file actions
110 lines (105 loc) · 3.5 KB
/
custom_fields.dart
File metadata and controls
110 lines (105 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
class CustomFields extends StatefulWidget {
const CustomFields({super.key});
@override
State<CustomFields> createState() => _CustomFieldsState();
}
class _CustomFieldsState extends State<CustomFields> {
final _formKey = GlobalKey<FormBuilderState>();
static const List<String> _kOptions = <String>[
'pikachu',
'bulbasaur',
'charmander',
'squirtle',
'caterpie',
];
@override
Widget build(BuildContext context) {
return FormBuilder(
key: _formKey,
child: Column(
children: <Widget>[
const SizedBox(height: 20),
FormBuilderField<DateTime?>(
name: 'date',
builder: (FormFieldState field) {
return InputDatePickerFormField(
firstDate: DateTime.now(),
lastDate: DateTime.now().add(const Duration(days: 30)),
onDateSubmitted: (value) => field.didChange(value),
errorInvalidText: field.errorText,
onDateSaved: (value) => field.didChange(value),
);
},
),
const SizedBox(height: 10),
FormBuilderField<bool>(
name: 'terms',
builder: (FormFieldState field) {
return CheckboxListTile(
title: const Text('I Accept the terms and conditions'),
value: field.value ?? false,
controlAffinity: ListTileControlAffinity.leading,
onChanged: (value) => field.didChange(value),
);
},
),
const SizedBox(height: 10),
FormBuilderField<String?>(
name: 'name',
builder: (FormFieldState field) {
return Autocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text == '') {
return const Iterable<String>.empty();
}
return _kOptions.where((String option) {
return option.contains(textEditingValue.text.toLowerCase());
});
},
onSelected: (String selection) {
field.didChange(selection);
},
);
},
autovalidateMode: AutovalidateMode.always,
validator: (valueCandidate) {
if (valueCandidate?.isEmpty ?? true) {
return 'This field is required.';
}
return null;
},
),
const SizedBox(height: 10),
Row(
children: <Widget>[
Expanded(
child: ElevatedButton(
child: const Text("Submit"),
onPressed: () {
_formKey.currentState!.save();
if (_formKey.currentState!.validate()) {
debugPrint(_formKey.currentState!.value.toString());
} else {
debugPrint("validation failed");
}
},
),
),
const SizedBox(width: 20),
Expanded(
child: ElevatedButton(
child: const Text("Reset"),
onPressed: () {
_formKey.currentState!.reset();
},
),
),
],
),
],
),
);
}
}