From 729ba11bc4a308903b3f415e1f904193f2da38c2 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Tue, 2 Jun 2020 13:18:01 -0700 Subject: [PATCH] Add doc and test for Container's hitTest behavior (#57522) --- .../flutter/lib/src/widgets/container.dart | 5 ++ .../flutter/test/widgets/container_test.dart | 55 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/packages/flutter/lib/src/widgets/container.dart b/packages/flutter/lib/src/widgets/container.dart index 6298e68d2d..e915710a53 100644 --- a/packages/flutter/lib/src/widgets/container.dart +++ b/packages/flutter/lib/src/widgets/container.dart @@ -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._ diff --git a/packages/flutter/test/widgets/container_test.dart b/packages/flutter/test/widgets/container_test.dart index 6f6e74c267..a6dac22608 100644 --- a/packages/flutter/test/widgets/container_test.dart +++ b/packages/flutter/test/widgets/container_test.dart @@ -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'; @@ -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 {}