Skip to content

Commit d238ac1

Browse files
YeungKCQuncCccccc
authored andcommitted
[material_ui] Hit test the floating label of InputDecorator at its painted position
Fixes flutter/flutter#145324
1 parent a355d5d commit d238ac1

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
@@ -1727,6 +1727,20 @@ class _RenderDecoration extends RenderBox
17271727
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) {
17281728
for (final RenderBox child in children) {
17291729
// The label must be handled specially since we've transformed it.
1730+
if (child == label && _labelTransform != null) {
1731+
final transform = Matrix4.identity();
1732+
applyPaintTransform(child, transform);
1733+
if (result.addWithPaintTransform(
1734+
transform: transform,
1735+
position: position,
1736+
hitTest: (BoxHitTestResult result, Offset transformed) {
1737+
return child.hitTest(result, position: transformed);
1738+
},
1739+
)) {
1740+
return true;
1741+
}
1742+
continue;
1743+
}
17301744
final Offset offset = _boxParentData(child).offset;
17311745
final bool isHit = result.addWithPaintOffset(
17321746
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
@@ -16123,4 +16123,40 @@ void main() {
1612316123
final Text label = tester.widget<Text>(find.text(labelText));
1612416124
expect(label.textDirection, TextDirection.rtl);
1612516125
});
16126+
16127+
// Regression test for https://github.com/flutter/flutter/issues/145324.
16128+
for (final TextDirection textDirection in TextDirection.values) {
16129+
testWidgets('Floating label hit test matches its painted position ($textDirection)', (
16130+
WidgetTester tester,
16131+
) async {
16132+
var tapCount = 0;
16133+
await tester.pumpWidget(
16134+
buildInputDecorator(
16135+
textDirection: textDirection,
16136+
decoration: InputDecoration(
16137+
border: const OutlineInputBorder(),
16138+
label: GestureDetector(
16139+
behavior: HitTestBehavior.opaque,
16140+
onTap: () => tapCount++,
16141+
child: const SizedBox(width: 100.0, height: 20.0),
16142+
),
16143+
),
16144+
),
16145+
);
16146+
16147+
// The label floats (isEmpty is false), so it is painted scaled and moved up
16148+
// onto the border, above its layout position.
16149+
final Rect labelRect = tester.getRect(find.byType(GestureDetector));
16150+
expect(labelRect.width, 75.0);
16151+
16152+
// Taps at the painted label hit the label.
16153+
await tester.tapAt(labelRect.center);
16154+
await tester.tapAt(labelRect.bottomLeft + const Offset(1.0, -1.0));
16155+
expect(tapCount, 2);
16156+
16157+
// A tap below the painted label, inside its layout position, misses the label.
16158+
await tester.tapAt(labelRect.bottomCenter + const Offset(0.0, 10.0));
16159+
expect(tapCount, 2);
16160+
});
16161+
}
1612616162
}

0 commit comments

Comments
 (0)