Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add {value_length}, {max_length}, and {symbols_left} placeholders to TextField.counter_text #4403

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/flet/lib/src/controls/textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ class _TextFieldControlState extends State<TextFieldControl>
}
var fitParentSize = widget.control.attrBool("fitParentSize", false)!;

var maxLength = widget.control.attrInt("maxLength");

Widget textField = TextFormField(
style: textStyle,
autofocus: autofocus,
Expand All @@ -234,6 +236,8 @@ class _TextFieldControlState extends State<TextFieldControl>
helper: helperCtrl.isNotEmpty ? helperCtrl.first : null,
label: labelCtrl.isNotEmpty ? labelCtrl.first : null,
customSuffix: revealPasswordIcon,
valueLength: _value.length,
maxLength: maxLength,
focused: _focused,
disabled: disabled,
adaptive: adaptive),
Expand Down Expand Up @@ -261,7 +265,7 @@ class _TextFieldControlState extends State<TextFieldControl>
widget.control.attrString("textAlign"), TextAlign.start)!,
minLines: fitParentSize ? null : minLines,
maxLines: fitParentSize ? null : maxLines,
maxLength: widget.control.attrInt("maxLength"),
maxLength: maxLength,
readOnly: widget.control.attrBool("readOnly", false)!,
inputFormatters: inputFormatters.isNotEmpty ? inputFormatters : null,
obscureText: password && !_revealPassword,
Expand Down
11 changes: 10 additions & 1 deletion packages/flet/lib/src/utils/form_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ InputDecoration buildInputDecoration(BuildContext context, Control control,
Control? helper,
Control? label,
Widget? customSuffix,
int? valueLength,
int? maxLength,
bool focused = false,
bool disabled = false,
bool? adaptive}) {
Expand Down Expand Up @@ -98,6 +100,13 @@ InputDecoration buildInputDecoration(BuildContext context, Control control,
var borderWidth = control.attrDouble("borderWidth");
var focusedBorderWidth = control.attrDouble("focusedBorderWidth");

var counterText = control
.attrString("counterText", "")
?.replaceAll("{value_length}", valueLength.toString())
.replaceAll("{max_length}", maxLength?.toString() ?? "None")
.replaceAll("{symbols_left}",
"${maxLength == null ? 'None' : (maxLength - (valueLength ?? 0))}");

InputBorder? border;
if (inputBorder == FormFieldInputBorder.underline) {
border = const UnderlineInputBorder();
Expand Down Expand Up @@ -163,7 +172,7 @@ InputDecoration buildInputDecoration(BuildContext context, Control control,
hintStyle: parseTextStyle(Theme.of(context), control, "hintStyle"),
helperText: control.attrString("helperText"),
helperStyle: parseTextStyle(Theme.of(context), control, "helperStyle"),
counterText: control.attrString("counterText"),
counterText: counterText,
counterStyle: parseTextStyle(Theme.of(context), control, "counterStyle"),
counter: counter != null
? createControl(control, counter.id, control.isDisabled,
Expand Down
1 change: 1 addition & 0 deletions sdk/python/packages/flet/src/flet/core/dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def __init__(
self.max_menu_height = max_menu_height
self.select_icon_size = select_icon_size or icon_size
self.select_icon_enabled_color = select_icon_enabled_color or icon_enabled_color
self.icon_disabled_color = icon_disabled_color
self.select_icon_disabled_color = (
select_icon_disabled_color or icon_disabled_color
)
Expand Down