-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[google_maps_flutter_platform_interface] Convert PatternItem
and Cap
to typesafe structures.
#7703
Changes from 8 commits
2189b35
d79bbd1
486830f
6b8c020
e22fe57
644152f
1894614
a51e2ff
b8173b2
d757868
f935885
5bb0294
9b5c826
54c3af5
91a4878
e1de2fa
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 |
---|---|---|
|
@@ -6,27 +6,56 @@ import 'package:flutter/foundation.dart' show immutable; | |
|
||
import 'types.dart'; | ||
|
||
/// Enumeration of possible types of caps. | ||
enum CapType { | ||
/// Cap that is squared off exactly at the start or end vertex of a [Polyline] | ||
/// with solid stroke pattern, equivalent to having no additional cap beyond | ||
/// the start or end vertex. | ||
butt, | ||
|
||
/// Cap that is a semicircle with radius equal to half the stroke width, | ||
/// centered at the start or end vertex of a [Polyline] with solid stroke | ||
/// pattern. | ||
round, | ||
|
||
/// Cap that is squared off after extending half the stroke width beyond the | ||
/// start or end vertex of a [Polyline] with solid stroke pattern. | ||
square, | ||
|
||
/// CustomCap with a bitmap overlay centered at the start or | ||
/// end vertex of a [Polyline], orientated according to the direction of the line's | ||
/// first or last edge and scaled with respect to the line's stroke width. | ||
custom, | ||
} | ||
|
||
String _capTypeToJson(CapType capType) => <CapType, String>{ | ||
CapType.butt: 'buttCap', | ||
CapType.round: 'roundCap', | ||
CapType.square: 'squareCap', | ||
CapType.custom: 'customCap', | ||
}[capType]!; | ||
|
||
/// Cap that can be applied at the start or end vertex of a [Polyline]. | ||
@immutable | ||
class Cap { | ||
const Cap._(this._json); | ||
const Cap._(this.capType); | ||
|
||
/// Cap that is squared off exactly at the start or end vertex of a [Polyline] | ||
/// with solid stroke pattern, equivalent to having no additional cap beyond | ||
/// the start or end vertex. | ||
/// | ||
/// This is the default cap type at start and end vertices of Polylines with | ||
/// solid stroke pattern. | ||
static const Cap buttCap = Cap._(<Object>['buttCap']); | ||
static const Cap buttCap = Cap._(CapType.butt); | ||
|
||
/// Cap that is a semicircle with radius equal to half the stroke width, | ||
/// centered at the start or end vertex of a [Polyline] with solid stroke | ||
/// pattern. | ||
static const Cap roundCap = Cap._(<Object>['roundCap']); | ||
static const Cap roundCap = Cap._(CapType.round); | ||
|
||
/// Cap that is squared off after extending half the stroke width beyond the | ||
/// start or end vertex of a [Polyline] with solid stroke pattern. | ||
static const Cap squareCap = Cap._(<Object>['squareCap']); | ||
static const Cap squareCap = Cap._(CapType.square); | ||
|
||
/// Constructs a new CustomCap with a bitmap overlay centered at the start or | ||
/// end vertex of a [Polyline], orientated according to the direction of the line's | ||
|
@@ -44,11 +73,36 @@ class Cap { | |
double refWidth = 10, | ||
}) { | ||
assert(refWidth > 0.0); | ||
return Cap._(<Object>['customCap', bitmapDescriptor.toJson(), refWidth]); | ||
return CustomCap(bitmapDescriptor, refWidth: refWidth); | ||
} | ||
|
||
final Object _json; | ||
/// The type of rendering used for the cap at a start or end vertex of a | ||
/// [Polyline]. | ||
final CapType capType; | ||
yaakovschectman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Converts this object to something serializable in JSON. | ||
Object toJson() => _json; | ||
Object toJson() => <Object>[_capTypeToJson(capType)]; | ||
} | ||
|
||
/// CustomCap with a bitmap overlay centered at the start or | ||
/// end vertex of a [Polyline], orientated according to the direction of the line's | ||
/// first or last edge and scaled with respect to the line's stroke width. | ||
class CustomCap extends Cap { | ||
/// [bitmapDescriptor] must not be null. | ||
/// | ||
/// [refWidth] is the reference stroke width (in pixels) - the stroke width for which | ||
/// the cap bitmap at its native dimension is designed. Must be positive. Default value | ||
/// is 10 pixels. | ||
const CustomCap(this.bitmapDescriptor, {this.refWidth = 10}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it documented if these are logical pixels or physical pixels? If so please specify. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few redirections through the Maps SDK documentation describes these as "screen pixels." I will update the comment to include a link to the page. |
||
: super._(CapType.custom); | ||
|
||
/// Bitmap overlay centered at the start or end vertex of a [Polyline]. | ||
final BitmapDescriptor bitmapDescriptor; | ||
|
||
/// Reference stroke width in pixels. | ||
final double refWidth; | ||
|
||
@override | ||
Object toJson() => | ||
<Object>[_capTypeToJson(capType), bitmapDescriptor.toJson(), refWidth]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,32 +4,58 @@ | |
|
||
import 'package:flutter/foundation.dart' show immutable; | ||
|
||
/// Enumeration of types of pattern items. | ||
enum PatternItemType { | ||
/// A dot used in the stroke pattern for a [Polyline]. | ||
dot, | ||
|
||
/// A dash used in the stroke pattern for a [Polyline]. | ||
dash, | ||
|
||
/// A gap used in the stroke pattern for a [Polyline]. | ||
gap, | ||
} | ||
|
||
String _patternItemTypeToJson(PatternItemType itemType) => | ||
const <PatternItemType, String>{ | ||
PatternItemType.dot: 'dot', | ||
PatternItemType.dash: 'dash', | ||
PatternItemType.gap: 'gap', | ||
}[itemType]!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
|
||
/// Item used in the stroke pattern for a Polyline. | ||
@immutable | ||
class PatternItem { | ||
const PatternItem._(this._json); | ||
const PatternItem._(this.patternItemType, [this.length]); | ||
|
||
/// A dot used in the stroke pattern for a [Polyline]. | ||
static const PatternItem dot = PatternItem._(<Object>['dot']); | ||
static const PatternItem dot = PatternItem._(PatternItemType.dot); | ||
|
||
/// A dash used in the stroke pattern for a [Polyline]. | ||
/// | ||
/// [length] has to be non-negative. | ||
static PatternItem dash(double length) { | ||
assert(length >= 0.0); | ||
return PatternItem._(<Object>['dash', length]); | ||
return PatternItem._(PatternItemType.dash, length); | ||
} | ||
|
||
/// A gap used in the stroke pattern for a [Polyline]. | ||
/// | ||
/// [length] has to be non-negative. | ||
static PatternItem gap(double length) { | ||
assert(length >= 0.0); | ||
return PatternItem._(<Object>['gap', length]); | ||
return PatternItem._(PatternItemType.gap, length); | ||
} | ||
|
||
final Object _json; | ||
/// The type of rendering used for an item in a pattern. | ||
final PatternItemType patternItemType; | ||
yaakovschectman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// The length in pixels of a dash or gap. | ||
final double? length; | ||
yaakovschectman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Converts this object to something serializable in JSON. | ||
Object toJson() => _json; | ||
Object toJson() => <Object>[ | ||
_patternItemTypeToJson(patternItemType), | ||
if (length != null) length! | ||
]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2013 The Flutter 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_test/flutter_test.dart'; | ||
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'; | ||
|
||
void main() { | ||
reidbaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
test('buttCap', () { | ||
const Cap cap = Cap.buttCap; | ||
expect(cap.toJson(), equals(<Object>['buttCap'])); | ||
}); | ||
|
||
test('roundCap', () { | ||
const Cap cap = Cap.roundCap; | ||
expect(cap.toJson(), equals(<Object>['roundCap'])); | ||
}); | ||
|
||
test('squareCap', () { | ||
const Cap cap = Cap.squareCap; | ||
expect(cap.toJson(), equals(<Object>['squareCap'])); | ||
}); | ||
|
||
test('customCap', () { | ||
final Cap cap = Cap.customCapFromBitmap(BitmapDescriptor.defaultMarker); | ||
expect( | ||
cap.toJson(), | ||
equals(<Object>[ | ||
'customCap', | ||
<Object>['defaultMarker'], | ||
10.0 | ||
])); | ||
}); | ||
|
||
test('customCapWithWidth', () { | ||
final Cap cap = | ||
Cap.customCapFromBitmap(BitmapDescriptor.defaultMarker, refWidth: 100); | ||
expect( | ||
cap.toJson(), | ||
equals(<Object>[ | ||
'customCap', | ||
<Object>['defaultMarker'], | ||
100.0 | ||
])); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2013 The Flutter 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_test/flutter_test.dart'; | ||
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'; | ||
|
||
void main() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
test('dot', () { | ||
const PatternItem item = PatternItem.dot; | ||
expect(item.toJson(), equals(<Object>['dot'])); | ||
}); | ||
|
||
test('dash', () { | ||
final PatternItem item = PatternItem.dash(10.0); | ||
expect(item.toJson(), equals(<Object>['dash', 10.0])); | ||
}); | ||
|
||
test('gap', () { | ||
final PatternItem item = PatternItem.gap(20.0); | ||
expect(item.toJson(), equals(<Object>['gap', 20.0])); | ||
}); | ||
} |
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 use a switch instead; a switch would fail
analyze
if a new enum value were added, while this would not, making this version harder to maintain.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 had not considered that, I will make the change and try to keep that in mind going forward. Thanks.