-
Notifications
You must be signed in to change notification settings - Fork 123
feat(shapes): add dotted and dashed stroke styles #1003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fdb4343
155e973
83c76df
683b9c3
bb7d3e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "permissions": { | ||
| "allow": [ | ||
| "Bash(flutter build:*)", | ||
| "Bash(flutter upgrade:*)", | ||
| "Bash(dart run build_runner:*)", | ||
| "Bash(flutter gen-l10n:*)" | ||
| ] | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,47 @@ class ShapeRenderer extends Renderer<ShapeElement> { | |||||
|
|
||||||
| ShapeRenderer(super.element, [super.layer]); | ||||||
|
|
||||||
| /// Creates a dashed path from the source path based on stroke style | ||||||
| Path _createDashedPath(Path source, StrokeStyle strokeStyle) { | ||||||
| if (strokeStyle == StrokeStyle.solid) return source; | ||||||
|
|
||||||
| final strokeWidth = element.property.strokeWidth; | ||||||
| final dashLength = strokeStyle == StrokeStyle.dashed | ||||||
| ? strokeWidth * 3 // Dashed: 3x stroke width | ||||||
|
||||||
| : strokeWidth; // Dotted: 1x stroke width | ||||||
| final gapLength = strokeWidth * 2; | ||||||
|
|
||||||
| final dashedPath = Path(); | ||||||
| for (final metric in source.computeMetrics()) { | ||||||
| double distance = 0; | ||||||
| bool draw = true; | ||||||
| while (distance < metric.length) { | ||||||
| final length = draw ? dashLength : gapLength; | ||||||
| final end = (distance + length).clamp(0.0, metric.length); | ||||||
| if (draw) { | ||||||
| dashedPath.addPath( | ||||||
| metric.extractPath(distance, end), | ||||||
| Offset.zero, | ||||||
| ); | ||||||
| } | ||||||
| distance = end; | ||||||
| draw = !draw; | ||||||
| } | ||||||
| } | ||||||
| return dashedPath; | ||||||
| } | ||||||
|
|
||||||
| /// Draws a path with the appropriate stroke style (solid, dashed, or dotted) | ||||||
| void _drawStyledPath(Canvas canvas, Path path, Paint paint) { | ||||||
| final strokeStyle = element.property.strokeStyle; | ||||||
| if (strokeStyle == StrokeStyle.solid) { | ||||||
| canvas.drawPath(path, paint); | ||||||
| } else { | ||||||
| final dashedPath = _createDashedPath(path, strokeStyle); | ||||||
| canvas.drawPath(dashedPath, paint); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @override | ||||||
| void build( | ||||||
| Canvas canvas, | ||||||
|
|
@@ -52,16 +93,15 @@ class ShapeRenderer extends Renderer<ShapeElement> { | |||||
| ), | ||||||
| ); | ||||||
| if (strokeWidth > 0) { | ||||||
| canvas.drawRRect( | ||||||
| RRect.fromRectAndCorners( | ||||||
| rect, | ||||||
| topLeft: topLeftCornerRadius, | ||||||
| topRight: topRightCornerRadius, | ||||||
| bottomLeft: bottomLeftCornerRadius, | ||||||
| bottomRight: bottomRightCornerRadius, | ||||||
| ), | ||||||
| paint, | ||||||
| final rrect = RRect.fromRectAndCorners( | ||||||
| rect, | ||||||
| topLeft: topLeftCornerRadius, | ||||||
| topRight: topRightCornerRadius, | ||||||
| bottomLeft: bottomLeftCornerRadius, | ||||||
| bottomRight: bottomRightCornerRadius, | ||||||
| ); | ||||||
|
Comment on lines
94
to
100
|
||||||
| final path = Path()..addRRect(rrect); | ||||||
| _drawStyledPath(canvas, path, paint); | ||||||
| } | ||||||
| } else if (shape is CircleShape) { | ||||||
| canvas.drawOval( | ||||||
|
|
@@ -72,14 +112,14 @@ class ShapeRenderer extends Renderer<ShapeElement> { | |||||
| ), | ||||||
| ); | ||||||
| if (strokeWidth > 0) { | ||||||
| canvas.drawOval(rect, paint); | ||||||
| final path = Path()..addOval(rect); | ||||||
|
||||||
| final path = Path()..addOval(rect); | |
| final path = Path()..addOval(drawRect); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please dont add this file