Clean up the standalone examples
Our examples have been growing organically over time. This patch cleans them up to illustrate specific aspects of Flutter.
This commit is contained in:
63
examples/layers/rendering/autolayout.dart
Normal file
63
examples/layers/rendering/autolayout.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:cassowary/cassowary.dart' as al;
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
void main() {
|
||||
RenderDecoratedBox c1 = new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0xFFFF0000))
|
||||
);
|
||||
|
||||
RenderDecoratedBox c2 = new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0xFF00FF00))
|
||||
);
|
||||
|
||||
RenderDecoratedBox c3 = new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0xFF0000FF))
|
||||
);
|
||||
|
||||
RenderDecoratedBox c4 = new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0xFFFFFFFF))
|
||||
);
|
||||
|
||||
RenderAutoLayout root = new RenderAutoLayout(children: <RenderBox>[c1, c2, c3, c4]);
|
||||
|
||||
AutoLayoutParentData p1 = c1.parentData;
|
||||
AutoLayoutParentData p2 = c2.parentData;
|
||||
AutoLayoutParentData p3 = c3.parentData;
|
||||
AutoLayoutParentData p4 = c4.parentData;
|
||||
|
||||
root.addConstraints(<al.Constraint>[
|
||||
// Sum of widths of each box must be equal to that of the container
|
||||
(p1.width + p2.width + p3.width == root.width) as al.Constraint,
|
||||
|
||||
// The boxes must be stacked left to right
|
||||
p1.rightEdge <= p2.leftEdge,
|
||||
p2.rightEdge <= p3.leftEdge,
|
||||
|
||||
// The widths of the first and the third boxes should be equal
|
||||
(p1.width == p3.width) as al.Constraint,
|
||||
|
||||
// The width of the second box should be twice as much as that of the first
|
||||
// and third
|
||||
(p2.width * al.cm(2.0) == p1.width) as al.Constraint,
|
||||
|
||||
// The height of the three boxes should be equal to that of the container
|
||||
(p1.height == p2.height) as al.Constraint,
|
||||
(p2.height == p3.height) as al.Constraint,
|
||||
(p3.height == root.height) as al.Constraint,
|
||||
|
||||
// The fourth box should be half as wide as the second and must be attached
|
||||
// to the right edge of the same (by its center)
|
||||
(p4.width == p2.width / al.cm(2.0)) as al.Constraint,
|
||||
(p4.height == al.cm(50.0)) as al.Constraint,
|
||||
(p4.horizontalCenter == p2.rightEdge) as al.Constraint,
|
||||
(p4.verticalCenter == p2.height / al.cm(2.0)) as al.Constraint,
|
||||
]);
|
||||
|
||||
new RenderingFlutterBinding(root: root);
|
||||
}
|
||||
22
examples/layers/rendering/custom_coordinate_systems.dart
Normal file
22
examples/layers/rendering/custom_coordinate_systems.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'src/sector_layout.dart';
|
||||
|
||||
RenderBox buildSectorExample() {
|
||||
RenderSectorRing rootCircle = new RenderSectorRing(padding: 20.0);
|
||||
rootCircle.add(new RenderSolidColor(const Color(0xFF00FFFF), desiredDeltaTheta: kTwoPi * 0.15));
|
||||
rootCircle.add(new RenderSolidColor(const Color(0xFF0000FF), desiredDeltaTheta: kTwoPi * 0.4));
|
||||
RenderSectorSlice stack = new RenderSectorSlice(padding: 2.0);
|
||||
stack.add(new RenderSolidColor(const Color(0xFFFFFF00), desiredDeltaRadius: 20.0));
|
||||
stack.add(new RenderSolidColor(const Color(0xFFFF9000), desiredDeltaRadius: 20.0));
|
||||
stack.add(new RenderSolidColor(const Color(0xFF00FF00)));
|
||||
rootCircle.add(stack);
|
||||
return new RenderBoxToRenderSectorAdapter(innerRadius: 50.0, child: rootCircle);
|
||||
}
|
||||
|
||||
void main() {
|
||||
new RenderingFlutterBinding(root: buildSectorExample());
|
||||
}
|
||||
72
examples/layers/rendering/flex_layout.dart
Normal file
72
examples/layers/rendering/flex_layout.dart
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
import 'src/solid_color_box.dart';
|
||||
|
||||
void main() {
|
||||
RenderFlex table = new RenderFlex(direction: FlexDirection.vertical);
|
||||
|
||||
void addAlignmentRow(FlexAlignItems alignItems) {
|
||||
TextStyle style = const TextStyle(color: const Color(0xFF000000));
|
||||
RenderParagraph paragraph = new RenderParagraph(new StyledTextSpan(style, <TextSpan>[new PlainTextSpan('$alignItems')]));
|
||||
table.add(new RenderPadding(child: paragraph, padding: new EdgeDims.only(top: 20.0)));
|
||||
RenderFlex row = new RenderFlex(alignItems: alignItems, textBaseline: TextBaseline.alphabetic);
|
||||
style = new TextStyle(fontSize: 15.0, color: const Color(0xFF000000));
|
||||
row.add(new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0x7FFFCCCC)),
|
||||
child: new RenderParagraph(new StyledTextSpan(style, <TextSpan>[new PlainTextSpan('foo foo foo')]))
|
||||
));
|
||||
style = new TextStyle(fontSize: 10.0, color: const Color(0xFF000000));
|
||||
row.add(new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0x7FCCFFCC)),
|
||||
child: new RenderParagraph(new StyledTextSpan(style, <TextSpan>[new PlainTextSpan('foo foo foo')]))
|
||||
));
|
||||
RenderFlex subrow = new RenderFlex(alignItems: alignItems, textBaseline: TextBaseline.alphabetic);
|
||||
style = new TextStyle(fontSize: 25.0, color: const Color(0xFF000000));
|
||||
subrow.add(new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0x7FCCCCFF)),
|
||||
child: new RenderParagraph(new StyledTextSpan(style, <TextSpan>[new PlainTextSpan('foo foo foo foo')]))
|
||||
));
|
||||
subrow.add(new RenderSolidColorBox(const Color(0x7FCCFFFF), desiredSize: new Size(30.0, 40.0)));
|
||||
row.add(subrow);
|
||||
table.add(row);
|
||||
final FlexParentData rowParentData = row.parentData;
|
||||
rowParentData.flex = 1;
|
||||
}
|
||||
|
||||
addAlignmentRow(FlexAlignItems.start);
|
||||
addAlignmentRow(FlexAlignItems.end);
|
||||
addAlignmentRow(FlexAlignItems.center);
|
||||
addAlignmentRow(FlexAlignItems.stretch);
|
||||
addAlignmentRow(FlexAlignItems.baseline);
|
||||
|
||||
void addJustificationRow(FlexJustifyContent justify) {
|
||||
const TextStyle style = const TextStyle(color: const Color(0xFF000000));
|
||||
RenderParagraph paragraph = new RenderParagraph(new StyledTextSpan(style, <TextSpan>[new PlainTextSpan('$justify')]));
|
||||
table.add(new RenderPadding(child: paragraph, padding: new EdgeDims.only(top: 20.0)));
|
||||
RenderFlex row = new RenderFlex(direction: FlexDirection.horizontal);
|
||||
row.add(new RenderSolidColorBox(const Color(0xFFFFCCCC), desiredSize: new Size(80.0, 60.0)));
|
||||
row.add(new RenderSolidColorBox(const Color(0xFFCCFFCC), desiredSize: new Size(64.0, 60.0)));
|
||||
row.add(new RenderSolidColorBox(const Color(0xFFCCCCFF), desiredSize: new Size(160.0, 60.0)));
|
||||
row.justifyContent = justify;
|
||||
table.add(row);
|
||||
final FlexParentData rowParentData = row.parentData;
|
||||
rowParentData.flex = 1;
|
||||
}
|
||||
|
||||
addJustificationRow(FlexJustifyContent.start);
|
||||
addJustificationRow(FlexJustifyContent.end);
|
||||
addJustificationRow(FlexJustifyContent.center);
|
||||
addJustificationRow(FlexJustifyContent.spaceBetween);
|
||||
addJustificationRow(FlexJustifyContent.spaceAround);
|
||||
|
||||
RenderDecoratedBox root = new RenderDecoratedBox(
|
||||
decoration: new BoxDecoration(backgroundColor: const Color(0xFFFFFFFF)),
|
||||
child: new RenderPadding(child: table, padding: new EdgeDims.symmetric(vertical: 50.0))
|
||||
);
|
||||
|
||||
new RenderingFlutterBinding(root: root);
|
||||
}
|
||||
14
examples/layers/rendering/hello_world.dart
Normal file
14
examples/layers/rendering/hello_world.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
void main() {
|
||||
new RenderingFlutterBinding(
|
||||
root: new RenderPositionedBox(
|
||||
alignment: const FractionalOffset(0.5, 0.5),
|
||||
child: new RenderParagraph(new PlainTextSpan('Hello, world.'))
|
||||
)
|
||||
);
|
||||
}
|
||||
44
examples/layers/rendering/spinning_square.dart
Normal file
44
examples/layers/rendering/spinning_square.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// 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 'package:flutter/animation.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
void main() {
|
||||
// A green box...
|
||||
RenderBox green = new RenderDecoratedBox(
|
||||
decoration: const BoxDecoration(backgroundColor: const Color(0xFF00FF00))
|
||||
);
|
||||
// of a certain size...
|
||||
RenderBox square = new RenderConstrainedBox(
|
||||
additionalConstraints: const BoxConstraints.tightFor(width: 200.0, height: 200.0),
|
||||
child: green
|
||||
);
|
||||
// With a given rotation (starts off as the identity transform)...
|
||||
RenderTransform spin = new RenderTransform(
|
||||
transform: new Matrix4.identity(),
|
||||
alignment: const FractionalOffset(0.5, 0.5),
|
||||
child: square
|
||||
);
|
||||
// centered...
|
||||
RenderBox root = new RenderPositionedBox(
|
||||
alignment: const FractionalOffset(0.5, 0.5),
|
||||
child: spin
|
||||
);
|
||||
// on the screen.
|
||||
new RenderingFlutterBinding(root: root);
|
||||
|
||||
// A repeating animation every 1800 milliseconds...
|
||||
AnimationController animation = new AnimationController(
|
||||
duration: const Duration(milliseconds: 1800)
|
||||
)..repeat();
|
||||
// From 0.0 to math.PI.
|
||||
Tween<double> tween = new Tween<double>(begin: 0.0, end: math.PI);
|
||||
animation.addListener(() {
|
||||
// Each frame of the animation, set the rotation of the square.
|
||||
spin.transform = new Matrix4.rotationZ(tween.evaluate(animation));
|
||||
});
|
||||
}
|
||||
535
examples/layers/rendering/src/sector_layout.dart
Normal file
535
examples/layers/rendering/src/sector_layout.dart
Normal file
@@ -0,0 +1,535 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// 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 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
|
||||
const double kTwoPi = 2 * math.PI;
|
||||
|
||||
class SectorConstraints extends Constraints {
|
||||
const SectorConstraints({
|
||||
this.minDeltaRadius: 0.0,
|
||||
this.maxDeltaRadius: double.INFINITY,
|
||||
this.minDeltaTheta: 0.0,
|
||||
this.maxDeltaTheta: kTwoPi
|
||||
});
|
||||
|
||||
const SectorConstraints.tight({ double deltaRadius: 0.0, double deltaTheta: 0.0 })
|
||||
: minDeltaRadius = deltaRadius,
|
||||
maxDeltaRadius = deltaRadius,
|
||||
minDeltaTheta = deltaTheta,
|
||||
maxDeltaTheta = deltaTheta;
|
||||
|
||||
final double minDeltaRadius;
|
||||
final double maxDeltaRadius;
|
||||
final double minDeltaTheta;
|
||||
final double maxDeltaTheta;
|
||||
|
||||
double constrainDeltaRadius(double deltaRadius) {
|
||||
return deltaRadius.clamp(minDeltaRadius, maxDeltaRadius);
|
||||
}
|
||||
|
||||
double constrainDeltaTheta(double deltaTheta) {
|
||||
return deltaTheta.clamp(minDeltaTheta, maxDeltaTheta);
|
||||
}
|
||||
|
||||
bool get isTight => minDeltaTheta >= maxDeltaTheta && minDeltaTheta >= maxDeltaTheta;
|
||||
|
||||
bool get isNormalized => minDeltaRadius <= maxDeltaRadius && minDeltaTheta <= maxDeltaTheta;
|
||||
bool get debugAssertIsNormalized {
|
||||
assert(isNormalized);
|
||||
return isNormalized;
|
||||
}
|
||||
}
|
||||
|
||||
class SectorDimensions {
|
||||
const SectorDimensions({ this.deltaRadius: 0.0, this.deltaTheta: 0.0 });
|
||||
|
||||
factory SectorDimensions.withConstraints(
|
||||
SectorConstraints constraints,
|
||||
{ double deltaRadius: 0.0, double deltaTheta: 0.0 }
|
||||
) {
|
||||
return new SectorDimensions(
|
||||
deltaRadius: constraints.constrainDeltaRadius(deltaRadius),
|
||||
deltaTheta: constraints.constrainDeltaTheta(deltaTheta)
|
||||
);
|
||||
}
|
||||
|
||||
final double deltaRadius;
|
||||
final double deltaTheta;
|
||||
}
|
||||
|
||||
class SectorParentData extends ParentData {
|
||||
double radius = 0.0;
|
||||
double theta = 0.0;
|
||||
}
|
||||
|
||||
abstract class RenderSector extends RenderObject {
|
||||
|
||||
void setupParentData(RenderObject child) {
|
||||
if (child.parentData is! SectorParentData)
|
||||
child.parentData = new SectorParentData();
|
||||
}
|
||||
|
||||
// RenderSectors always use SectorParentData subclasses, as they need to be
|
||||
// able to read their position information for painting and hit testing.
|
||||
SectorParentData get parentData => super.parentData;
|
||||
|
||||
SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) {
|
||||
return new SectorDimensions.withConstraints(constraints);
|
||||
}
|
||||
|
||||
SectorConstraints get constraints => super.constraints;
|
||||
void debugAssertDoesMeetConstraints() {
|
||||
assert(constraints != null);
|
||||
assert(deltaRadius != null);
|
||||
assert(deltaRadius < double.INFINITY);
|
||||
assert(deltaTheta != null);
|
||||
assert(deltaTheta < double.INFINITY);
|
||||
assert(constraints.minDeltaRadius <= deltaRadius);
|
||||
assert(deltaRadius <= math.max(constraints.minDeltaRadius, constraints.maxDeltaRadius));
|
||||
assert(constraints.minDeltaTheta <= deltaTheta);
|
||||
assert(deltaTheta <= math.max(constraints.minDeltaTheta, constraints.maxDeltaTheta));
|
||||
}
|
||||
void performResize() {
|
||||
// default behavior for subclasses that have sizedByParent = true
|
||||
deltaRadius = constraints.constrainDeltaRadius(0.0);
|
||||
deltaTheta = constraints.constrainDeltaTheta(0.0);
|
||||
}
|
||||
void performLayout() {
|
||||
// descendants have to either override performLayout() to set both
|
||||
// the dimensions and lay out children, or, set sizedByParent to
|
||||
// true so that performResize()'s logic above does its thing.
|
||||
assert(sizedByParent);
|
||||
}
|
||||
|
||||
Rect get paintBounds => new Rect.fromLTWH(0.0, 0.0, 2.0 * deltaRadius, 2.0 * deltaRadius);
|
||||
Rect get semanticBounds => new Rect.fromLTWH(-deltaRadius, -deltaRadius, 2.0 * deltaRadius, 2.0 * deltaRadius);
|
||||
|
||||
bool hitTest(HitTestResult result, { double radius, double theta }) {
|
||||
if (radius < parentData.radius || radius >= parentData.radius + deltaRadius ||
|
||||
theta < parentData.theta || theta >= parentData.theta + deltaTheta)
|
||||
return false;
|
||||
hitTestChildren(result, radius: radius, theta: theta);
|
||||
result.add(new HitTestEntry(this));
|
||||
return true;
|
||||
}
|
||||
void hitTestChildren(HitTestResult result, { double radius, double theta }) { }
|
||||
|
||||
double deltaRadius;
|
||||
double deltaTheta;
|
||||
}
|
||||
|
||||
abstract class RenderDecoratedSector extends RenderSector {
|
||||
|
||||
RenderDecoratedSector(BoxDecoration decoration) : _decoration = decoration;
|
||||
|
||||
BoxDecoration _decoration;
|
||||
BoxDecoration get decoration => _decoration;
|
||||
void set decoration (BoxDecoration value) {
|
||||
if (value == _decoration)
|
||||
return;
|
||||
_decoration = value;
|
||||
markNeedsPaint();
|
||||
}
|
||||
|
||||
// offset must point to the center of the circle
|
||||
void paint(PaintingContext context, Offset offset) {
|
||||
assert(deltaRadius != null);
|
||||
assert(deltaTheta != null);
|
||||
assert(parentData is SectorParentData);
|
||||
|
||||
if (_decoration == null)
|
||||
return;
|
||||
|
||||
if (_decoration.backgroundColor != null) {
|
||||
final Canvas canvas = context.canvas;
|
||||
Paint paint = new Paint()..color = _decoration.backgroundColor;
|
||||
Path path = new Path();
|
||||
double outerRadius = (parentData.radius + deltaRadius);
|
||||
Rect outerBounds = new Rect.fromLTRB(offset.dx-outerRadius, offset.dy-outerRadius, offset.dx+outerRadius, offset.dy+outerRadius);
|
||||
path.arcTo(outerBounds, parentData.theta, deltaTheta, true);
|
||||
double innerRadius = parentData.radius;
|
||||
Rect innerBounds = new Rect.fromLTRB(offset.dx-innerRadius, offset.dy-innerRadius, offset.dx+innerRadius, offset.dy+innerRadius);
|
||||
path.arcTo(innerBounds, parentData.theta + deltaTheta, -deltaTheta, false);
|
||||
path.close();
|
||||
canvas.drawPath(path, paint);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SectorChildListParentData extends SectorParentData with ContainerParentDataMixin<RenderSector> { }
|
||||
|
||||
class RenderSectorWithChildren extends RenderDecoratedSector with ContainerRenderObjectMixin<RenderSector, SectorChildListParentData> {
|
||||
RenderSectorWithChildren(BoxDecoration decoration) : super(decoration);
|
||||
|
||||
void hitTestChildren(HitTestResult result, { double radius, double theta }) {
|
||||
RenderSector child = lastChild;
|
||||
while (child != null) {
|
||||
if (child.hitTest(result, radius: radius, theta: theta))
|
||||
return;
|
||||
final SectorChildListParentData childParentData = child.parentData;
|
||||
child = childParentData.previousSibling;
|
||||
}
|
||||
}
|
||||
|
||||
void visitChildren(RenderObjectVisitor visitor) {
|
||||
RenderSector child = lastChild;
|
||||
while (child != null) {
|
||||
visitor(child);
|
||||
final SectorChildListParentData childParentData = child.parentData;
|
||||
child = childParentData.previousSibling;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RenderSectorRing extends RenderSectorWithChildren {
|
||||
// lays out RenderSector children in a ring
|
||||
|
||||
RenderSectorRing({
|
||||
BoxDecoration decoration,
|
||||
double deltaRadius: double.INFINITY,
|
||||
double padding: 0.0
|
||||
}) : _padding = padding, _desiredDeltaRadius = deltaRadius, super(decoration);
|
||||
|
||||
double _desiredDeltaRadius;
|
||||
double get desiredDeltaRadius => _desiredDeltaRadius;
|
||||
void set desiredDeltaRadius(double value) {
|
||||
assert(value != null);
|
||||
if (_desiredDeltaRadius != value) {
|
||||
_desiredDeltaRadius = value;
|
||||
markNeedsLayout();
|
||||
}
|
||||
}
|
||||
|
||||
double _padding;
|
||||
double get padding => _padding;
|
||||
void set padding(double value) {
|
||||
// TODO(ianh): avoid code duplication
|
||||
assert(value != null);
|
||||
if (_padding != value) {
|
||||
_padding = value;
|
||||
markNeedsLayout();
|
||||
}
|
||||
}
|
||||
|
||||
void setupParentData(RenderObject child) {
|
||||
// TODO(ianh): avoid code duplication
|
||||
if (child.parentData is! SectorChildListParentData)
|
||||
child.parentData = new SectorChildListParentData();
|
||||
}
|
||||
|
||||
SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) {
|
||||
double outerDeltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius);
|
||||
double innerDeltaRadius = outerDeltaRadius - padding * 2.0;
|
||||
double childRadius = radius + padding;
|
||||
double paddingTheta = math.atan(padding / (radius + outerDeltaRadius));
|
||||
double innerTheta = paddingTheta; // increments with each child
|
||||
double remainingDeltaTheta = constraints.maxDeltaTheta - (innerTheta + paddingTheta);
|
||||
RenderSector child = firstChild;
|
||||
while (child != null) {
|
||||
SectorConstraints innerConstraints = new SectorConstraints(
|
||||
maxDeltaRadius: innerDeltaRadius,
|
||||
maxDeltaTheta: remainingDeltaTheta
|
||||
);
|
||||
SectorDimensions childDimensions = child.getIntrinsicDimensions(innerConstraints, childRadius);
|
||||
innerTheta += childDimensions.deltaTheta;
|
||||
remainingDeltaTheta -= childDimensions.deltaTheta;
|
||||
final SectorChildListParentData childParentData = child.parentData;
|
||||
child = childParentData.nextSibling;
|
||||
if (child != null) {
|
||||
innerTheta += paddingTheta;
|
||||
remainingDeltaTheta -= paddingTheta;
|
||||
}
|
||||
}
|
||||
return new SectorDimensions.withConstraints(constraints,
|
||||
deltaRadius: outerDeltaRadius,
|
||||
deltaTheta: innerTheta);
|
||||
}
|
||||
|
||||
void performLayout() {
|
||||
assert(this.parentData is SectorParentData);
|
||||
deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius);
|
||||
assert(deltaRadius < double.INFINITY);
|
||||
double innerDeltaRadius = deltaRadius - padding * 2.0;
|
||||
double childRadius = this.parentData.radius + padding;
|
||||
double paddingTheta = math.atan(padding / (this.parentData.radius + deltaRadius));
|
||||
double innerTheta = paddingTheta; // increments with each child
|
||||
double remainingDeltaTheta = constraints.maxDeltaTheta - (innerTheta + paddingTheta);
|
||||
RenderSector child = firstChild;
|
||||
while (child != null) {
|
||||
SectorConstraints innerConstraints = new SectorConstraints(
|
||||
maxDeltaRadius: innerDeltaRadius,
|
||||
maxDeltaTheta: remainingDeltaTheta
|
||||
);
|
||||
assert(child.parentData is SectorParentData);
|
||||
child.parentData.theta = innerTheta;
|
||||
child.parentData.radius = childRadius;
|
||||
child.layout(innerConstraints, parentUsesSize: true);
|
||||
innerTheta += child.deltaTheta;
|
||||
remainingDeltaTheta -= child.deltaTheta;
|
||||
final SectorChildListParentData childParentData = child.parentData;
|
||||
child = childParentData.nextSibling;
|
||||
if (child != null) {
|
||||
innerTheta += paddingTheta;
|
||||
remainingDeltaTheta -= paddingTheta;
|
||||
}
|
||||
}
|
||||
deltaTheta = innerTheta;
|
||||
}
|
||||
|
||||
// offset must point to the center of our circle
|
||||
// each sector then knows how to paint itself at its location
|
||||
void paint(PaintingContext context, Offset offset) {
|
||||
// TODO(ianh): avoid code duplication
|
||||
super.paint(context, offset);
|
||||
RenderSector child = firstChild;
|
||||
while (child != null) {
|
||||
context.paintChild(child, offset);
|
||||
final SectorChildListParentData childParentData = child.parentData;
|
||||
child = childParentData.nextSibling;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class RenderSectorSlice extends RenderSectorWithChildren {
|
||||
// lays out RenderSector children in a stack
|
||||
|
||||
RenderSectorSlice({
|
||||
BoxDecoration decoration,
|
||||
double deltaTheta: kTwoPi,
|
||||
double padding: 0.0
|
||||
}) : _padding = padding, _desiredDeltaTheta = deltaTheta, super(decoration);
|
||||
|
||||
double _desiredDeltaTheta;
|
||||
double get desiredDeltaTheta => _desiredDeltaTheta;
|
||||
void set desiredDeltaTheta(double value) {
|
||||
assert(value != null);
|
||||
if (_desiredDeltaTheta != value) {
|
||||
_desiredDeltaTheta = value;
|
||||
markNeedsLayout();
|
||||
}
|
||||
}
|
||||
|
||||
double _padding;
|
||||
double get padding => _padding;
|
||||
void set padding(double value) {
|
||||
// TODO(ianh): avoid code duplication
|
||||
assert(value != null);
|
||||
if (_padding != value) {
|
||||
_padding = value;
|
||||
markNeedsLayout();
|
||||
}
|
||||
}
|
||||
|
||||
void setupParentData(RenderObject child) {
|
||||
// TODO(ianh): avoid code duplication
|
||||
if (child.parentData is! SectorChildListParentData)
|
||||
child.parentData = new SectorChildListParentData();
|
||||
}
|
||||
|
||||
SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) {
|
||||
assert(this.parentData is SectorParentData);
|
||||
double paddingTheta = math.atan(padding / this.parentData.radius);
|
||||
double outerDeltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta);
|
||||
double innerDeltaTheta = outerDeltaTheta - paddingTheta * 2.0;
|
||||
double childRadius = this.parentData.radius + padding;
|
||||
double remainingDeltaRadius = constraints.maxDeltaRadius - (padding * 2.0);
|
||||
RenderSector child = firstChild;
|
||||
while (child != null) {
|
||||
SectorConstraints innerConstraints = new SectorConstraints(
|
||||
maxDeltaRadius: remainingDeltaRadius,
|
||||
maxDeltaTheta: innerDeltaTheta
|
||||
);
|
||||
SectorDimensions childDimensions = child.getIntrinsicDimensions(innerConstraints, childRadius);
|
||||
childRadius += childDimensions.deltaRadius;
|
||||
remainingDeltaRadius -= childDimensions.deltaRadius;
|
||||
final SectorChildListParentData childParentData = child.parentData;
|
||||
child = childParentData.nextSibling;
|
||||
childRadius += padding;
|
||||
remainingDeltaRadius -= padding;
|
||||
}
|
||||
return new SectorDimensions.withConstraints(constraints,
|
||||
deltaRadius: childRadius - this.parentData.radius,
|
||||
deltaTheta: outerDeltaTheta);
|
||||
}
|
||||
|
||||
void performLayout() {
|
||||
assert(this.parentData is SectorParentData);
|
||||
deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta);
|
||||
assert(deltaTheta <= kTwoPi);
|
||||
double paddingTheta = math.atan(padding / this.parentData.radius);
|
||||
double innerTheta = this.parentData.theta + paddingTheta;
|
||||
double innerDeltaTheta = deltaTheta - paddingTheta * 2.0;
|
||||
double childRadius = this.parentData.radius + padding;
|
||||
double remainingDeltaRadius = constraints.maxDeltaRadius - (padding * 2.0);
|
||||
RenderSector child = firstChild;
|
||||
while (child != null) {
|
||||
SectorConstraints innerConstraints = new SectorConstraints(
|
||||
maxDeltaRadius: remainingDeltaRadius,
|
||||
maxDeltaTheta: innerDeltaTheta
|
||||
);
|
||||
child.parentData.theta = innerTheta;
|
||||
child.parentData.radius = childRadius;
|
||||
child.layout(innerConstraints, parentUsesSize: true);
|
||||
childRadius += child.deltaRadius;
|
||||
remainingDeltaRadius -= child.deltaRadius;
|
||||
final SectorChildListParentData childParentData = child.parentData;
|
||||
child = childParentData.nextSibling;
|
||||
childRadius += padding;
|
||||
remainingDeltaRadius -= padding;
|
||||
}
|
||||
deltaRadius = childRadius - this.parentData.radius;
|
||||
}
|
||||
|
||||
// offset must point to the center of our circle
|
||||
// each sector then knows how to paint itself at its location
|
||||
void paint(PaintingContext context, Offset offset) {
|
||||
// TODO(ianh): avoid code duplication
|
||||
super.paint(context, offset);
|
||||
RenderSector child = firstChild;
|
||||
while (child != null) {
|
||||
assert(child.parentData is SectorChildListParentData);
|
||||
context.paintChild(child, offset);
|
||||
final SectorChildListParentData childParentData = child.parentData;
|
||||
child = childParentData.nextSibling;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class RenderBoxToRenderSectorAdapter extends RenderBox with RenderObjectWithChildMixin<RenderSector> {
|
||||
|
||||
RenderBoxToRenderSectorAdapter({ double innerRadius: 0.0, RenderSector child }) :
|
||||
_innerRadius = innerRadius {
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
double _innerRadius;
|
||||
double get innerRadius => _innerRadius;
|
||||
void set innerRadius(double value) {
|
||||
_innerRadius = value;
|
||||
markNeedsLayout();
|
||||
}
|
||||
|
||||
void setupParentData(RenderObject child) {
|
||||
if (child.parentData is! SectorParentData)
|
||||
child.parentData = new SectorParentData();
|
||||
}
|
||||
|
||||
double getMinIntrinsicWidth(BoxConstraints constraints) {
|
||||
if (child == null)
|
||||
return super.getMinIntrinsicWidth(constraints);
|
||||
return getIntrinsicDimensions(constraints).width;
|
||||
}
|
||||
|
||||
double getMaxIntrinsicWidth(BoxConstraints constraints) {
|
||||
if (child == null)
|
||||
return super.getMaxIntrinsicWidth(constraints);
|
||||
return getIntrinsicDimensions(constraints).width;
|
||||
}
|
||||
|
||||
double getMinIntrinsicHeight(BoxConstraints constraints) {
|
||||
if (child == null)
|
||||
return super.getMinIntrinsicHeight(constraints);
|
||||
return getIntrinsicDimensions(constraints).height;
|
||||
}
|
||||
|
||||
double getMaxIntrinsicHeight(BoxConstraints constraints) {
|
||||
if (child == null)
|
||||
return super.getMaxIntrinsicHeight(constraints);
|
||||
return getIntrinsicDimensions(constraints).height;
|
||||
}
|
||||
|
||||
Size getIntrinsicDimensions(BoxConstraints constraints) {
|
||||
assert(child is RenderSector);
|
||||
assert(child.parentData is SectorParentData);
|
||||
assert(constraints.maxWidth < double.INFINITY || constraints.maxHeight < double.INFINITY);
|
||||
double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxHeight) / 2.0 - innerRadius;
|
||||
SectorDimensions childDimensions = child.getIntrinsicDimensions(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), innerRadius);
|
||||
double dimension = (innerRadius + childDimensions.deltaRadius) * 2.0;
|
||||
return constraints.constrain(new Size(dimension, dimension));
|
||||
}
|
||||
|
||||
void performLayout() {
|
||||
if (child == null) {
|
||||
size = constraints.constrain(Size.zero);
|
||||
} else {
|
||||
assert(child is RenderSector);
|
||||
assert(constraints.maxWidth < double.INFINITY || constraints.maxHeight < double.INFINITY);
|
||||
double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxHeight) / 2.0 - innerRadius;
|
||||
assert(child.parentData is SectorParentData);
|
||||
child.parentData.radius = innerRadius;
|
||||
child.parentData.theta = 0.0;
|
||||
child.layout(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), parentUsesSize: true);
|
||||
double dimension = (innerRadius + child.deltaRadius) * 2.0;
|
||||
size = constraints.constrain(new Size(dimension, dimension));
|
||||
}
|
||||
}
|
||||
|
||||
void paint(PaintingContext context, Offset offset) {
|
||||
super.paint(context, offset);
|
||||
if (child != null) {
|
||||
Rect bounds = offset & size;
|
||||
// we move the offset to the center of the circle for the RenderSectors
|
||||
context.paintChild(child, bounds.center.toOffset());
|
||||
}
|
||||
}
|
||||
|
||||
bool hitTest(HitTestResult result, { Point position }) {
|
||||
if (child == null)
|
||||
return false;
|
||||
double x = position.x;
|
||||
double y = position.y;
|
||||
// translate to our origin
|
||||
x -= size.width/2.0;
|
||||
y -= size.height/2.0;
|
||||
// convert to radius/theta
|
||||
double radius = math.sqrt(x*x+y*y);
|
||||
double theta = (math.atan2(x, -y) - math.PI/2.0) % kTwoPi;
|
||||
if (radius < innerRadius)
|
||||
return false;
|
||||
if (radius >= innerRadius + child.deltaRadius)
|
||||
return false;
|
||||
if (theta > child.deltaTheta)
|
||||
return false;
|
||||
child.hitTest(result, radius: radius, theta: theta);
|
||||
result.add(new BoxHitTestEntry(this, position));
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class RenderSolidColor extends RenderDecoratedSector {
|
||||
RenderSolidColor(Color backgroundColor, {
|
||||
this.desiredDeltaRadius: double.INFINITY,
|
||||
this.desiredDeltaTheta: kTwoPi
|
||||
}) : this.backgroundColor = backgroundColor,
|
||||
super(new BoxDecoration(backgroundColor: backgroundColor));
|
||||
|
||||
double desiredDeltaRadius;
|
||||
double desiredDeltaTheta;
|
||||
final Color backgroundColor;
|
||||
|
||||
SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) {
|
||||
return new SectorDimensions.withConstraints(constraints, deltaTheta: desiredDeltaTheta);
|
||||
}
|
||||
|
||||
void performLayout() {
|
||||
deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius);
|
||||
deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta);
|
||||
}
|
||||
|
||||
void handleEvent(PointerEvent event, HitTestEntry entry) {
|
||||
if (event is PointerDownEvent) {
|
||||
decoration = new BoxDecoration(backgroundColor: const Color(0xFFFF0000));
|
||||
} else if (event is PointerUpEvent) {
|
||||
decoration = new BoxDecoration(backgroundColor: backgroundColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
51
examples/layers/rendering/src/solid_color_box.dart
Normal file
51
examples/layers/rendering/src/solid_color_box.dart
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
|
||||
class RenderSolidColorBox extends RenderDecoratedBox {
|
||||
final Size desiredSize;
|
||||
final Color backgroundColor;
|
||||
|
||||
RenderSolidColorBox(Color backgroundColor, { this.desiredSize: Size.infinite })
|
||||
: backgroundColor = backgroundColor,
|
||||
super(decoration: new BoxDecoration(backgroundColor: backgroundColor));
|
||||
|
||||
double getMinIntrinsicWidth(BoxConstraints constraints) {
|
||||
return constraints.constrainHeight(
|
||||
this.desiredSize == Size.infinite ? 0.0 : desiredSize.width
|
||||
);
|
||||
}
|
||||
|
||||
double getMaxIntrinsicWidth(BoxConstraints constraints) {
|
||||
return constraints.constrainWidth(
|
||||
this.desiredSize == Size.infinite ? 0.0 : desiredSize.width
|
||||
);
|
||||
}
|
||||
|
||||
double getMinIntrinsicHeight(BoxConstraints constraints) {
|
||||
return constraints.constrainHeight(
|
||||
this.desiredSize == Size.infinite ? 0.0 : desiredSize.height
|
||||
);
|
||||
}
|
||||
|
||||
double getMaxIntrinsicHeight(BoxConstraints constraints) {
|
||||
return constraints.constrainHeight(
|
||||
this.desiredSize == Size.infinite ? 0.0 : desiredSize.height
|
||||
);
|
||||
}
|
||||
|
||||
void performLayout() {
|
||||
size = constraints.constrain(desiredSize);
|
||||
}
|
||||
|
||||
void handleEvent(PointerEvent event, BoxHitTestEntry entry) {
|
||||
if (event is PointerDownEvent) {
|
||||
decoration = new BoxDecoration(backgroundColor: const Color(0xFFFF0000));
|
||||
} else if (event is PointerUpEvent) {
|
||||
decoration = new BoxDecoration(backgroundColor: backgroundColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
92
examples/layers/rendering/touch_input.dart
Normal file
92
examples/layers/rendering/touch_input.dart
Normal file
@@ -0,0 +1,92 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
// Material design colors. :p
|
||||
List<Color> _kColors = <Color>[
|
||||
Colors.teal[500],
|
||||
Colors.amber[500],
|
||||
Colors.purple[500],
|
||||
Colors.lightBlue[500],
|
||||
Colors.deepPurple[500],
|
||||
Colors.lime[500],
|
||||
];
|
||||
|
||||
/// A simple model object for a dot that reacts to pointer pressure.
|
||||
class Dot {
|
||||
Dot({ Color color }) : _paint = new Paint()..color = color;
|
||||
|
||||
final Paint _paint;
|
||||
Point position = Point.origin;
|
||||
double radius = 0.0;
|
||||
|
||||
void update(PointerEvent event) {
|
||||
position = event.position;
|
||||
radius = 5 + (95 * event.pressure);
|
||||
}
|
||||
|
||||
void paint(Canvas canvas, Offset offset) {
|
||||
canvas.drawCircle(position + offset, radius, _paint);
|
||||
}
|
||||
}
|
||||
|
||||
/// A render object that draws dots under each pointer.
|
||||
class RenderDots extends RenderConstrainedBox {
|
||||
RenderDots() : super(additionalConstraints: const BoxConstraints.expand());
|
||||
|
||||
/// State to remember which dots to paint.
|
||||
final Map<int, Dot> _dots = <int, Dot>{};
|
||||
|
||||
/// Makes this render object hittable so that it receives pointer events.
|
||||
bool hitTestSelf(Point position) => true;
|
||||
|
||||
/// Processes pointer events by mutating state and invalidating its previous
|
||||
/// painting commands.
|
||||
void handleEvent(PointerEvent event, BoxHitTestEntry entry) {
|
||||
if (event is PointerDownEvent) {
|
||||
Color color = _kColors[event.pointer.remainder(_kColors.length)];
|
||||
_dots[event.pointer] = new Dot(color: color)..update(event);
|
||||
markNeedsPaint();
|
||||
} else if (event is PointerUpEvent || event is PointerCancelEvent) {
|
||||
_dots.remove(event.pointer);
|
||||
markNeedsPaint();
|
||||
} else if (event is PointerMoveEvent) {
|
||||
_dots[event.pointer].update(event);
|
||||
markNeedsPaint();
|
||||
}
|
||||
}
|
||||
|
||||
/// Issues new painting commands.
|
||||
void paint(PaintingContext context, Offset offset) {
|
||||
final Canvas canvas = context.canvas;
|
||||
canvas.drawRect(offset & size, new Paint()..color = const Color(0xFFFFFFFF));
|
||||
for (Dot dot in _dots.values)
|
||||
dot.paint(canvas, offset);
|
||||
super.paint(context, offset);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
RenderParagraph paragraph = new RenderParagraph(
|
||||
new StyledTextSpan(
|
||||
new TextStyle(color: Colors.black87),
|
||||
[ new PlainTextSpan("Touch me!") ]
|
||||
)
|
||||
);
|
||||
RenderStack stack = new RenderStack(
|
||||
children: <RenderBox>[
|
||||
new RenderDots(),
|
||||
paragraph,
|
||||
]
|
||||
);
|
||||
// Prevent the RenderParagraph from filling the whole screen so
|
||||
// that it doesn't eat events.
|
||||
final StackParentData paragraphParentData = paragraph.parentData;
|
||||
paragraphParentData
|
||||
..top = 40.0
|
||||
..left = 20.0;
|
||||
new RenderingFlutterBinding(root: stack);
|
||||
}
|
||||
Reference in New Issue
Block a user