Commit e548d465 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Document GridPaper (#10357)

parent 767ab66c
...@@ -246,7 +246,7 @@ class _MaterialAppState extends State<MaterialApp> { ...@@ -246,7 +246,7 @@ class _MaterialAppState extends State<MaterialApp> {
checkerboardRasterCacheImages: widget.checkerboardRasterCacheImages, checkerboardRasterCacheImages: widget.checkerboardRasterCacheImages,
checkerboardOffscreenLayers: widget.checkerboardOffscreenLayers, checkerboardOffscreenLayers: widget.checkerboardOffscreenLayers,
showSemanticsDebugger: widget.showSemanticsDebugger, showSemanticsDebugger: widget.showSemanticsDebugger,
debugShowCheckedModeBanner: widget.debugShowCheckedModeBanner debugShowCheckedModeBanner: widget.debugShowCheckedModeBanner,
) )
); );
...@@ -256,8 +256,8 @@ class _MaterialAppState extends State<MaterialApp> { ...@@ -256,8 +256,8 @@ class _MaterialAppState extends State<MaterialApp> {
color: const Color(0xE0F9BBE0), color: const Color(0xE0F9BBE0),
interval: 8.0, interval: 8.0,
divisions: 2, divisions: 2,
subDivisions: 1, subdivisions: 1,
child: result child: result,
); );
} }
return true; return true;
...@@ -265,7 +265,7 @@ class _MaterialAppState extends State<MaterialApp> { ...@@ -265,7 +265,7 @@ class _MaterialAppState extends State<MaterialApp> {
return new ScrollConfiguration( return new ScrollConfiguration(
behavior: new _MaterialScrollBehavior(), behavior: new _MaterialScrollBehavior(),
child: result child: result,
); );
} }
} }
...@@ -12,25 +12,25 @@ class _GridPaperPainter extends CustomPainter { ...@@ -12,25 +12,25 @@ class _GridPaperPainter extends CustomPainter {
this.color, this.color,
this.interval, this.interval,
this.divisions, this.divisions,
this.subDivisions this.subdivisions
}); });
final Color color; final Color color;
final double interval; final double interval;
final int divisions; final int divisions;
final int subDivisions; final int subdivisions;
@override @override
void paint(Canvas canvas, Size size) { void paint(Canvas canvas, Size size) {
final Paint linePaint = new Paint() final Paint linePaint = new Paint()
..color = color; ..color = color;
final double allDivisions = (divisions * subDivisions).toDouble(); final double allDivisions = (divisions * subdivisions).toDouble();
for (double x = 0.0; x <= size.width; x += interval / allDivisions) { for (double x = 0.0; x <= size.width; x += interval / allDivisions) {
linePaint.strokeWidth = (x % interval == 0.0) ? 1.0 : (x % (interval / subDivisions) == 0.0) ? 0.5: 0.25; linePaint.strokeWidth = (x % interval == 0.0) ? 1.0 : (x % (interval / subdivisions) == 0.0) ? 0.5 : 0.25;
canvas.drawLine(new Offset(x, 0.0), new Offset(x, size.height), linePaint); canvas.drawLine(new Offset(x, 0.0), new Offset(x, size.height), linePaint);
} }
for (double y = 0.0; y <= size.height; y += interval / allDivisions) { for (double y = 0.0; y <= size.height; y += interval / allDivisions) {
linePaint.strokeWidth = (y % interval == 0.0) ? 1.0 : (y % (interval / subDivisions) == 0.0) ? 0.5: 0.25; linePaint.strokeWidth = (y % interval == 0.0) ? 1.0 : (y % (interval / subdivisions) == 0.0) ? 0.5 : 0.25;
canvas.drawLine(new Offset(0.0, y), new Offset(size.width, y), linePaint); canvas.drawLine(new Offset(0.0, y), new Offset(size.width, y), linePaint);
} }
} }
...@@ -40,38 +40,65 @@ class _GridPaperPainter extends CustomPainter { ...@@ -40,38 +40,65 @@ class _GridPaperPainter extends CustomPainter {
return oldPainter.color != color return oldPainter.color != color
|| oldPainter.interval != interval || oldPainter.interval != interval
|| oldPainter.divisions != divisions || oldPainter.divisions != divisions
|| oldPainter.subDivisions != subDivisions; || oldPainter.subdivisions != subdivisions;
} }
@override @override
bool hitTest(Offset position) => false; bool hitTest(Offset position) => false;
} }
/// A widget that draws a rectilinear grid of 1px wide lines. /// A widget that draws a rectilinear grid of lines one pixel wide.
/// ///
/// Useful with a [Stack] for visualizing your layout along a grid. /// Useful with a [Stack] for visualizing your layout along a grid.
///
/// The grid's origin (where the first primary horizontal line and the first
/// primary vertical line intersect) is at the top left of the widget.
///
/// The grid is drawn over the [child] widget.
class GridPaper extends StatelessWidget { class GridPaper extends StatelessWidget {
/// Creates a widget that draws a rectilinear grid of 1px wide lines. /// Creates a widget that draws a rectilinear grid of 1-pixel-wide lines.
const GridPaper({ const GridPaper({
Key key, Key key,
this.color: const Color(0x7FC3E8F3), this.color: const Color(0x7FC3E8F3),
this.interval: 100.0, this.interval: 100.0,
this.divisions: 2, this.divisions: 2,
this.subDivisions: 5, this.subdivisions: 5,
this.child this.child,
}) : super(key: key); }) : assert(divisions > 0, 'The "divisions" property must be greater than zero. If there were no divisions, the grid paper would not paint anything.'),
assert(subdivisions > 0, 'The "subdivisions" property must be greater than zero. If there were no subdivisions, the grid paper would not paint anything.'),
super(key: key);
/// The color to draw the lines in the grid. /// The color to draw the lines in the grid.
///
/// Defaults to a light blue commonly seen on traditional gridpaper.
final Color color; final Color color;
/// The distance between the primary lines in the grid, in logical pixels. /// The distance between the primary lines in the grid, in logical pixels.
///
/// Each primary line is one logical pixel wide.
final double interval; final double interval;
/// The number of major divisions within each primary grid cell. /// The number of major divisions within each primary grid cell.
///
/// This is the number of major divisions per [interval], including the
/// primary grid's line.
///
/// The lines after the first are half a logical pixel wide.
///
/// If this is set to 2 (the default), then for each [interval] there will be
/// a 1-pixel line on the left, a half-pixel line in the middle, and a 1-pixel
/// line on the right (the latter being the 1-pixel line on the left of the
/// next [interval]).
final int divisions; final int divisions;
/// The number of minor divisions within each major division. /// The number of minor divisions within each major division, including the
final int subDivisions; /// major division itself.
///
/// If [subdivisions] is 5 (the default), it means that there will be four
/// lines between each major ([divisions]) line.
///
/// The subdivision lines after the first are a quarter of a logical pixel wide.
final int subdivisions;
/// The widget below this widget in the tree. /// The widget below this widget in the tree.
final Widget child; final Widget child;
...@@ -83,9 +110,9 @@ class GridPaper extends StatelessWidget { ...@@ -83,9 +110,9 @@ class GridPaper extends StatelessWidget {
color: color, color: color,
interval: interval, interval: interval,
divisions: divisions, divisions: divisions,
subDivisions: subDivisions subdivisions: subdivisions,
), ),
child: child child: child,
); );
} }
} }
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