Repository navigation
TextField maxLength property is not working correctly. #67236
Copy link
Copy link
Closed
Labels
a: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsfound in release: 1.22Found to occur in 1.22Found to occur in 1.22found in release: 1.23Found to occur in 1.23Found to occur in 1.23frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packages
Description
Sample app:
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: TextField(
maxLength: 5,
maxLengthEnforced: true
)
)
)
);
}
}
When I enter only letters into the TextField, the length is not restricted and I can enter an unlimited amount of characters. When I enter only numbers into the TextField, it does not display more than 5 characters (as it should) although pressing backspace is bugged when typing more than 5 numbers. Example: If you type 12345678, the TextField will show 12345 but typing backspace will do nothing 3 times as if the TextField contains 12345678 but is only showing 12345.
flutter doctor -v:
[√] Flutter (Channel stable, 1.22.0, on Microsoft Windows [Version 10.0.19041.508], locale en-CA)
• Flutter version 1.22.0 at C:\Flutter
• Framework revision d408d302e2 (4 days ago), 2020-09-29 11:49:17 -0700
• Engine revision 5babba6c4d
• Dart version 2.10.0
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at C:\Users\Matt\AppData\Local\Android\sdk
• Platform android-30, build-tools 30.0.2
• Java binary at: C:\Program Files\Android\Android Studio Canary\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b01)
• All Android licenses accepted.
[!] Android Studio (version 3.3)
• Android Studio at C:\Program Files\Android\Android Studio
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
[!] Android Studio (version 3.5)
• Android Studio at C:\Program Files\Android\Android Studio Canary
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b01)
[!] IntelliJ IDEA Community Edition (version 2019.3)
• IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.2
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
• For information about installing plugins, see
https://flutter.dev/intellij-setup/#installing-the-plugins
[√] VS Code (version 1.49.3)
• VS Code at C:\Users\Matt\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.15.0
[√] Connected device (1 available)
• ONEPLUS A6013 (mobile) • 9e76e20e • android-arm64 • Android 10 (API 29)
! Doctor found issues in 3 categories.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
a: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsfound in release: 1.22Found to occur in 1.22Found to occur in 1.22found in release: 1.23Found to occur in 1.23Found to occur in 1.23frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packages
Activity
ayaankhan98 commentedon Oct 4, 2020
i would like to work on this.
darshankawar commentedon Oct 5, 2020
Tested on latest stable and master and I do see that if
maxLengthis set totrueand is enforced, entering the letters more than max length isn't restricted and we can keep type in.also, if we enter numbers, it does restrict at the max length, but if we keep typing the number, the additional numbers won't show-up in the field, but will be treated to be deleted while pressing back or cancel button from keyboard.
67236_master.mp4.zip
67236.mp4.zip
flutter doctor -v
xclud commentedon Oct 6, 2020
#67391
tippuvenky commentedon Oct 7, 2020
#67236 any updates?
arielbh commentedon Oct 7, 2020
Make sure to verify that it's not only maxLength but any other input formatter. as in the bug I've opened #67513
Albert221 commentedon Oct 7, 2020
This bug is introduced here: 2c3652b
If you don't care about the character counter you can use this
TextInputFormatteras a temporary fix instead of themaxLengthproperty:arielbh commentedon Oct 7, 2020
@Albert221 are you positive it fixes the "Delete" behaviour?
I had a custom Text Input Formatter and it happened all the same.
Albert221 commentedon Oct 7, 2020
@arielbh no, it does not fix that one.
[-]TextField maxLength problems[/-][+]TextField maxLength property is not working correctly.[/+]blackmenthor commentedon Oct 11, 2020
Up for this one, I also experienced this, this is the Flutter doctor result:
LevonVardanyan commentedon Nov 27, 2020
maxLength and inputFormaters did not work for me, I have some workaround for this.
I. added onChanged method to the TextField like this:
onChanged: (value) { String result = value; if (result.length > 4) { result = result.substring(0, 4); } _textFieldController.text = result; _textFieldController.selection = TextSelection.fromPosition(TextPosition(offset: result.length)); }And you will not have endless calling of onChanged when you set
_textFieldController.text = result;because as documentation of onChanged says, This callback doesn't run when the TextField's text is changed programmatically, via the TextField's controller.All other ways were making endless calling of callbacks, I mean if you set controller.text from addListener method of controller, it will bring listener's endless loop call.
chrisDK1977 commentedon Dec 28, 2020
This is still not working correctly.
in both cases - using
inputFormatters: [ new LengthLimitingTextInputFormatter(20), ],or using
maxLength: 20, maxLengthEnforced: true, buildCounter: (BuildContext context, { int currentLength, int maxLength, bool isFocused }) => null,I can type in any amount of Text until the first " " (blank) leads to the behaviour or select...
I can type in:
OneTwoThreeFourFiveSixSevenEight
when I click on the text to select it or enter a whitespace the text is cutted to 20 characters:
OneTwoThreeFourFiveS
C:\SDKs\flutter\bin\flutter.bat doctor --verbose
[√] Flutter (Channel stable, 1.22.4, on Microsoft Windows [Version 10.0.19041.685], locale de-DE)
• Flutter version 1.22.4 at C:\SDKs\flutter
• Framework revision 1aafb3a (6 weeks ago), 2020-11-13 09:59:28 -0800
• Engine revision 2c956a31c0
• Dart version 2.10.4
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at C:\Users\xxx\AppData\Local\Android\sdk
• Platform android-30, build-tools 30.0.3
• Java binary at: C:\SDKs\Java\jdk-15.0.1\bin\java
• Java version Java(TM) SE Runtime Environment (build 15.0.1+9-18)
• All Android licenses accepted.
[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
[√] Connected device (2 available)
• Android SDK built for x86 (mobile) • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)
• sdk gphone x86 arm (mobile) • emulator-5556 • android-x86 • Android 11 (API 30) (emulator)
mahidul-islam commentedon Jan 5, 2021
I don't know why above solution created some problem in my app. so i modified it and working now.
xu-baolin commentedon Jan 5, 2021
Hi, everyone,
The original issue was solved with the master channel by #67892.
Before you leave a comment, please confirm whether the latest master channel can be reproduced, otherwise, please file a new issue with how to reproduce the problem to report it.