Skip to content

Commit ccbf988

Browse files
feat: WIP remove decoration from default form field
1 parent cb2a1eb commit ccbf988

File tree

4 files changed

+88
-30
lines changed

4 files changed

+88
-30
lines changed

lib/src/fields/form_builder_slider.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
import 'package:flutter/material.dart';
2-
32
import 'package:intl/intl.dart';
4-
53
import 'package:flutter_form_builder/flutter_form_builder.dart';
64

7-
/// Configuration to what values show on sliders
8-
///
9-
/// - `all`: Show all values
10-
/// - `current`: Show only the current value (middle)
11-
/// - `minMax`: Show only the min and max values (start and finish)
12-
/// - `none`: No show any values
13-
enum DisplayValues { all, current, minMax, none }
14-
155
/// Field for selection of a numerical value on a slider
166
class FormBuilderSlider extends FormBuilderField<double> {
177
/// Called when the user starts selecting a new value for the slider.

lib/src/fields/form_builder_text_field.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:flutter/services.dart';
66
import 'package:flutter_form_builder/flutter_form_builder.dart';
77

88
/// A Material Design text field input.
9-
class FormBuilderTextField extends FormBuilderField<String> {
9+
class FormBuilderTextField extends FormBuilderFieldDecoration<String> {
1010
/// Controls the text being edited.
1111
///
1212
/// If null, this widget will create its own [TextEditingController].
@@ -271,13 +271,14 @@ class FormBuilderTextField extends FormBuilderField<String> {
271271
///{@macro flutter.widgets.text_selection.TextMagnifierConfiguration.details}
272272
final TextMagnifierConfiguration? magnifierConfiguration;
273273

274+
/// By default `false`
275+
final bool readOnly;
276+
274277
/// Creates a Material Design text field input.
275278
FormBuilderTextField({
276279
super.key,
277280
required super.name,
278281
super.validator,
279-
String? initialValue,
280-
bool readOnly = false,
281282
super.decoration,
282283
super.onChanged,
283284
super.valueTransformer,
@@ -287,6 +288,8 @@ class FormBuilderTextField extends FormBuilderField<String> {
287288
super.onReset,
288289
super.focusNode,
289290
super.restorationId,
291+
String? initialValue,
292+
this.readOnly = false,
290293
this.maxLines = 1,
291294
this.obscureText = false,
292295
this.textCapitalization = TextCapitalization.none,
@@ -348,8 +351,6 @@ class FormBuilderTextField extends FormBuilderField<String> {
348351
initialValue: controller != null ? controller.text : initialValue,
349352
builder: (FormFieldState<String?> field) {
350353
final state = field as _FormBuilderTextFieldState;
351-
/*final effectiveDecoration = (decoration ?? const InputDecoration())
352-
.applyDefaults(Theme.of(field.context).inputDecorationTheme);*/
353354

354355
return TextField(
355356
restorationId: restorationId,
@@ -414,12 +415,12 @@ class FormBuilderTextField extends FormBuilderField<String> {
414415
}
415416

416417
@override
417-
FormBuilderFieldState<FormBuilderTextField, String> createState() =>
418+
FormBuilderFieldDecorationState<FormBuilderTextField, String> createState() =>
418419
_FormBuilderTextFieldState();
419420
}
420421

421422
class _FormBuilderTextFieldState
422-
extends FormBuilderFieldState<FormBuilderTextField, String> {
423+
extends FormBuilderFieldDecorationState<FormBuilderTextField, String> {
423424
TextEditingController? get _effectiveController =>
424425
widget.controller ?? _controller;
425426

lib/src/form_builder_field.dart

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class FormBuilderField<T> extends FormField<T> {
3838
/// Called when the field value is changed.
3939
final ValueChanged<T?>? onChanged;
4040

41-
/// The border, labels, icons, and styles used to decorate the field.
42-
final InputDecoration decoration;
41+
// /// The border, labels, icons, and styles used to decorate the field.
42+
// final InputDecoration decoration;
4343

4444
/// Called when the field value is reset.
4545
final VoidCallback? onReset;
@@ -60,11 +60,27 @@ class FormBuilderField<T> extends FormField<T> {
6060
required this.name,
6161
this.valueTransformer,
6262
this.onChanged,
63-
this.decoration = const InputDecoration(),
6463
this.onReset,
6564
this.focusNode,
6665
});
6766

67+
const factory FormBuilderField.decoration({
68+
Key? key,
69+
void Function(T?)? onSaved,
70+
T? initialValue,
71+
AutovalidateMode? autovalidateMode,
72+
bool enabled,
73+
String? Function(T?)? validator,
74+
String? restorationId,
75+
required Widget Function(FormFieldState<T>) builder,
76+
required String name,
77+
ValueTransformer<T?>? valueTransformer,
78+
ValueChanged<T?>? onChanged,
79+
VoidCallback? onReset,
80+
FocusNode? focusNode,
81+
InputDecoration decoration,
82+
}) = FormBuilderFieldDecoration;
83+
6884
@override
6985
FormBuilderFieldState<FormBuilderField<T>, T> createState() =>
7086
FormBuilderFieldState<FormBuilderField<T>, T>();
@@ -98,12 +114,10 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T>
98114
String? get errorText => super.errorText ?? _customErrorText;
99115

100116
@override
101-
bool get hasError =>
102-
super.hasError || decoration.errorText != null || errorText != null;
117+
bool get hasError => super.hasError || errorText != null;
103118

104119
@override
105-
bool get isValid =>
106-
super.isValid && decoration.errorText == null && errorText == null;
120+
bool get isValid => super.isValid && errorText == null;
107121

108122
bool get enabled => widget.enabled && (_formBuilderState?.enabled ?? true);
109123
bool get _readOnly => !(_formBuilderState?.widget.skipDisabled ?? false);
@@ -124,12 +138,12 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T>
124138
/// The field is focused by user or by logic code
125139
bool get isTouched => _touched;
126140

127-
InputDecoration get decoration => widget.decoration.copyWith(
128-
errorText: widget.enabled || _readOnly
129-
? widget.decoration.errorText ?? errorText
130-
: null,
131-
enabled: widget.enabled || _readOnly,
132-
);
141+
// InputDecoration get decoration => widget.decoration.copyWith(
142+
// errorText: widget.enabled || _readOnly
143+
// ? widget.decoration.errorText ?? errorText
144+
// : null,
145+
// enabled: widget.enabled || _readOnly,
146+
// );
133147

134148
void registerTransformer(Map<String, Function> map) {
135149
final fun = widget.valueTransformer;
@@ -298,3 +312,47 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T>
298312
Scrollable.ensureVisible(context);
299313
}
300314
}
315+
316+
class FormBuilderFieldDecoration<T> extends FormBuilderField<T> {
317+
const FormBuilderFieldDecoration({
318+
super.key,
319+
super.onSaved,
320+
super.initialValue,
321+
super.autovalidateMode,
322+
super.enabled = true,
323+
super.validator,
324+
super.restorationId,
325+
required super.name,
326+
super.valueTransformer,
327+
super.onChanged,
328+
super.onReset,
329+
super.focusNode,
330+
required super.builder,
331+
this.decoration = const InputDecoration(),
332+
});
333+
final InputDecoration decoration;
334+
335+
@override
336+
FormBuilderFieldDecorationState<FormBuilderFieldDecoration<T>, T>
337+
createState() =>
338+
FormBuilderFieldDecorationState<FormBuilderFieldDecoration<T>, T>();
339+
}
340+
341+
class FormBuilderFieldDecorationState<F extends FormBuilderFieldDecoration<T>,
342+
T> extends FormBuilderFieldState<FormBuilderField<T>, T> {
343+
@override
344+
F get widget => super.widget as F;
345+
346+
InputDecoration get decoration => widget.decoration.copyWith(
347+
errorText: widget.enabled || _readOnly
348+
? widget.decoration.errorText ?? errorText
349+
: null,
350+
enabled: widget.enabled || _readOnly,
351+
);
352+
353+
@override
354+
bool get hasError => super.hasError || widget.decoration.errorText != null;
355+
356+
@override
357+
bool get isValid => super.isValid && widget.decoration.errorText == null;
358+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'package:flutter/material.dart';
2+
3+
mixin FormBuilderInputDecoration {
4+
final InputDecoration decoration = const InputDecoration();
5+
6+
InputDecoration get getdecoration => decoration.copyWith();
7+
8+
bool get hasDecorationError => decoration.errorText != null;
9+
}

0 commit comments

Comments
 (0)