Repository navigation
Taps on TextField prefix/suffix #39376
Description
I'm experiencing weird issues with the latest stable version of Flutter: 1.7.8+hotfix.4.
I have a simple textfield widget that has an onTap() action. The textfield widget also has a prefix icon which has its own onTap() action. When I press the prefix icon, it completes everything inside the prefix onTap() but it also then goes and completes everything in the textfield onTap().
I've been able to replicate it with the below code. Both onTap methods print outputs to the console. When the prefix icon is clicked, it is outputting the icon onTap as well as the textfield onTap actions.
Replicated in below code
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void _incrementCounter() {
setState(() {
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
color: Colors.red,
child: TextField(
onTap: () {
print('Hello this is a print out');
},
decoration: InputDecoration(
hintText: 'Hint text',
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 25.0, top: 16.0),
prefixIcon: IconButton(
icon: Icon(Icons.hotel, color: Colors.green, size: 40.0,),
onPressed: () {
print("Hi this is a second print out");
},
),
),
),
)
],
),
),
);
}
}
Flutter Doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.14.6 18G87, locale en-NZ)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 10.3)
[✓] iOS tools - develop for iOS devices
[✓] Android Studio (version 3.4)
[✓] VS Code (version 1.37.1)
[✓] Connected device (1 available)
• No issues found!
I have switched back to the master channel in the mean time to get around this issue but would be good to have it fixed.
Thanks.
Activity
MRL-00 commentedon Sep 24, 2019
@HansMuller @mikhail-tokarev Hey guys, is there any idea on when this might be fixed? It is also happening now on the 1.9.1+hotfix 2 version.
HansMuller commentedon Sep 24, 2019
CC @justinmc
justinmc commentedon Sep 24, 2019
There is a discussion at the end of #36324 about this problem, and a work in progress PR to fix it #38722. I'm hoping to get some time to finish that up soon.
JZ-at-TP commentedon Oct 8, 2019
Any update on this? I'de love to update my Flutter version but can't with this outstanding.
justinmc commentedon Oct 9, 2019
@JZ-at-TP I haven't had time to circle back to that WIP PR, and I won't for at least a week or two. If this is a blocker for you (or anyone else reading this), and you want to take over the PR yourself, I would be happy to help you through it. Otherwise I will hopefully be pushing commits to that PR in a few weeks to finish it up.
MRL-00 commentedon Oct 11, 2019
Hey @justinmc I would be happy to look at doing the PR. Can you point me in the right direction on where to look? You can reach me at [email protected] if you want to touch base via email.
justinmc commentedon Oct 11, 2019
@MattL-NZ Thanks so much! I'll email you with some details.
[-]Flutter Stable Version 1.7.8+hotfix.4 issue with TextField[/-][+]Taps on TextField prefix/suffix[/+]MRL-00 commentedon Jan 21, 2020
Hi @justinmc, just thought i would check in and see how you are progressing with this issue? Have you been able to get something working?
justinmc commentedon Jan 22, 2020
Sorry, I haven't had time to finish #38722, but I plan to revisit it when I can!
Lecarvalho commentedon Feb 2, 2020
Do we have a workaround for instant?
8 remaining items
MarsGoatz commentedon Dec 26, 2020
TextField working properly without bugs is bare minimum TBH and writing hacks for these bare minimum widgets is just another level crazy.
Hoping a fix will be out soon!
ilkercankaya commentedon Jan 15, 2021
Instead of giving 100ms as delay, try
It seems to work and is a smaller window.
drekka commentedon Jan 18, 2021
Not sure if this will help, but here's what I ended up with to stop the field from taking focus when I tap a suffix button.
I did it by creating an extension which I used to generate input decorations. When a suffix button is tapped, it deactivates the ability of field to accept focus, then calls the action, passing it a closure to restore the field's focus ability. This allows the button to perform any amount of async work before calling the completion.
bayramaliunuvar commentedon Mar 12, 2021
all you have to do is define a variable.
bool vadeTarihiClear=false;SuffixIcon
decoration: InputDecoration( suffixIcon: IconButton( icon: Icon(clearButtonIcon), onPressed: () { vadeTarihiClear = true; Future.delayed(Duration(milliseconds: 100), () { vadeTarihiClear = false; }); })),TextField ontap
onTap: () async { if (vadeTarihiClear) return;milliseconds: 100 enough
drekka commentedon Mar 23, 2021
I learned long ago o never trust code that relies on time. Too many bugs come out of it and sometimes some bad user experiences. Not saying that won’t work. It’s just been my experience to avoid such code.
LongCatIsLooong commentedon Mar 24, 2021
internal link: b/183372823.
cpboyd commentedon Apr 29, 2021
This also applies to other uses of touchable components on top of widgets that utilize
onTapDown, e.g.CupertinoContextMenu.Putting an
IconButtonon top will result in theCupertinoContextMenu's internalonTapDownstill triggering.wwwqyhme commentedon May 24, 2021
LongCatIsLooong commentedon May 26, 2021
@justinmc @chunhtai as I understand it the tap "leaks" because of the use of the
_TransparentTapGestureRecognizerin the text field right? The text field wants to handle double/triple taps but single taps should be handled by the selection handle, but as a result taps that landed on something other than the selection handle are now leaking to the text field as well.After testing on my android devices and iphone I have a different interpretation of the tap gesture:
I tested this on a simulator so I can precisely land my taps on the tiny selection handle. It turns out the current behavior might result from less accurate pointer devices
so it does not yield the pointer to the text field when the gesture is a double tap.
So I guess we can retire the
_TransparentTapGestureRecognizerclass.And maybe deprecate the(can't deprecate that. Android still uses it to toggle the toolbar).onSelectionHandleTappedcallback onEditableTexts, etc. tooI think one of the main challenges to implement the iOS selection highlight gesture behavior would be that if the entire document is selected the user won't be able to tap somewhere that doesn't have a glyph to collapse the selection to a caret (since the current API only tells us the text position to the closest glyph:
GetGlyphPositionAtCoordinatealways return a valid position in the text even if the given coordinate is far away from every glyph).EDIT
It seems I was wrong about iOS selection handle not handling taps. I did the test again and found out they DO handle taps in aUITextField, but do not in aUITextView.Neither
UITextViewnorUITextFieldhandles taps on the selection handle.UITextFields just have a large effective "selectionRect" (larger than the visually highlighted area) so the taps are actually handled by the selection rect. AndUITextViews simply do not toggle the text selection toolbar when the user taps on the selected area (so the "selection rect" behavior above only applies toUITextField).LongCatIsLooong commentedon May 26, 2021
This has caused several really annoying issues (e.g., #78918), and I'd like to fix this before continuing with #79414 (it's difficult to port some of the hacks to the new implementation). I'm going to work on it ASAP.
github-actions commentedon Jul 31, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.