forked from firka/flutter
Add doc and test for Container's hitTest behavior (#57522)
This commit is contained in:
@@ -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._
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
Reference in New Issue
Block a user