I have some difficulties implementing a TextField. More specifically I tried to use a controller to clear the TextField on the onPress method of a button. controller.clear() removes the text from The TextField but once you continue typing, the old value reappears.
Reproduce (using the flutter-gallery):
Modify lib/demo/material/date_and_time_picker_demo.dart to add a controller to one of the TextFields:
Add the following to the beginning of class _DateAndTimePickerDemoState (line 124 in my case):
class _DateAndTimePickerDemoState extends State<DateAndTimePickerDemo> {
++ TextEditingController _controller = new TextEditingController();
DateTime _fromDate = new DateTime.now();
...
Add following line to the first TextField (line 146 in my case):
new TextField(
++ controller: _controller,
decoration: const InputDecoration(
labelText: 'Event name',
),
style: Theme.of(context).textTheme.display1,
),
Add a new FlatButton as last child to to Column (line 211 - 215 in my case):
new FlatButton(
onPressed: () {
_controller.clear();
},
child: new Text('Clear'),
)
Now go to the Date and time pickers example in the gallery, type something in the Event Name TextField, press Clear and continue typing. Once you pressed Clear, the value is removed from the TextField. The value reapers once you continue typing.
I worked around this issue by always creating a new TextEditingController when I wanted to clear the TextField. This works, but doesn't seem to be the intended way of doing it.
Can you help?
PS: >flutter doctor:
[✓] Flutter (on Mac OS X 10.12.3 16D32, channel master)
[✓] Host Executable Compatibility
[✓] Android toolchain - develop for Android devices (Android SDK 25.0.2)
[✓] iOS toolchain - develop for iOS devices (Xcode 8.3)
[✓] Android Studio (version 2.2)
[✓] IntelliJ IDEA Community Edition (version 2017.1)
[✓] Connected devices
Sounds like a bug. Would you be willing to file the bug at https://github.com/flutter/flutter/issues/new ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With