Skip to content

Commit baf6ed3

Browse files
committed
Restore support for custom prefix and suffix
1 parent 07d16c8 commit baf6ed3

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

lib/src/material/spin_box.dart

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ class SpinBox extends BaseSpinBox {
8585
this.afterChange,
8686
this.focusNode,
8787
}) : assert(min <= max),
88-
assert(decoration?.prefixIcon == null,
89-
'InputDecoration.prefixIcon is reserved for SpinBox decrement icon'),
90-
assert(decoration?.suffixIcon == null,
91-
'InputDecoration.suffixIcon is reserved for SpinBox increment icon'),
9288
keyboardType = keyboardType ??
9389
TextInputType.numberWithOptions(
9490
signed: min < 0,
@@ -353,15 +349,30 @@ class _SpinBoxState extends BaseSpinBoxState<SpinBox> {
353349
decoration.disabledBorder != null ||
354350
decoration.focusedErrorBorder != null;
355351

352+
Widget buildIcon(
353+
Icon icon, {
354+
Widget? prefix,
355+
Widget? suffix,
356+
}) {
357+
return Row(
358+
mainAxisSize: MainAxisSize.min,
359+
children: [
360+
if (suffix != null) suffix,
361+
if (isHorizontal && widget.showButtons)
362+
Padding(
363+
padding: EdgeInsets.symmetric(horizontal: widget.spacing),
364+
child: Icon(null, size: icon.size),
365+
),
366+
if (prefix != null) prefix,
367+
],
368+
);
369+
}
370+
356371
final inputDecoration = decoration.copyWith(
357372
border: !hasAnyBorder ? const OutlineInputBorder() : decoration.border,
358373
errorText: errorText,
359-
prefix: isHorizontal && widget.showButtons
360-
? Icon(null, size: widget.decrementIcon.size)
361-
: null,
362-
suffix: isHorizontal && widget.showButtons
363-
? Icon(null, size: widget.incrementIcon.size)
364-
: null,
374+
prefix: buildIcon(widget.decrementIcon, prefix: decoration.prefix),
375+
suffix: buildIcon(widget.incrementIcon, suffix: decoration.suffix),
365376
);
366377

367378
final textField = CallbackShortcuts(
@@ -418,14 +429,20 @@ class _SpinBoxState extends BaseSpinBoxState<SpinBox> {
418429
bottom: bottom,
419430
child: Align(
420431
alignment: Alignment.centerLeft,
421-
child: decrementButton,
432+
child: Padding(
433+
padding: EdgeInsets.symmetric(horizontal: widget.spacing),
434+
child: decrementButton,
435+
),
422436
),
423437
),
424438
Positioned.fill(
425439
bottom: bottom,
426440
child: Align(
427441
alignment: Alignment.centerRight,
428-
child: incrementButton,
442+
child: Padding(
443+
padding: EdgeInsets.symmetric(horizontal: widget.spacing),
444+
child: incrementButton,
445+
),
429446
),
430447
)
431448
],

test/material_spinbox_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,49 @@ void main() {
260260
expect(decoration, isNotNull);
261261
expect(decoration!.labelText, equals('custom'));
262262
});
263+
264+
testWidgets('prefix', (tester) async {
265+
await tester.pumpWidget(
266+
TestApp(
267+
widget: SpinBox(
268+
spacing: 25,
269+
decoration: const InputDecoration(prefix: Text('prefix')),
270+
),
271+
),
272+
);
273+
274+
final decrement = find.byIcon(TestIcons.decrement);
275+
expect(decrement, findsOneWidget);
276+
277+
final prefix = find.text('prefix');
278+
expect(prefix, findsOneWidget);
279+
280+
expect(
281+
tester.getRect(prefix).left,
282+
greaterThanOrEqualTo(tester.getRect(decrement).right + 25),
283+
);
284+
});
285+
286+
testWidgets('suffix', (tester) async {
287+
await tester.pumpWidget(
288+
TestApp(
289+
widget: SpinBox(
290+
spacing: 25,
291+
decoration: const InputDecoration(suffix: Text('suffix')),
292+
),
293+
),
294+
);
295+
296+
final increment = find.byIcon(TestIcons.increment);
297+
expect(increment, findsOneWidget);
298+
299+
final suffix = find.text('suffix');
300+
expect(suffix, findsOneWidget);
301+
302+
expect(
303+
tester.getRect(suffix).right,
304+
lessThanOrEqualTo(tester.getRect(increment).left - 25),
305+
);
306+
});
263307
});
264308
}

0 commit comments

Comments
 (0)