TextField controller.clear() doesn't actually clear the TextField

3,690

Sounds like a bug. Would you be willing to file the bug at https://github.com/flutter/flutter/issues/new ?

Share:
3,690
Rainer Wittmann
Author by

Rainer Wittmann

Updated on December 02, 2022

Comments

  • Rainer Wittmann
    Rainer Wittmann over 1 year

    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)

      • Flutter at /Users/rainerwittmann/Flutter/flutter
      • Framework revision 0d402242c5 (5 hours ago), 2017-04-03 22:50:30
      • Engine revision 878f0ff09d
      • Tools Dart version 1.23.0-dev.10.0
    • [✓] Host Executable Compatibility

      • Downloaded executables execute on host
    • [✓] Android toolchain - develop for Android devices (Android SDK 25.0.2)

      • Android SDK at /usr/local/opt/android-sdk
      • Platform android-25, build-tools 25.0.2
      • ANDROID_HOME = /usr/local/opt/android-sdk
      • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
      • Java version: OpenJDK Runtime Environment (build 1.8.0_112-release-b05)
    • [✓] iOS toolchain - develop for iOS devices (Xcode 8.3)

      • Xcode at /Applications/Xcode.app/Contents/Developer
      • Xcode 8.3, Build version 8E162
      • ios-deploy 1.9.0
      • CocoaPods version 1.1.1
    • [✓] Android Studio (version 2.2)

      • Android Studio at /Applications/Android Studio.app/Contents
      • Gradle version 2.14.1
      • Java version: OpenJDK Runtime Environment (build 1.8.0_112-release-b05)
    • [✓] IntelliJ IDEA Community Edition (version 2017.1)

      • Dart plugin version 171.4006
      • Flutter plugin version 12.0
    • [✓] Connected devices

      • iPhone 7 • 59820DDC-6A72-475E-84C0-A2E837F62572 • ios • iOS 10.3 (simulator)