-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
I don't know if it is by design, but the input formatters set on a TextField are not applied on text inputted through the TextEditingController.text property.
It makes it possible to input invalid text (even text that is longer than the maxLength if one was set), therefore forcing us to add another layer to format the text before adding it to the TextEditingController.
Although I imagine there are cases where one might want to make a distinction between the format of text inputted by the user and text set through the controller, I felt that the inputFormatters would apply to the TextField in general, wherever the text comes from.
Even if this behavior is wanted, it would be nice to have a way to format AND set text with the TextEditingController to not have to deal with the formatting ourselves.
Steps to Reproduce
Run this sample:
code sample
class NotInputtedText extends StatefulWidget{
@override
_NotInputtedTextState createState() => _NotInputtedTextState();
}
class _NotInputtedTextState extends State<NotInputtedText> {
TextEditingController controller;
@override
void initState(){
super.initState();
controller = TextEditingController();
}
@override
void dispose(){
controller.dispose();
super.dispose();
}
void setText(){
controller.text = "This text will not stop at 6 characters, not will it be in uppercase";
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(children: <Widget>[
TextField(controller: controller, maxLength: 6, inputFormatters: [UpperCaseTextFormatter()],),
FlatButton(child: Text("Click me"), onPressed: setText,)
],),
),
);
}
}
class UpperCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return new TextEditingValue(
text: newValue.text?.toUpperCase(),
selection: newValue.selection,
);
}
}Logs
flutter doctor -v
[✓] Flutter (Channel stable, v1.2.1, on Mac OS X 10.14.3 18D109, locale en-JP)
• Flutter version 1.2.1 at /Users/guillaume/flutter
• Framework revision 8661d8aecd (7 weeks ago), 2019-02-14 19:19:53 -0800
• Engine revision 3757390fa4
• Dart version 2.1.2 (build 2.1.2-dev.0.0 0a7dcf17eb)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /Users/guillaume/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
• All Android licenses accepted.
[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 10.1, Build version 10B61
• ios-deploy 1.9.4
• CocoaPods version 1.6.0
[✓] Android Studio (version 3.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 33.4.1
• Dart plugin version 182.5215
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
[✓] VS Code (version 1.32.3)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 2.24.0
[✓] Connected device (2 available)
• Pixel 3 • 895X05QV9 • android-arm64 • Android 9 (API 28)
• iPhone XS • 46D10895-1001-4801-9F7A-D65659B06914 • ios • iOS 12.1 (simulator)
• No issues found!