From 6aa98fb0c6ae0b89f99bc1916a6d5fafc929befe Mon Sep 17 00:00:00 2001 From: Alexandru Mariuti Date: Mon, 18 Nov 2024 10:52:09 +0100 Subject: [PATCH] fix/max-length and max-length-enforcement on shad-input --- CHANGELOG.md | 2 +- lib/src/components/input.dart | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d74dd23b..92bc008f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## 0.16.0 - **FEAT**: New `ShadTimePicker` and `ShadDatePickerFormField` components. - +- **FIX**: `maxLength` and `maxLengthEnforcement` not working on `ShadInput` - **CHORE**: Set minimum Flutter version to `3.24.0` - **CHORE**: Remove `trackColor` from `ShadSwitch` (thanks to @RaghavTheGreat) diff --git a/lib/src/components/input.dart b/lib/src/components/input.dart index ad662803..78916d1b 100644 --- a/lib/src/components/input.dart +++ b/lib/src/components/input.dart @@ -408,6 +408,19 @@ class ShadInputState extends State ); }; + final effectiveMaxLengthEnforcement = widget.maxLengthEnforcement ?? + LengthLimitingTextInputFormatter.getDefaultMaxLengthEnforcement( + Theme.of(context).platform); + + final effectiveInputFormatters = [ + ...?widget.inputFormatters, + if (widget.maxLength != null) + LengthLimitingTextInputFormatter( + widget.maxLength, + maxLengthEnforcement: effectiveMaxLengthEnforcement, + ), + ]; + return ShadDisabled( disabled: !widget.enabled, child: _selectionGestureDetectorBuilder.buildGestureDetector( @@ -512,7 +525,8 @@ class ShadInputState extends State onSubmitted: widget.onSubmitted, onAppPrivateCommand: widget.onAppPrivateCommand, - inputFormatters: widget.inputFormatters, + inputFormatters: + effectiveInputFormatters, cursorWidth: widget.cursorWidth, cursorHeight: widget.cursorHeight, cursorRadius: widget.cursorRadius,