Skip to content

Commit

Permalink
Add doc and test for Container's hitTest behavior (#57522)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkwingsmt committed Jun 2, 2020
1 parent 0bda633 commit 729ba11
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/flutter/lib/src/widgets/container.dart
Expand Up @@ -184,6 +184,11 @@ class DecoratedBox extends SingleChildRenderObjectWidget {
/// `width`, `height`, and [constraints] arguments to the constructor override
/// this.
///
/// By default, containers return false for all hit tests. If the [color]
/// property is specified, the hit testing is handled by [ColoredBox], which
/// always returns true. If the [decoration] or [foregroundDecoration] properties
/// are specified, hit testing is handled by [Decoration.hitTest].
///
/// ## Layout behavior
///
/// _See [BoxConstraints] for an introduction to box layout models._
Expand Down
55 changes: 55 additions & 0 deletions packages/flutter/test/widgets/container_test.dart
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
import 'package:mockito/mockito.dart';
Expand Down Expand Up @@ -502,6 +503,60 @@ void main() {
findsOneWidget,
);
});

testWidgets('Container is hittable only when having decorations', (WidgetTester tester) async {
bool tapped = false;
await tester.pumpWidget(GestureDetector(
onTap: () { tapped = true; },
child: Container(
decoration: const BoxDecoration(color: Colors.black),
),
));

await tester.tap(find.byType(Container));
expect(tapped, true);
tapped = false;

await tester.pumpWidget(GestureDetector(
onTap: () { tapped = true; },
child: Container(
foregroundDecoration: const BoxDecoration(color: Colors.black),
),
));

await tester.tap(find.byType(Container));
expect(tapped, true);
tapped = false;

await tester.pumpWidget(GestureDetector(
onTap: () { tapped = true; },
child: Container(
color: Colors.black,
),
));

await tester.tap(find.byType(Container));
expect(tapped, true);
tapped = false;

// Everything but color or decorations
await tester.pumpWidget(GestureDetector(
onTap: () { tapped = true; },
child: Center(
child: Container(
alignment: Alignment.bottomRight,
padding: const EdgeInsets.all(2),
width: 50,
height: 50,
margin: const EdgeInsets.all(2),
transform: Matrix4.rotationZ(1),
),
),
));

await tester.tap(find.byType(Container));
expect(tapped, false);
});
}

class _MockPaintingContext extends Mock implements PaintingContext {}
Expand Down

0 comments on commit 729ba11

Please sign in to comment.