Skip to content

Commit 81c3b97

Browse files
Merge pull request #1233 from flutter-form-builder-ecosystem/feature-1207
feat: add custom widgets to legend slider
2 parents 76a1074 + 53d9ab6 commit 81c3b97

File tree

2 files changed

+118
-20
lines changed

2 files changed

+118
-20
lines changed

lib/src/fields/form_builder_range_slider.dart

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,48 @@ class FormBuilderRangeSlider extends FormBuilderField<RangeValues> {
118118
/// When used [maxValueWidget] will override the value for the maximum widget.
119119
final Widget Function(String max)? maxValueWidget;
120120

121-
final DisplayValues displayValues;
121+
/// Provides the ability to format a number in a locale-specific way.
122+
///
123+
/// The format is specified as a pattern using a subset of the ICU formatting
124+
/// patterns.
125+
///
126+
/// - `0` A single digit
127+
/// - `#` A single digit, omitted if the value is zero
128+
/// - `.` Decimal separator
129+
/// - `-` Minus sign
130+
/// - `,` Grouping separator
131+
/// - `E` Separates mantissa and expontent
132+
/// - `+` - Before an exponent, to say it should be prefixed with a plus sign.
133+
/// - `%` - In prefix or suffix, multiply by 100 and show as percentage
134+
/// - `‰ (\u2030)` In prefix or suffix, multiply by 1000 and show as per mille
135+
/// - `¤ (\u00A4)` Currency sign, replaced by currency name
136+
/// - `'` Used to quote special characters
137+
/// - `;` Used to separate the positive and negative patterns (if both present)
138+
///
139+
/// For example,
140+
///
141+
/// var f = NumberFormat("###.0#", "en_US");
142+
/// print(f.format(12.345));
143+
/// ==> 12.34
144+
///
145+
/// If the locale is not specified, it will default to the current locale. If
146+
/// the format is not specified it will print in a basic format with at least
147+
/// one integer digit and three fraction digits.
148+
///
149+
/// There are also standard patterns available via the special constructors.
150+
/// e.g.
151+
///
152+
/// var percent = NumberFormat.percentPattern("ar"); var
153+
/// eurosInUSFormat = NumberFormat.currency(locale: "en_US",
154+
/// symbol: "€");
155+
///
156+
/// There are several such constructors available, though some of them are
157+
/// limited. For example, at the moment, scientificPattern prints only as
158+
/// equivalent to "#E0" and does not take into account significant digits.
122159
final NumberFormat? numberFormat;
123160

161+
final DisplayValues displayValues;
162+
124163
/// Creates field to select a range of values on a Slider
125164
FormBuilderRangeSlider({
126165
super.key,

lib/src/fields/form_builder_slider.dart

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import 'package:intl/intl.dart';
44

55
import 'package:flutter_form_builder/flutter_form_builder.dart';
66

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
713
enum DisplayValues { all, current, minMax, none }
814

915
/// Field for selection of a numerical value on a slider
@@ -121,12 +127,68 @@ class FormBuilderSlider extends FormBuilderField<double> {
121127
/// {@macro flutter.widgets.Focus.autofocus}
122128
final bool autofocus;
123129

124-
///TODO: Add documentation
130+
/// An alternative to displaying the text value of the slider.
131+
///
132+
/// Defaults to null.
133+
///
134+
/// When used [minValueWidget] will override the value for the minimum widget.
135+
final Widget Function(String min)? minValueWidget;
136+
137+
/// An alternative to displaying the text value of the slider.
138+
///
139+
/// Defaults to null.
140+
///
141+
/// When used [valueWidget] will override the value for the selected value widget.
142+
final Widget Function(String value)? valueWidget;
143+
144+
/// An alternative to displaying the text value of the slider.
145+
///
146+
/// Defaults to null.
147+
///
148+
/// When used [maxValueWidget] will override the value for the maximum widget.
149+
final Widget Function(String max)? maxValueWidget;
150+
151+
/// Provides the ability to format a number in a locale-specific way.
152+
///
153+
/// The format is specified as a pattern using a subset of the ICU formatting
154+
/// patterns.
155+
///
156+
/// - `0` A single digit
157+
/// - `#` A single digit, omitted if the value is zero
158+
/// - `.` Decimal separator
159+
/// - `-` Minus sign
160+
/// - `,` Grouping separator
161+
/// - `E` Separates mantissa and expontent
162+
/// - `+` - Before an exponent, to say it should be prefixed with a plus sign.
163+
/// - `%` - In prefix or suffix, multiply by 100 and show as percentage
164+
/// - `‰ (\u2030)` In prefix or suffix, multiply by 1000 and show as per mille
165+
/// - `¤ (\u00A4)` Currency sign, replaced by currency name
166+
/// - `'` Used to quote special characters
167+
/// - `;` Used to separate the positive and negative patterns (if both present)
168+
///
169+
/// For example,
170+
///
171+
/// var f = NumberFormat("###.0#", "en_US");
172+
/// print(f.format(12.345));
173+
/// ==> 12.34
174+
///
175+
/// If the locale is not specified, it will default to the current locale. If
176+
/// the format is not specified it will print in a basic format with at least
177+
/// one integer digit and three fraction digits.
178+
///
179+
/// There are also standard patterns available via the special constructors.
180+
/// e.g.
181+
///
182+
/// var percent = NumberFormat.percentPattern("ar"); var
183+
/// eurosInUSFormat = NumberFormat.currency(locale: "en_US",
184+
/// symbol: "€");
185+
///
186+
/// There are several such constructors available, though some of them are
187+
/// limited. For example, at the moment, scientificPattern prints only as
188+
/// equivalent to "#E0" and does not take into account significant digits.
125189
final NumberFormat? numberFormat;
190+
126191
final DisplayValues displayValues;
127-
final TextStyle? minTextStyle;
128-
final TextStyle? textStyle;
129-
final TextStyle? maxTextStyle;
130192

131193
/// Creates field for selection of a numerical value on a slider
132194
FormBuilderSlider({
@@ -154,11 +216,11 @@ class FormBuilderSlider extends FormBuilderField<double> {
154216
this.semanticFormatterCallback,
155217
this.numberFormat,
156218
this.displayValues = DisplayValues.all,
157-
this.minTextStyle,
158-
this.textStyle,
159-
this.maxTextStyle,
160219
this.autofocus = false,
161220
this.mouseCursor,
221+
this.maxValueWidget,
222+
this.minValueWidget,
223+
this.valueWidget,
162224
}) : super(
163225
builder: (FormFieldState<double?> field) {
164226
final state = field as _FormBuilderSliderState;
@@ -196,24 +258,21 @@ class FormBuilderSlider extends FormBuilderField<double> {
196258
children: <Widget>[
197259
if (displayValues != DisplayValues.none &&
198260
displayValues != DisplayValues.current)
199-
Text(
200-
effectiveNumberFormat.format(min),
201-
style: minTextStyle ?? textStyle,
202-
),
261+
minValueWidget
262+
?.call(effectiveNumberFormat.format(min)) ??
263+
Text(effectiveNumberFormat.format(min)),
203264
const Spacer(),
204265
if (displayValues != DisplayValues.none &&
205266
displayValues != DisplayValues.minMax)
206-
Text(
207-
effectiveNumberFormat.format(field.value),
208-
style: textStyle,
209-
),
267+
valueWidget?.call(
268+
effectiveNumberFormat.format(field.value)) ??
269+
Text(effectiveNumberFormat.format(field.value)),
210270
const Spacer(),
211271
if (displayValues != DisplayValues.none &&
212272
displayValues != DisplayValues.current)
213-
Text(
214-
effectiveNumberFormat.format(max),
215-
style: maxTextStyle ?? textStyle,
216-
),
273+
maxValueWidget
274+
?.call(effectiveNumberFormat.format(max)) ??
275+
Text(effectiveNumberFormat.format(max)),
217276
],
218277
),
219278
],

0 commit comments

Comments
 (0)