From fd68741fb330ef4bcfb73b3900743c608decbe1b Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 6 Aug 2015 09:41:22 -0700 Subject: [PATCH] Add operator+ and operator- to EdgeDims ... and use them to improve the horizontal_scrolling example. --- .../example/widgets/horizontal_scrolling.dart | 18 +++++++----------- packages/flutter/lib/rendering/box.dart | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/flutter/example/widgets/horizontal_scrolling.dart b/packages/flutter/example/widgets/horizontal_scrolling.dart index 0b344e21c27..f872798d3d6 100644 --- a/packages/flutter/example/widgets/horizontal_scrolling.dart +++ b/packages/flutter/example/widgets/horizontal_scrolling.dart @@ -6,10 +6,14 @@ import 'package:sky/painting/box_painter.dart'; import 'package:sky/widgets.dart'; class Circle extends Component { + Circle({ this.margin: EdgeDims.zero }); + + final EdgeDims margin; + Widget build() { return new Container( width: 50.0, - margin: new EdgeDims.symmetric(horizontal: 2.0), + margin: margin + new EdgeDims.symmetric(horizontal: 2.0), decoration: new BoxDecoration( shape: Shape.circle, backgroundColor: const Color(0xFF00FF00) @@ -18,16 +22,10 @@ class Circle extends Component { } } -class Pad extends Component { - Widget build() { - return new SizedBox(width: 10.0); - } -} - class HorizontalScrollingApp extends App { Widget build() { List circles = [ - new Pad(), + new Circle(margin: new EdgeDims.only(left: 10.0)), new Circle(), new Circle(), new Circle(), @@ -38,9 +36,7 @@ class HorizontalScrollingApp extends App { new Circle(), new Circle(), new Circle(), - new Circle(), - new Circle(), - new Pad(), + new Circle(margin: new EdgeDims.only(right: 10.0)), ]; return new Center( diff --git a/packages/flutter/lib/rendering/box.dart b/packages/flutter/lib/rendering/box.dart index 6aa5dab5725..d9925018919 100644 --- a/packages/flutter/lib/rendering/box.dart +++ b/packages/flutter/lib/rendering/box.dart @@ -52,6 +52,22 @@ class EdgeDims { && left == other.left; } + EdgeDims operator+(EdgeDims other) { + return new EdgeDims(top + other.top, + right + other.right, + bottom + other.bottom, + left + other.left); + } + + EdgeDims operator-(EdgeDims other) { + return new EdgeDims(top - other.top, + right - other.right, + bottom - other.bottom, + left - other.left); + } + + static const EdgeDims zero = const EdgeDims(0.0, 0.0, 0.0, 0.0); + int get hashCode { int value = 373; value = 37 * value + top.hashCode;