-
Notifications
You must be signed in to change notification settings - Fork 4
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
[Shapes] - Implement Ellipse (resolves #29) #30
Merged
matthew-carroll
merged 4 commits into
Flutter-Bounty-Hunters:main
from
suragch:i29_ellipse
Jun 13, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
doc_swift_ui_gallery/swift_ui_gallery/swift_ui_gallery/shapes/EllipseExamples.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import SwiftUI | ||
|
||
struct EllipsePage: View { | ||
var body: some View { | ||
ScrollView { | ||
VStack(spacing: 20) { | ||
// Ellipse with solid fill | ||
Ellipse() | ||
.fill(Color.blue) | ||
.frame(width: 200, height: 100) | ||
|
||
// Ellipse with gradient fill | ||
Ellipse() | ||
.fill(LinearGradient(gradient: Gradient(colors: [.yellow, .orange]), startPoint: .leading, endPoint: .trailing)) | ||
.frame(width: 200, height: 100) | ||
|
||
// Ellipse with solid stroke | ||
Ellipse() | ||
.stroke(Color.red, lineWidth: 4) | ||
.frame(width: 200, height: 100) | ||
|
||
// Ellipse with gradient stroke | ||
Ellipse() | ||
.stroke(LinearGradient(gradient: Gradient(colors: [.yellow, .blue]), startPoint: .leading, endPoint: .trailing), lineWidth: 14) | ||
.frame(width: 200, height: 100) | ||
}.frame(width: 300) | ||
} | ||
} | ||
} | ||
|
||
struct EllipsePage_Previews: PreviewProvider { | ||
static var previews: some View { | ||
EllipsePage() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:swift_ui/swift_ui.dart'; | ||
|
||
class EllipseDemo extends StatelessWidget { | ||
const EllipseDemo({ | ||
super.key, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const VStack( | ||
[ | ||
// Ellipse with solid fill | ||
Frame( | ||
width: 200, | ||
height: 100, | ||
child: Ellipse( | ||
fillColor: Colors.blue, | ||
), | ||
), | ||
|
||
// Ellipse with gradient fill | ||
Frame( | ||
width: 200, | ||
height: 100, | ||
child: Ellipse( | ||
fillGradient: LinearGradient( | ||
colors: [Colors.yellow, Colors.orange], | ||
), | ||
), | ||
), | ||
|
||
// Ellipse with solid stroke | ||
Frame( | ||
width: 200, | ||
height: 100, | ||
child: Ellipse( | ||
strokeColor: Colors.red, | ||
strokeLineWidth: 4.0, | ||
), | ||
), | ||
|
||
// Ellipse with gradient stroke | ||
Frame( | ||
width: 200, | ||
height: 100, | ||
child: Ellipse( | ||
strokeGradient: LinearGradient( | ||
colors: [Colors.yellow, Colors.blue], | ||
), | ||
strokeLineWidth: 14.0, | ||
), | ||
), | ||
], | ||
spacing: 20, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,136 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
/// An elliptical shape whose width and height match size of the parent | ||
class Ellipse extends StatelessWidget { | ||
const Ellipse({super.key}); | ||
const Ellipse({ | ||
super.key, | ||
this.fillColor, | ||
this.fillGradient, | ||
this.strokeColor, | ||
this.strokeGradient, | ||
this.strokeLineWidth = 1.0, | ||
}); | ||
|
||
/// A color used for painting the interior of the ellipse. | ||
/// | ||
/// This is an alternative to [fillGradient]. | ||
final Color? fillColor; | ||
|
||
/// A gradient used for painting the interior of the ellipse. | ||
/// | ||
/// This is an alternative to [fillColor]. | ||
final Gradient? fillGradient; | ||
|
||
/// A color used for painting the outline of the ellipse. | ||
/// | ||
/// This is an alternative to [strokeGradient]. | ||
final Color? strokeColor; | ||
|
||
/// A gradient used for painting the outline of the ellipse. | ||
/// | ||
/// This is an alternative to [strokeColor]. | ||
final Gradient? strokeGradient; | ||
|
||
/// The width of the stroke used to paint the outline of the ellipse. | ||
/// | ||
/// The stroke line is centered along the edge of the ellipse. Half of | ||
/// [strokeLineWidth] is painted outside of the ellipse and half inside. | ||
/// | ||
/// The default is 1.0. | ||
final double strokeLineWidth; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const Placeholder(); | ||
return CustomPaint( | ||
size: Size.infinite, | ||
painter: _EllipsePainter( | ||
fillColor: fillColor, | ||
fillGradient: fillGradient, | ||
strokeColor: strokeColor, | ||
strokeGradient: strokeGradient, | ||
strokeLineWidth: strokeLineWidth, | ||
), | ||
); | ||
} | ||
} | ||
|
||
class _EllipsePainter extends CustomPainter { | ||
_EllipsePainter({ | ||
required this.fillColor, | ||
required this.fillGradient, | ||
required this.strokeColor, | ||
required this.strokeGradient, | ||
required this.strokeLineWidth, | ||
}); | ||
|
||
final Color? fillColor; | ||
final Gradient? fillGradient; | ||
final Color? strokeColor; | ||
final Gradient? strokeGradient; | ||
final double strokeLineWidth; | ||
|
||
@override | ||
void paint(Canvas canvas, Size size) { | ||
_paintFill(canvas, size); | ||
_paintStroke(canvas, size); | ||
} | ||
|
||
void _paintFill(Canvas canvas, Size size) { | ||
if (fillColor != null) { | ||
_paintSolidFill(canvas, size); | ||
} else if (fillGradient != null) { | ||
_paintGradientFill(canvas, size); | ||
} | ||
} | ||
|
||
void _paintSolidFill(Canvas canvas, Size size) { | ||
final rect = Rect.fromLTWH(0, 0, size.width, size.height); | ||
final paint = Paint() | ||
..style = PaintingStyle.fill | ||
..color = fillColor!; | ||
canvas.drawOval(rect, paint); | ||
} | ||
|
||
void _paintGradientFill(Canvas canvas, Size size) { | ||
final rect = Rect.fromLTWH(0, 0, size.width, size.height); | ||
final paint = Paint() | ||
..style = PaintingStyle.fill | ||
..shader = fillGradient!.createShader(rect); | ||
canvas.drawOval(rect, paint); | ||
} | ||
|
||
void _paintStroke(Canvas canvas, Size size) { | ||
if (strokeColor != null) { | ||
_paintSolidStroke(canvas, size); | ||
} else if (strokeGradient != null) { | ||
_paintGradientStroke(canvas, size); | ||
} | ||
} | ||
|
||
void _paintSolidStroke(Canvas canvas, Size size) { | ||
final rect = Rect.fromLTWH(0, 0, size.width, size.height); | ||
final paint = Paint() | ||
..style = PaintingStyle.stroke | ||
..color = strokeColor! | ||
..strokeWidth = strokeLineWidth; | ||
canvas.drawOval(rect, paint); | ||
} | ||
|
||
void _paintGradientStroke(Canvas canvas, Size size) { | ||
final rect = Rect.fromLTWH(0, 0, size.width, size.height); | ||
final paint = Paint() | ||
..style = PaintingStyle.stroke | ||
..shader = strokeGradient!.createShader(rect) | ||
..strokeWidth = strokeLineWidth; | ||
canvas.drawOval(rect, paint); | ||
} | ||
|
||
@override | ||
bool shouldRepaint(covariant _EllipsePainter oldDelegate) { | ||
return oldDelegate.fillColor != fillColor || | ||
oldDelegate.fillGradient != fillGradient || | ||
oldDelegate.strokeColor != strokeColor || | ||
oldDelegate.strokeGradient != strokeGradient || | ||
oldDelegate.strokeLineWidth != strokeLineWidth; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:golden_toolkit/golden_toolkit.dart'; | ||
import 'package:swift_ui/swift_ui.dart'; | ||
|
||
void main() { | ||
group("Shapes > Ellipse >", () { | ||
testGoldens("smoke test", (widgetTester) async { | ||
final builder = GoldenBuilder.grid(columns: 2, widthToHeightRatio: 1) | ||
..addScenario( | ||
'Solid fill', | ||
const Frame( | ||
width: 200, | ||
height: 100, | ||
child: Ellipse( | ||
fillColor: Colors.blue, | ||
), | ||
), | ||
) | ||
..addScenario( | ||
'Gradient fill', | ||
const Frame( | ||
width: 200, | ||
height: 100, | ||
child: Ellipse( | ||
fillGradient: LinearGradient( | ||
colors: [Colors.yellow, Colors.orange], | ||
), | ||
), | ||
), | ||
) | ||
..addScenario( | ||
'Solid stroke', | ||
const Frame( | ||
width: 200, | ||
height: 100, | ||
child: Ellipse( | ||
strokeColor: Colors.blue, | ||
), | ||
), | ||
) | ||
..addScenario( | ||
'Gradient stroke', | ||
const Frame( | ||
width: 200, | ||
height: 100, | ||
child: Ellipse( | ||
strokeGradient: LinearGradient( | ||
colors: [Colors.yellow, Colors.orange], | ||
), | ||
), | ||
), | ||
); | ||
await widgetTester.pumpWidgetBuilder(builder.build()); | ||
await screenMatchesGolden(widgetTester, 'ellipse_smoke-test'); | ||
}); | ||
}); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we introduce a gradient API that matches the Swift UI API, too? I'm thinking that part of enabling Swift UI developers in Flutter is to make it as likely as possible that when developers start typing what they expect from Swift UI, they'll get the right thing in this package. It looks like Flutter already has
LinearGradient
, which matches the name in Swift UI, but it looks like Swift UI has a different set of property names and enum values.This might end up being a counterproductive idea, but I think the only way to know is to try it and then see whether its harder or easier to work with this package.
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.
Is your meaning to have the following properties on Ellipse (and every other shape) and define new classes to represent the gradient types?
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.
No I meant that Swift UI appears to have properties inside of
LinearGradient
such asstartPoint
andendPoint
, with values such as.leading
and.trailing
. Those are different than the properties in Flutter'sLinearGradient
. So I'm thinking we should create a swift_ui version ofLinearGradient
that matches the properties and possible values of the Swift UI version.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.
So we would keep the
fillGradient
andstrokeGradient
parameter names, but they would take aLinearGradient
matching SwiftUI that we would define ourselves, correct?If so, I'm assuming
LinearGradient
would implement an abstractGradient
class so that we could addRadialGradient
,EllipticalGradient
,AngularGradient
andConicGradient
as well. (Those are all SwiftUI gradient types).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.
On second thought, let's go ahead and keep using Flutter's gradients. I'm a little worried that deep areas of Flutter might depends upon the
Gradient
type and our class would be incompatible. We can re-evaluate later.Should I approve this and merge it if I don't find anything else?
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.
I agree I think. Gradients aren't something people use often enough to require one specific API. I have to review how to do it every time I use them. The class names are similar enough in Flutter that a SwiftUI dev should be able to figure it out fairly easily.
You can merge if the rest looks ok.