Add BuildContext.size as a convenience getter (#6355)

Developers need to get the size of the BuildContext sufficiently often
that we should provide a convenient getter for the value. Having this
getter is also an opportunity to catch common mistakes and provide
useful error messages that guide developers towards better patterns.

Fixes #2321
This commit is contained in:
Adam Barth
2016-10-17 12:33:11 -07:00
committed by GitHub
parent 4052a36677
commit 63e7a0e80a
8 changed files with 123 additions and 35 deletions

View File

@@ -62,11 +62,11 @@ class _GridPhotoViewerState extends State<GridPhotoViewer> with SingleTickerProv
// renderer's box is w,h then the maximum value of the focal point is
// (w * _scale - w)/_scale, (h * _scale - h)/_scale.
Point _clampFocalPoint(Point point) {
final RenderBox box = context.findRenderObject();
final Size size = context.size;
final double inverseScale = (_scale - 1.0) / _scale;
final Point bottomRight = new Point(
box.size.width * inverseScale,
box.size.height * inverseScale
size.width * inverseScale,
size.height * inverseScale,
);
return new Point(point.x.clamp(0.0, bottomRight.x), point.y.clamp(0.0, bottomRight.y));
}
@@ -101,8 +101,7 @@ class _GridPhotoViewerState extends State<GridPhotoViewer> with SingleTickerProv
if (magnitude < _kMinFlingVelocity)
return;
final Offset direction = details.velocity.pixelsPerSecond / magnitude;
final RenderBox box = context.findRenderObject();
final double distance = (Point.origin & box.size).shortestSide;
final double distance = (Point.origin & context.size).shortestSide;
_flingAnimation = new Tween<Point>(
begin: _focalPoint,
end: _clampFocalPoint(_focalPoint + direction * -distance)