diff --git a/packages/flutter/lib/src/material/icon_button.dart b/packages/flutter/lib/src/material/icon_button.dart index b8d3c5df26..087c28429c 100644 --- a/packages/flutter/lib/src/material/icon_button.dart +++ b/packages/flutter/lib/src/material/icon_button.dart @@ -22,6 +22,8 @@ class IconButton extends StatelessComponent { final VoidCallback onPressed; Widget build(BuildContext context) { + // TODO(abarth): We should use a radial reaction here so you can hit the + // 8.0 pixel padding as well as the icon. return new GestureDetector( onTap: onPressed, child: new Padding( diff --git a/packages/flutter/lib/src/material/ink_well.dart b/packages/flutter/lib/src/material/ink_well.dart index 21eb524c7a..6051dc96cb 100644 --- a/packages/flutter/lib/src/material/ink_well.dart +++ b/packages/flutter/lib/src/material/ink_well.dart @@ -248,6 +248,8 @@ class _RenderInkSplashes extends RenderProxyBox { onLongPress(); } + bool hitTestSelf(Point position) => true; + void paint(PaintingContext context, Offset offset) { if (!_splashes.isEmpty) { final PaintingCanvas canvas = context.canvas; diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 40ae53cbf4..64a7902edd 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -205,8 +205,8 @@ class _RenderTabBar extends RenderBox with reportLayoutChangedIfNeeded(); } - void hitTestChildren(HitTestResult result, { Point position }) { - defaultHitTestChildren(result, position: position); + bool hitTestChildren(HitTestResult result, { Point position }) { + return defaultHitTestChildren(result, position: position); } void _paintIndicator(PaintingCanvas canvas, RenderBox selectedTab, Offset offset) { diff --git a/packages/flutter/lib/src/rendering/auto_layout.dart b/packages/flutter/lib/src/rendering/auto_layout.dart index 4aca6f7dd0..54f31ef9d0 100644 --- a/packages/flutter/lib/src/rendering/auto_layout.dart +++ b/packages/flutter/lib/src/rendering/auto_layout.dart @@ -219,8 +219,8 @@ class RenderAutoLayout extends RenderBox // only indicates that the value has been flushed to the variable. } - void hitTestChildren(HitTestResult result, {Point position}) { - defaultHitTestChildren(result, position: position); + bool hitTestChildren(HitTestResult result, {Point position}) { + return defaultHitTestChildren(result, position: position); } void paint(PaintingContext context, Offset offset) { diff --git a/packages/flutter/lib/src/rendering/block.dart b/packages/flutter/lib/src/rendering/block.dart index 463ea55123..275a88391f 100644 --- a/packages/flutter/lib/src/rendering/block.dart +++ b/packages/flutter/lib/src/rendering/block.dart @@ -219,8 +219,8 @@ class RenderBlock extends RenderBlockBase { defaultPaint(context, offset); } - void hitTestChildren(HitTestResult result, { Point position }) { - defaultHitTestChildren(result, position: position); + bool hitTestChildren(HitTestResult result, { Point position }) { + return defaultHitTestChildren(result, position: position); } } @@ -423,11 +423,11 @@ class RenderBlockViewport extends RenderBlockBase { transform.translate(startOffset, 0.0); } - void hitTestChildren(HitTestResult result, { Point position }) { + bool hitTestChildren(HitTestResult result, { Point position }) { if (isVertical) - defaultHitTestChildren(result, position: position + new Offset(0.0, -startOffset)); + return defaultHitTestChildren(result, position: position + new Offset(0.0, -startOffset)); else - defaultHitTestChildren(result, position: position + new Offset(-startOffset, 0.0)); + return defaultHitTestChildren(result, position: position + new Offset(-startOffset, 0.0)); } void debugDescribeSettings(List settings) { diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index 7e1a89e211..586671bc25 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -545,20 +545,25 @@ abstract class RenderBox extends RenderObject { bool hitTest(HitTestResult result, { Point position }) { if (position.x >= 0.0 && position.x < _size.width && position.y >= 0.0 && position.y < _size.height) { - hitTestChildren(result, position: position); - result.add(new BoxHitTestEntry(this, position)); - return true; + if (hitTestChildren(result, position: position) || hitTestSelf(position)) { + result.add(new BoxHitTestEntry(this, position)); + return true; + } } return false; } + /// Override this function if this render object can be hit even if its + /// children were not hit + bool hitTestSelf(Point position) => false; + /// Override this function to check whether any children are located at the /// given position /// /// Typically children should be hit tested in reverse paint order so that /// hit tests at locations where children overlap hit the child that is /// visually "on top" (i.e., paints later). - void hitTestChildren(HitTestResult result, { Point position }) { } + bool hitTestChildren(HitTestResult result, { Point position }) => false; /// Multiply the transform from the parent's coordinate system to this box's /// coordinate system into the given transform diff --git a/packages/flutter/lib/src/rendering/custom_layout.dart b/packages/flutter/lib/src/rendering/custom_layout.dart index d08c90c70d..bed5b179af 100644 --- a/packages/flutter/lib/src/rendering/custom_layout.dart +++ b/packages/flutter/lib/src/rendering/custom_layout.dart @@ -108,7 +108,7 @@ class RenderCustomMultiChildLayoutBox extends RenderBox defaultPaint(context, offset); } - void hitTestChildren(HitTestResult result, { Point position }) { - defaultHitTestChildren(result, position: position); + bool hitTestChildren(HitTestResult result, { Point position }) { + return defaultHitTestChildren(result, position: position); } } diff --git a/packages/flutter/lib/src/rendering/error.dart b/packages/flutter/lib/src/rendering/error.dart index 01a72d040e..4e4b8c2e94 100644 --- a/packages/flutter/lib/src/rendering/error.dart +++ b/packages/flutter/lib/src/rendering/error.dart @@ -30,6 +30,8 @@ class RenderErrorBox extends RenderBox { bool get sizedByParent => true; + bool hitTestSelf(Point position) => true; + void performResize() { size = constraints.constrain(const Size(_kMaxWidth, _kMaxHeight)); } diff --git a/packages/flutter/lib/src/rendering/flex.dart b/packages/flutter/lib/src/rendering/flex.dart index d3047955ba..9de75918b0 100644 --- a/packages/flutter/lib/src/rendering/flex.dart +++ b/packages/flutter/lib/src/rendering/flex.dart @@ -542,8 +542,8 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin true; + void performLayout() { size = _sizeForConstraints(constraints); } diff --git a/packages/flutter/lib/src/rendering/overflow.dart b/packages/flutter/lib/src/rendering/overflow.dart index e72a5db9ff..d1cd29d80a 100644 --- a/packages/flutter/lib/src/rendering/overflow.dart +++ b/packages/flutter/lib/src/rendering/overflow.dart @@ -125,11 +125,8 @@ class RenderOverflowBox extends RenderBox with RenderObjectWithChildMixin true; + void performLayout() { layoutText(constraints); size = constraints.constrain(textPainter.size); diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 2bf09936b7..f795ca4c33 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.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 'dart:math' as math; import 'dart:ui' as ui; import 'package:flutter/painting.dart'; @@ -70,11 +71,8 @@ class RenderProxyBox extends RenderBox with RenderObjectWithChildMixin 0.5) + return false; + return super.hitTest(result, position: position); + } + void paint(PaintingContext context, Offset offset) { if (child != null) { Rect rect = offset & size; @@ -706,6 +713,19 @@ class RenderDecoratedBox extends RenderProxyBox { super.detach(); } + bool hitTestSelf(Point position) { + switch (_painter.decoration.shape) { + case Shape.rectangle: + // TODO(abarth): We should check the border radius. + return true; + case Shape.circle: + // Circles are inscribed into our smallest dimension. + Point center = size.center(Point.origin); + double distance = (position - center).distance; + return distance <= math.min(size.width, size.height) / 2.0; + } + } + void paint(PaintingContext context, Offset offset) { assert(size.width != null); assert(size.height != null); @@ -894,6 +914,7 @@ class RenderSizeObserver extends RenderProxyBox { /// Called when its time to paint into the given canvas typedef void CustomPaintCallback(PaintingCanvas canvas, Size size); +typedef bool CustomHitTestCallback(Point position); /// Delegates its painting to [onPaint] /// @@ -911,6 +932,7 @@ class RenderCustomPaint extends RenderProxyBox { RenderCustomPaint({ CustomPaintCallback onPaint, + this.onHitTest, RenderBox child }) : super(child) { assert(onPaint != null); @@ -931,11 +953,17 @@ class RenderCustomPaint extends RenderProxyBox { markNeedsPaint(); } + CustomHitTestCallback onHitTest; + void attach() { assert(_onPaint != null); super.attach(); } + bool hitTestSelf(Point position) { + return onHitTest == null || onHitTest(position); + } + void paint(PaintingContext context, Offset offset) { assert(_onPaint != null); context.canvas.translate(offset.dx, offset.dy); diff --git a/packages/flutter/lib/src/rendering/shifted_box.dart b/packages/flutter/lib/src/rendering/shifted_box.dart index 63901e0e12..f1b46ac9fb 100644 --- a/packages/flutter/lib/src/rendering/shifted_box.dart +++ b/packages/flutter/lib/src/rendering/shifted_box.dart @@ -60,12 +60,14 @@ abstract class RenderShiftedBox extends RenderBox with RenderObjectWithChildMixi } } - void hitTestChildren(HitTestResult result, { Point position }) { + bool hitTestChildren(HitTestResult result, { Point position }) { if (child != null) { final BoxParentData childParentData = child.parentData; - child.hitTest(result, position: new Point(position.x - childParentData.position.x, - position.y - childParentData.position.y)); + final Point childPosition = new Point(position.x - childParentData.position.x, + position.y - childParentData.position.y); + return child.hitTest(result, position: childPosition); } + return false; } } diff --git a/packages/flutter/lib/src/rendering/stack.dart b/packages/flutter/lib/src/rendering/stack.dart index da97748fff..40db7cc89b 100644 --- a/packages/flutter/lib/src/rendering/stack.dart +++ b/packages/flutter/lib/src/rendering/stack.dart @@ -357,8 +357,8 @@ abstract class RenderStackBase extends RenderBox } } - void hitTestChildren(HitTestResult result, { Point position }) { - defaultHitTestChildren(result, position: position); + bool hitTestChildren(HitTestResult result, { Point position }) { + return defaultHitTestChildren(result, position: position); } void paintStack(PaintingContext context, Offset offset); @@ -455,15 +455,15 @@ class RenderIndexedStack extends RenderStackBase { return child; } - void hitTestChildren(HitTestResult result, { Point position }) { + bool hitTestChildren(HitTestResult result, { Point position }) { if (firstChild == null) - return; + return false; assert(position != null); RenderBox child = _childAtIndex(); final StackParentData childParentData = child.parentData; Point transformed = new Point(position.x - childParentData.position.x, position.y - childParentData.position.y); - child.hitTest(result, position: transformed); + return child.hitTest(result, position: transformed); } void paintStack(PaintingContext context, Offset offset) { diff --git a/packages/flutter/lib/src/rendering/toggleable.dart b/packages/flutter/lib/src/rendering/toggleable.dart index 7e7e1ef52c..5c9d22d8c7 100644 --- a/packages/flutter/lib/src/rendering/toggleable.dart +++ b/packages/flutter/lib/src/rendering/toggleable.dart @@ -73,4 +73,6 @@ abstract class RenderToggleable extends RenderConstrainedBox { if (onChanged != null) onChanged(!_value); } + + bool hitTestSelf(Point position) => true; } diff --git a/packages/flutter/lib/src/rendering/viewport.dart b/packages/flutter/lib/src/rendering/viewport.dart index 942dbcbfea..03f317ef9a 100644 --- a/packages/flutter/lib/src/rendering/viewport.dart +++ b/packages/flutter/lib/src/rendering/viewport.dart @@ -160,11 +160,12 @@ class RenderViewport extends RenderBox with RenderObjectWithChildMixin new RenderCustomPaint(onPaint: onPaint); + RenderCustomPaint createRenderObject() => new RenderCustomPaint(onPaint: onPaint, onHitTest: onHitTest); void updateRenderObject(RenderCustomPaint renderObject, CustomPaint oldWidget) { if (oldWidget.token != token) renderObject.markNeedsPaint(); renderObject.onPaint = onPaint; + renderObject.onHitTest = onHitTest; } void didUnmountRenderObject(RenderCustomPaint renderObject) { renderObject.onPaint = null; + renderObject.onHitTest = null; } } diff --git a/packages/flutter/lib/src/widgets/modal_barrier.dart b/packages/flutter/lib/src/widgets/modal_barrier.dart index 6b2cd17c13..d11ab198f1 100644 --- a/packages/flutter/lib/src/widgets/modal_barrier.dart +++ b/packages/flutter/lib/src/widgets/modal_barrier.dart @@ -17,29 +17,23 @@ const Color _kTransparent = const Color(0x00000000); class ModalBarrier extends StatelessComponent { ModalBarrier({ Key key, - this.color + this.color: _kTransparent }) : super(key: key); final Color color; Widget build(BuildContext context) { - Widget child; - - if (color != null) { - child = new DecoratedBox( - decoration: new BoxDecoration( - backgroundColor: color - ) - ); - } - return new Listener( onPointerDown: (_) { Navigator.of(context).pop(); }, child: new ConstrainedBox( constraints: const BoxConstraints.expand(), - child: child + child: new DecoratedBox( + decoration: new BoxDecoration( + backgroundColor: color + ) + ) ) ); } diff --git a/packages/unit/test/rendering/viewport_test.dart b/packages/unit/test/rendering/viewport_test.dart index dae2c93acd..af1cf1640a 100644 --- a/packages/unit/test/rendering/viewport_test.dart +++ b/packages/unit/test/rendering/viewport_test.dart @@ -5,8 +5,14 @@ import 'rendering_tester.dart'; void main() { test('Should be able to hit with negative scroll offset', () { + RenderBox green = new RenderDecoratedBox( + decoration: new BoxDecoration( + backgroundColor: const Color(0xFF00FF00) + )); + RenderBox size = new RenderConstrainedBox( - additionalConstraints: new BoxConstraints.tight(const Size(100.0, 100.0))); + additionalConstraints: new BoxConstraints.tight(const Size(100.0, 100.0)), + child: green); RenderBox red = new RenderDecoratedBox( decoration: new BoxDecoration( @@ -21,10 +27,10 @@ void main() { result = new HitTestResult(); renderView.hitTest(result, position: new Point(15.0, 0.0)); - expect(result.path.first.target, equals(viewport)); + expect(result.path.first.target.runtimeType, equals(TestRenderView)); result = new HitTestResult(); renderView.hitTest(result, position: new Point(15.0, 15.0)); - expect(result.path.first.target, equals(size)); + expect(result.path.first.target, equals(green)); }); } diff --git a/packages/unit/test/widget/gesture_detector_test.dart b/packages/unit/test/widget/gesture_detector_test.dart index 56f9bdecf2..38488fcc8e 100644 --- a/packages/unit/test/widget/gesture_detector_test.dart +++ b/packages/unit/test/widget/gesture_detector_test.dart @@ -23,7 +23,11 @@ void main() { onVerticalDragEnd: (Offset velocity) { didEndDrag = true; }, - child: new Container() + child: new Container( + decoration: const BoxDecoration( + backgroundColor: const Color(0xFF00FF00) + ) + ) ); tester.pumpWidget(widget); @@ -70,7 +74,11 @@ void main() { onVerticalDragEnd: (Offset velocity) { gestureCount += 1; }, onHorizontalDragUpdate: (_) { fail("gesture should not match"); }, onHorizontalDragEnd: (Offset velocity) { fail("gesture should not match"); }, - child: new Container() + child: new Container( + decoration: const BoxDecoration( + backgroundColor: const Color(0xFF00FF00) + ) + ) ); tester.pumpWidget(widget); @@ -106,7 +114,11 @@ void main() { onPanEnd: (_) { didEndPan = true; }, - child: new Container() + child: new Container( + decoration: const BoxDecoration( + backgroundColor: const Color(0xFF00FF00) + ) + ) ) ); diff --git a/packages/unit/test/widget/snack_bar_test.dart b/packages/unit/test/widget/snack_bar_test.dart index 77d9ebce57..f79ef54c03 100644 --- a/packages/unit/test/widget/snack_bar_test.dart +++ b/packages/unit/test/widget/snack_bar_test.dart @@ -21,9 +21,14 @@ void main() { content: new Text(helloSnackBar) ); }, - child: new Center( - key: tapTarget, - child: new Placeholder(key: placeholderKey) + child: new Container( + decoration: const BoxDecoration( + backgroundColor: const Color(0xFF00FF00) + ), + child: new Center( + key: tapTarget, + child: new Placeholder(key: placeholderKey) + ) ) ); } diff --git a/packages/unit/test/widget/transform_test.dart b/packages/unit/test/widget/transform_test.dart index 03c0eef907..b1e9afd3d3 100644 --- a/packages/unit/test/widget/transform_test.dart +++ b/packages/unit/test/widget/transform_test.dart @@ -33,7 +33,11 @@ void main() { onTap: () { didReceiveTap = true; }, - child: new Container() + child: new Container( + decoration: new BoxDecoration( + backgroundColor: new Color(0xFF00FFFF) + ) + ) ) ) ) @@ -78,7 +82,11 @@ void main() { onTap: () { didReceiveTap = true; }, - child: new Container() + child: new Container( + decoration: new BoxDecoration( + backgroundColor: new Color(0xFF00FFFF) + ) + ) ) ) ) @@ -124,7 +132,11 @@ void main() { onTap: () { didReceiveTap = true; }, - child: new Container() + child: new Container( + decoration: new BoxDecoration( + backgroundColor: new Color(0xFF00FFFF) + ) + ) ) ) )