mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
EdgeDims changes and other fixes to core classes.
- Rename EdgeDims constructor to EdgeDims.TRBL(). - Add operator== to Size and Offset so that you can compare Size to DebugSize in checked mode. - Add Size.lerp(). - Add various operators to EdgeDims. (*, /, ~/, %) - Add EdgeDims.lerp(). - Update style guide. I went there to fix an EdgeDims constructor example, and stayed because some recent things came up and I wanted to add them before I forgot.
This commit is contained in:
parent
4ebf26bfaa
commit
ce2c56b35c
@ -47,7 +47,7 @@ class StockRow extends StatelessComponent {
|
||||
onLongPress: onLongPressed,
|
||||
child: new InkWell(
|
||||
child: new Container(
|
||||
padding: const EdgeDims(16.0, 16.0, 20.0, 16.0),
|
||||
padding: const EdgeDims.TRBL(16.0, 16.0, 20.0, 16.0),
|
||||
decoration: new BoxDecoration(
|
||||
border: new Border(
|
||||
bottom: new BorderSide(color: Theme.of(context).dividerColor)
|
||||
|
@ -14,9 +14,8 @@ import 'package:sky/src/painting/shadows.dart';
|
||||
/// Typically used for an offset from each of the four sides of a box. For
|
||||
/// example, the padding inside a box can be represented using this class.
|
||||
class EdgeDims {
|
||||
// TODO(abarth): Remove this constructor or rename it to EdgeDims.fromTRBL.
|
||||
/// Constructs an EdgeDims from offsets from the top, right, bottom and left
|
||||
const EdgeDims(this.top, this.right, this.bottom, this.left);
|
||||
const EdgeDims.TRBL(this.top, this.right, this.bottom, this.left);
|
||||
|
||||
/// Constructs an EdgeDims where all the offsets are value
|
||||
const EdgeDims.all(double value)
|
||||
@ -47,32 +46,89 @@ class EdgeDims {
|
||||
|
||||
bool get isNonNegative => top >= 0.0 && right >= 0.0 && bottom >= 0.0 && left >= 0.0;
|
||||
|
||||
bool operator ==(other) {
|
||||
if (identical(this, other))
|
||||
return true;
|
||||
return other is EdgeDims
|
||||
&& top == other.top
|
||||
&& right == other.right
|
||||
&& bottom == other.bottom
|
||||
&& left == other.left;
|
||||
EdgeDims operator-(EdgeDims other) {
|
||||
return new EdgeDims.TRBL(
|
||||
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);
|
||||
return new EdgeDims.TRBL(
|
||||
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);
|
||||
EdgeDims operator*(double other) {
|
||||
return new EdgeDims.TRBL(
|
||||
top * other,
|
||||
right * other,
|
||||
bottom * other,
|
||||
left * other
|
||||
);
|
||||
}
|
||||
|
||||
EdgeDims operator/(double other) {
|
||||
return new EdgeDims.TRBL(
|
||||
top / other,
|
||||
right / other,
|
||||
bottom / other,
|
||||
left / other
|
||||
);
|
||||
}
|
||||
|
||||
EdgeDims operator~/(double other) {
|
||||
return new EdgeDims.TRBL(
|
||||
(top ~/ other).toDouble(),
|
||||
(right ~/ other).toDouble(),
|
||||
(bottom ~/ other).toDouble(),
|
||||
(left ~/ other).toDouble()
|
||||
);
|
||||
}
|
||||
|
||||
EdgeDims operator%(double other) {
|
||||
return new EdgeDims.TRBL(
|
||||
top % other,
|
||||
right % other,
|
||||
bottom % other,
|
||||
left % other
|
||||
);
|
||||
}
|
||||
|
||||
bool operator ==(other) {
|
||||
return identical(this, other) ||
|
||||
(other is EdgeDims &&
|
||||
top == other.top &&
|
||||
right == other.right &&
|
||||
bottom == other.bottom &&
|
||||
left == other.left);
|
||||
}
|
||||
|
||||
/// Linearly interpolate between two EdgeDims
|
||||
///
|
||||
/// If either is null, this function interpolates from [EdgeDims.zero].
|
||||
static EdgeDims lerp(EdgeDims a, EdgeDims b, double t) {
|
||||
if (a == null && b == null)
|
||||
return null;
|
||||
if (a == null)
|
||||
return b * t;
|
||||
if (b == null)
|
||||
return a * (1.0 - t);
|
||||
return new EdgeDims.TRBL(
|
||||
sky.lerpDouble(a.top, b.top, t),
|
||||
sky.lerpDouble(a.right, b.right, t),
|
||||
sky.lerpDouble(a.bottom, b.bottom, t),
|
||||
sky.lerpDouble(a.left, b.left, t)
|
||||
);
|
||||
}
|
||||
|
||||
/// An EdgeDims with zero offsets in each direction
|
||||
static const EdgeDims zero = const EdgeDims(0.0, 0.0, 0.0, 0.0);
|
||||
static const EdgeDims zero = const EdgeDims.TRBL(0.0, 0.0, 0.0, 0.0);
|
||||
|
||||
int get hashCode {
|
||||
int value = 373;
|
||||
@ -142,7 +198,7 @@ class Border {
|
||||
|
||||
/// The widths of the sides of this border represented as an EdgeDims
|
||||
EdgeDims get dimensions {
|
||||
return new EdgeDims(top.width, right.width, bottom.width, left.width);
|
||||
return new EdgeDims.TRBL(top.width, right.width, bottom.width, left.width);
|
||||
}
|
||||
|
||||
int get hashCode {
|
||||
|
@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:sky/animation.dart';
|
||||
import 'package:sky/rendering.dart';
|
||||
import 'package:sky/src/widgets/framework.dart';
|
||||
|
@ -70,7 +70,7 @@ class Dialog extends StatelessComponent {
|
||||
if (title != null) {
|
||||
EdgeDims padding = titlePadding;
|
||||
if (padding == null)
|
||||
padding = new EdgeDims(24.0, 24.0, content == null ? 20.0 : 0.0, 24.0);
|
||||
padding = new EdgeDims.TRBL(24.0, 24.0, content == null ? 20.0 : 0.0, 24.0);
|
||||
dialogBody.add(new Padding(
|
||||
padding: padding,
|
||||
child: new DefaultTextStyle(
|
||||
@ -83,7 +83,7 @@ class Dialog extends StatelessComponent {
|
||||
if (content != null) {
|
||||
EdgeDims padding = contentPadding;
|
||||
if (padding == null)
|
||||
padding = const EdgeDims(20.0, 24.0, 24.0, 24.0);
|
||||
padding = const EdgeDims.TRBL(20.0, 24.0, 24.0, 24.0);
|
||||
dialogBody.add(new Padding(
|
||||
padding: padding,
|
||||
child: new DefaultTextStyle(
|
||||
|
14
packages/unit/test/painting/edge_dims_test.dart
Normal file
14
packages/unit/test/painting/edge_dims_test.dart
Normal file
@ -0,0 +1,14 @@
|
||||
import 'package:sky/painting.dart';
|
||||
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
test("EdgeDims.lerp()", () {
|
||||
EdgeDims a = new EdgeDims.all(10.0);
|
||||
EdgeDims b = new EdgeDims.all(20.0);
|
||||
expect(EdgeDims.lerp(a, b, 0.25), equals(a * 1.25));
|
||||
expect(EdgeDims.lerp(a, b, 0.25), equals(b * 0.625));
|
||||
expect(EdgeDims.lerp(a, b, 0.25), equals(a + const EdgeDims.all(2.5)));
|
||||
expect(EdgeDims.lerp(a, b, 0.25), equals(b - const EdgeDims.all(7.5)));
|
||||
});
|
||||
}
|
18
packages/unit/test/rendering/size_test.dart
Normal file
18
packages/unit/test/rendering/size_test.dart
Normal file
@ -0,0 +1,18 @@
|
||||
import 'package:sky/rendering.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'rendering_tester.dart';
|
||||
|
||||
void main() {
|
||||
test('Stack can layout with top, right, bottom, left 0.0', () {
|
||||
RenderBox box = new RenderConstrainedBox(
|
||||
additionalConstraints: new BoxConstraints.tight(const Size(100.0, 100.0)));
|
||||
|
||||
layout(box, constraints: const BoxConstraints());
|
||||
|
||||
expect(box.size.width, equals(100.0));
|
||||
expect(box.size.height, equals(100.0));
|
||||
expect(box.size, equals(const Size(100.0, 100.0)));
|
||||
expect(box.size.runtimeType.toString(), equals('_DebugSize'));
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user