Skip to content

Commit 5b5c3b9

Browse files
authored
[material_ui] Hit test the floating label of InputDecorator at its painted position (flutter#13326)
When the label of an `InputDecorator` floats, `_RenderDecoration.paint` draws the label with `_labelTransform` (a translation and a scale of 0.75). `applyPaintTransform` reports the same transform. But `hitTestChildren` hit tests the label with only its layout offset. A comment there says "The label must be handled specially since we've transformed it", but the code does not do this. Thus the label is painted in one location and hit tested in a different location: - A tap on the visible floating label does not hit the label. - A tap below the visible label, inside its unscaled layout box, hits the label. This is a problem when the label contains an interactive widget, for example a `PopupMenuButton`, as in the issue. A maintainer said in the issue: "we shouldn't have weird issues with the hit testing here. Flutter should be able to do this" ([comment](flutter/flutter#145324 (comment))). This change hit tests the label with the transform from `applyPaintTransform` when `_labelTransform` is set. The hit test order of the children does not change. Other children keep the offset-based hit test. There is no API change. The code in `packages/flutter/lib/src/material` of flutter/flutter is frozen (see flutter/flutter#188444), so this change is only for `material_ui`. Fixes flutter/flutter#145324 ## Test plan - Added `Floating label hit test matches its painted position` to `test/input_decorator_test.dart`, for `TextDirection.ltr` and `TextDirection.rtl`. The test taps the painted label two times (2 taps expected), then taps 10 px below the painted label, inside its layout box (no new tap expected). - Without the fix: both cases fail with `Expected: <2> Actual: <0>`. - With the fix: both cases pass. ## Pre-Review Checklist The change uses the repository's pending changelog workflow (`packages/material_ui/pending_changelogs/change_2026_10_20_floating_label_hit_test.yaml`, `version: patch`). No public API documentation change is necessary. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent f631bb8 commit 5b5c3b9

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎packages/material_ui/lib/src/input_decorator.dart‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,20 @@ class _RenderDecoration extends RenderBox
17281728
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) {
17291729
for (final RenderBox child in children) {
17301730
// The label must be handled specially since we've transformed it.
1731+
if (child == label && _labelTransform != null) {
1732+
final transform = Matrix4.identity();
1733+
applyPaintTransform(child, transform);
1734+
if (result.addWithPaintTransform(
1735+
transform: transform,
1736+
position: position,
1737+
hitTest: (BoxHitTestResult result, Offset transformed) {
1738+
return child.hitTest(result, position: transformed);
1739+
},
1740+
)) {
1741+
return true;
1742+
}
1743+
continue;
1744+
}
17311745
final Offset offset = _boxParentData(child).offset;
17321746
final bool isHit = result.addWithPaintOffset(
17331747
offset: offset,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes `InputDecorator` hit testing of a floating label, so taps hit the label where it is painted.
3+
version: patch

‎packages/material_ui/test/input_decorator_test.dart‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16208,4 +16208,40 @@ void main() {
1620816208
final Text label = tester.widget<Text>(find.text(labelText));
1620916209
expect(label.textDirection, TextDirection.rtl);
1621016210
});
16211+
16212+
// Regression test for https://github.com/flutter/flutter/issues/145324.
16213+
for (final TextDirection textDirection in TextDirection.values) {
16214+
testWidgets('Floating label hit test matches its painted position ($textDirection)', (
16215+
WidgetTester tester,
16216+
) async {
16217+
var tapCount = 0;
16218+
await tester.pumpWidget(
16219+
buildInputDecorator(
16220+
textDirection: textDirection,
16221+
decoration: InputDecoration(
16222+
border: const OutlineInputBorder(),
16223+
label: GestureDetector(
16224+
behavior: HitTestBehavior.opaque,
16225+
onTap: () => tapCount++,
16226+
child: const SizedBox(width: 100.0, height: 20.0),
16227+
),
16228+
),
16229+
),
16230+
);
16231+
16232+
// The label floats (isEmpty is false), so it is painted scaled and moved up
16233+
// onto the border, above its layout position.
16234+
final Rect labelRect = tester.getRect(find.byType(GestureDetector));
16235+
expect(labelRect.width, 75.0);
16236+
16237+
// Taps at the painted label hit the label.
16238+
await tester.tapAt(labelRect.center);
16239+
await tester.tapAt(labelRect.bottomLeft + const Offset(1.0, -1.0));
16240+
expect(tapCount, 2);
16241+
16242+
// A tap below the painted label, inside its layout position, misses the label.
16243+
await tester.tapAt(labelRect.bottomCenter + const Offset(0.0, 10.0));
16244+
expect(tapCount, 2);
16245+
});
16246+
}
1621116247
}

0 commit comments

Comments
 (0)