Commit ea6292e1 authored by Tony Gentilcore's avatar Tony Gentilcore

Merge pull request #509 from tonygentilcore/background

Add a FractionalOffset alignment parameter to BackgroundImage
parents 4355302e 6562c149
......@@ -661,7 +661,8 @@ class BackgroundImage {
this.fit,
this.repeat: ImageRepeat.noRepeat,
this.centerSlice,
this.colorFilter
this.colorFilter,
this.alignment
}) : _imageResource = image;
/// How the background image should be inscribed into the box.
......@@ -682,6 +683,9 @@ class BackgroundImage {
/// A color filter to apply to the background image before painting it.
final ColorFilter colorFilter;
/// How to align the image within its bounds.
final FractionalOffset alignment;
/// The image to be painted into the background.
ui.Image get image => _image;
ui.Image _image;
......@@ -730,6 +734,7 @@ class BackgroundImage {
repeat == typedOther.repeat &&
centerSlice == typedOther.centerSlice &&
colorFilter == typedOther.colorFilter &&
alignment == typedOther.alignment &&
_imageResource == typedOther._imageResource;
}
......@@ -739,6 +744,7 @@ class BackgroundImage {
value = 37 * value + repeat.hashCode;
value = 37 * value + centerSlice.hashCode;
value = 37 * value + colorFilter.hashCode;
value = 37 * value + alignment.hashCode;
value = 37 * value + _imageResource.hashCode;
return value;
}
......@@ -988,6 +994,8 @@ class BoxPainter {
rect: rect,
image: image,
colorFilter: backgroundImage.colorFilter,
alignX: backgroundImage.alignment?.x,
alignY: backgroundImage.alignment?.y,
fit: backgroundImage.fit,
repeat: backgroundImage.repeat
);
......@@ -1093,3 +1101,36 @@ class BoxPainter {
_paintBorder(canvas, rect);
}
}
/// An offset that's expressed as a fraction of a Size.
///
/// FractionalOffset(1.0, 0.0) represents the top right of the Size,
/// FractionalOffset(0.0, 1.0) represents the bottom left of the Size,
class FractionalOffset {
const FractionalOffset(this.x, this.y);
final double x;
final double y;
bool operator ==(dynamic other) {
if (other is! FractionalOffset)
return false;
final FractionalOffset typedOther = other;
return x == typedOther.x &&
y == typedOther.y;
}
int get hashCode {
int value = 373;
value = 37 * value + x.hashCode;
value = 37 * value + y.hashCode;
return value;
}
static FractionalOffset lerp(FractionalOffset a, FractionalOffset b, double t) {
if (a == null && b == null)
return null;
if (a == null)
return new FractionalOffset(b.x * t, b.y * t);
if (b == null)
return new FractionalOffset(b.x * (1.0 - t), b.y * (1.0 - t));
return new FractionalOffset(ui.lerpDouble(a.x, b.x, t), ui.lerpDouble(a.y, b.y, t));
}
String toString() => '$runtimeType($x, $y)';
}
......@@ -13,7 +13,7 @@ import 'package:vector_math/vector_math_64.dart';
import 'debug.dart';
import 'object.dart';
export 'package:flutter/painting.dart' show TextBaseline;
export 'package:flutter/painting.dart' show FractionalOffset, TextBaseline;
// This class should only be used in debug builds
class _DebugSize extends Size {
......@@ -780,39 +780,6 @@ abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, Pare
}
}
/// An offset that's expressed as a fraction of a Size.
///
/// FractionalOffset(1.0, 0.0) represents the top right of the Size,
/// FractionalOffset(0.0, 1.0) represents the bottom left of the Size,
class FractionalOffset {
const FractionalOffset(this.x, this.y);
final double x;
final double y;
bool operator ==(dynamic other) {
if (other is! FractionalOffset)
return false;
final FractionalOffset typedOther = other;
return x == typedOther.x &&
y == typedOther.y;
}
int get hashCode {
int value = 373;
value = 37 * value + x.hashCode;
value = 37 * value + y.hashCode;
return value;
}
static FractionalOffset lerp(FractionalOffset a, FractionalOffset b, double t) {
if (a == null && b == null)
return null;
if (a == null)
return new FractionalOffset(b.x * t, b.y * t);
if (b == null)
return new FractionalOffset(b.x * (1.0 - t), b.y * (1.0 - t));
return new FractionalOffset(ui.lerpDouble(a.x, b.x, t), ui.lerpDouble(a.y, b.y, t));
}
String toString() => '$runtimeType($x, $y)';
}
class AnimatedFractionalOffsetValue extends AnimatedValue<FractionalOffset> {
AnimatedFractionalOffsetValue(FractionalOffset begin, { FractionalOffset end, Curve curve, Curve reverseCurve })
: super(begin, end: end, curve: curve, reverseCurve: reverseCurve);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment