Skip to content

Commit 40f14ce

Browse files
committed
Added more TextField options, fixed onChanged bug on TextField
1 parent 01cf41c commit 40f14ce

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

example/lib/main.dart

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class MyHomePageState extends State<MyHomePage> {
4545
final ValueChanged _onChanged = (val) => print(val);
4646
var genderOptions = ['Male', 'Female', 'Other'];
4747
final _ageController = TextEditingController(text: '45');
48+
bool _ageHasError = false;
4849

4950
@override
5051
Widget build(BuildContext context) {
@@ -236,7 +237,7 @@ class MyHomePageState extends State<MyHomePage> {
236237
label: RichText(
237238
text: TextSpan(
238239
children: [
239-
TextSpan(text: 'I have read and agree to the '),
240+
TextSpan(text: 'I have read and agree to the ', style: TextStyle(color: Colors.black)),
240241
TextSpan(
241242
text: 'Terms and Conditions',
242243
style: TextStyle(color: Colors.blue),
@@ -256,26 +257,30 @@ class MyHomePageState extends State<MyHomePage> {
256257
],
257258
),
258259
SizedBox(height: 15),
259-
Text(
260-
'This value is passed along to the [Text.maxLines] attribute of the [Text] widget used to display the hint text.',
261-
style: Theme.of(context).inputDecorationTheme.labelStyle,
262-
),
263260
FormBuilderTextField(
264261
attribute: 'age',
262+
// autovalidate: true,
265263
controller: _ageController,
266-
decoration: const InputDecoration(
267-
labelText:
268-
'This value is passed along to the [Text.maxLines] attribute of the [Text] widget used to display the hint text.',
264+
decoration: InputDecoration(
265+
labelText: 'Age',
266+
suffixIcon: _ageHasError ?
267+
Icon(Icons.error, color: Colors.red)
268+
: Icon(Icons.check, color: Colors.green),
269269
),
270-
onChanged: _onChanged,
270+
onChanged: (val) {
271+
print(val);
272+
setState(() {
273+
_ageHasError = !_fbKey
274+
.currentState.fields['age'].currentState
275+
.validate();
276+
});
277+
},
271278
valueTransformer: (text) {
272279
return text == null ? null : num.tryParse(text);
273280
},
274281
validators: [
275282
FormBuilderValidators.required(),
276283
FormBuilderValidators.numeric(),
277-
// FormBuilderValidators.max(70),
278-
FormBuilderValidators.minLength(2, allowEmpty: true),
279284
],
280285
keyboardType: TextInputType.number,
281286
),
@@ -596,3 +601,5 @@ class MyHomePageState extends State<MyHomePage> {
596601
);
597602
}
598603
}
604+
605+

lib/src/fields/form_builder_text_field.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class FormBuilderTextField extends StatefulWidget {
4646
final bool showCursor;
4747
final FormFieldSetter onSaved;
4848
final VoidCallback onTap;
49+
final ToolbarOptions toolbarOptions;
50+
final SmartQuotesType smartQuotesType;
51+
final SmartDashesType smartDashesType;
52+
final ScrollPhysics scrollPhysics;
53+
final bool enableSuggestions;
4954

5055
FormBuilderTextField({
5156
Key key,
@@ -89,6 +94,11 @@ class FormBuilderTextField extends StatefulWidget {
8994
this.showCursor,
9095
this.onSaved,
9196
this.onTap,
97+
this.toolbarOptions,
98+
this.smartQuotesType,
99+
this.smartDashesType,
100+
this.scrollPhysics,
101+
this.enableSuggestions = true,
92102
}) : assert(initialValue == null || controller == null),
93103
super(key: key);
94104

@@ -116,10 +126,6 @@ class FormBuilderTextFieldState extends State<FormBuilderTextField> {
116126
} else {
117127
_effectiveController.text = _initialValue ?? '';
118128
}
119-
120-
_effectiveController.addListener(() {
121-
widget.onChanged?.call(_effectiveController.text);
122-
});
123129
super.initState();
124130
}
125131

@@ -148,6 +154,14 @@ class FormBuilderTextFieldState extends State<FormBuilderTextField> {
148154
enabled: !_readOnly,
149155
),
150156
autovalidate: widget.autovalidate ?? false,
157+
onChanged: (val) {
158+
widget.onChanged?.call(_effectiveController.text);
159+
},
160+
toolbarOptions: widget.toolbarOptions,
161+
smartQuotesType: widget.smartQuotesType,
162+
smartDashesType: widget.smartDashesType,
163+
scrollPhysics: widget.scrollPhysics,
164+
enableSuggestions: widget.enableSuggestions,
151165
// initialValue: "${_initialValue ?? ''}",
152166
maxLines: widget.maxLines,
153167
keyboardType: widget.keyboardType,

0 commit comments

Comments
 (0)