Skip to content

Make CupertinoTextField call onChanged when clear button tapped #29345

@mleonhard

Description

@mleonhard

CupertinoTextField does not call onChanged when its clear button is tapped. This is a problem because my app needs to know when the user has cleared the search field so it can display unconstrained suggestions (including paid ads).

Here's a minimal reproduction. Tap the '(x)' button to clear the text box. Note that no message is printed. I tested debug builds on iOS Simulator and Android Emulator.

import 'package:flutter/cupertino.dart'
    show CupertinoApp, CupertinoPageScaffold;
import 'package:flutter/widgets.dart' show Center, runApp;
import 'package:flutter/cupertino.dart'
    show CupertinoTextField, OverlayVisibilityMode, TextEditingController;

void main() {
  runApp(
    CupertinoApp(
      home: CupertinoPageScaffold(
        child: Center(
          child: CupertinoTextField(
            controller: new TextEditingController(text: 'initial text'),
            clearButtonMode: OverlayVisibilityMode.always,
            onChanged: (String contents) {
              print('onChanged $contents');
            },
          ),
        ),
      ),
    ),
  );
}

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.2.1, on Mac OS X 10.14.3 18D109, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
[✓] Android Studio (version 3.3)
[!] IntelliJ IDEA Community Edition (version 2018.3.5)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] Connected device (2 available)

! Doctor found issues in 2 categories.

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: cupertinoflutter/packages/flutter/cupertino repositoryframeworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions