Skip to content

Pressing a suffix icon button on a TextField fires both the TextField-onTap and IconButton-onPressed callbacks #118340

@Renzo-Olivares

Description

@Renzo-Olivares

Pressing an IconButton that was added to a TextField through InputDecoration, fires both an onPressed callback from the IconButton as well as the onTap callback from the TextField. Only the onPressed callback from the IconButton should be fired.

Code sample:

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flutter code sample for Material Design 3 [TextField]s.

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const TextFieldExamplesApp());
}

class TextFieldExamplesApp extends StatelessWidget {
  const TextFieldExamplesApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true),
      home: Scaffold(
        appBar: AppBar(title: const Text('TextField Examples')),
        body: Column(
          children: <Widget>[
            Spacer(),
            SizedBox(
              width: 500,
              child: TextField(
                  onTap: () {
                    print(StackTrace.current);
                    print('onTap is called');
                  },
                  decoration: InputDecoration(
                    enabled: true,
                    label: Text('label'),
                    hintText: '',
                    suffixIcon: Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 4.0),
                      child: IconButton(
                        icon: const Icon(Icons.arrow_drop_down),
                        selectedIcon: const Icon(Icons.arrow_drop_up),
                        onPressed: () {
                          print('onPressed is called');
                          // _textFocusNode.requestFocus();
                          // handlePressed(controller);
                        },
                      ),
                    ),
                  )
              ),
            ),
            Spacer(),
          ],
        ),
      ),
    );
  }
}

Steps to reproduce:

  1. Tap drop down arrow at the end of the text field.
  2. Check logs for debug print statements.

Expected: Only onPressed is fired.
Actual: onTap and onPressed is fired.

Metadata

Metadata

Labels

a: text inputEntering text in a text field or keyboard related problemsc: regressionIt was better in the past than it is now

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions