Skip to content

Commit 6ec1b43

Browse files
[google_maps_flutter_platform_interface] Convert PatternItem and Cap to typesafe structures. (#7703)
Convert `PatternItem` and `Cap` from JSON wrappers to typesafe classes. flutter/flutter#155121 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] page, which explains my responsibilities. - [x] I read and followed the [relevant style guides] and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use `dart format`.) - [ ] I signed the [CLA]. - [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]` - [x] I [linked to at least one issue that this PR fixes] in the description above. - [x] I updated `pubspec.yaml` with an appropriate new version according to the [pub versioning philosophy], or this PR is [exempt from version changes]. - [x] I updated `CHANGELOG.md` to add a description of the change, [following repository CHANGELOG style], or this PR is [exempt from CHANGELOG changes]. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [pub versioning philosophy]: https://dart.dev/tools/pub/versioning [exempt from version changes]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version [following repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style [exempt from CHANGELOG changes]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog [test-exempt]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests
1 parent 05bf1d4 commit 6ec1b43

File tree

6 files changed

+190
-14
lines changed

6 files changed

+190
-14
lines changed

packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.9.4
2+
3+
* Converts `PatternItem` to typesafe structure.
4+
* Converts `Cap` to typesafe structure.
5+
16
## 2.9.3
27

38
* Corrects an incorrect comment in polyline.dart file.

packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/cap.dart

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,56 @@ import 'package:flutter/foundation.dart' show immutable;
66

77
import 'types.dart';
88

9+
/// Enumeration of possible types of caps.
10+
enum CapType {
11+
/// Cap that is squared off exactly at the start or end vertex of a [Polyline]
12+
/// with solid stroke pattern, equivalent to having no additional cap beyond
13+
/// the start or end vertex.
14+
butt,
15+
16+
/// Cap that is a semicircle with radius equal to half the stroke width,
17+
/// centered at the start or end vertex of a [Polyline] with solid stroke
18+
/// pattern.
19+
round,
20+
21+
/// Cap that is squared off after extending half the stroke width beyond the
22+
/// start or end vertex of a [Polyline] with solid stroke pattern.
23+
square,
24+
25+
/// CustomCap with a bitmap overlay centered at the start or
26+
/// end vertex of a [Polyline], orientated according to the direction of the line's
27+
/// first or last edge and scaled with respect to the line's stroke width.
28+
custom,
29+
}
30+
31+
String _capTypeToJson(CapType capType) => switch (capType) {
32+
CapType.butt => 'buttCap',
33+
CapType.round => 'roundCap',
34+
CapType.square => 'squareCap',
35+
CapType.custom => 'customCap',
36+
};
37+
938
/// Cap that can be applied at the start or end vertex of a [Polyline].
1039
@immutable
1140
class Cap {
12-
const Cap._(this._json);
41+
const Cap._(this.type);
1342

1443
/// Cap that is squared off exactly at the start or end vertex of a [Polyline]
1544
/// with solid stroke pattern, equivalent to having no additional cap beyond
1645
/// the start or end vertex.
1746
///
1847
/// This is the default cap type at start and end vertices of Polylines with
1948
/// solid stroke pattern.
20-
static const Cap buttCap = Cap._(<Object>['buttCap']);
49+
static const Cap buttCap = Cap._(CapType.butt);
2150

2251
/// Cap that is a semicircle with radius equal to half the stroke width,
2352
/// centered at the start or end vertex of a [Polyline] with solid stroke
2453
/// pattern.
25-
static const Cap roundCap = Cap._(<Object>['roundCap']);
54+
static const Cap roundCap = Cap._(CapType.round);
2655

2756
/// Cap that is squared off after extending half the stroke width beyond the
2857
/// start or end vertex of a [Polyline] with solid stroke pattern.
29-
static const Cap squareCap = Cap._(<Object>['squareCap']);
58+
static const Cap squareCap = Cap._(CapType.square);
3059

3160
/// Constructs a new CustomCap with a bitmap overlay centered at the start or
3261
/// end vertex of a [Polyline], orientated according to the direction of the line's
@@ -44,11 +73,37 @@ class Cap {
4473
double refWidth = 10,
4574
}) {
4675
assert(refWidth > 0.0);
47-
return Cap._(<Object>['customCap', bitmapDescriptor.toJson(), refWidth]);
76+
return CustomCap(bitmapDescriptor, refWidth: refWidth);
4877
}
4978

50-
final Object _json;
79+
/// The type of rendering used for the cap at a start or end vertex of a
80+
/// [Polyline].
81+
final CapType type;
5182

5283
/// Converts this object to something serializable in JSON.
53-
Object toJson() => _json;
84+
Object toJson() => <Object>[_capTypeToJson(type)];
85+
}
86+
87+
/// CustomCap with a bitmap overlay centered at the start or
88+
/// end vertex of a [Polyline], orientated according to the direction of the line's
89+
/// first or last edge and scaled with respect to the line's stroke width.
90+
class CustomCap extends Cap {
91+
/// [bitmapDescriptor] must not be null.
92+
///
93+
/// [refWidth] is the reference stroke width (in pixels) - the stroke width for which
94+
/// the cap bitmap at its native dimension is designed. Must be positive. Default value
95+
/// is 10 pixels.
96+
const CustomCap(this.bitmapDescriptor, {this.refWidth = 10})
97+
: super._(CapType.custom);
98+
99+
/// Bitmap overlay centered at the start or end vertex of a [Polyline].
100+
final BitmapDescriptor bitmapDescriptor;
101+
102+
/// Reference stroke width in screen pixels. See
103+
/// https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/model/CustomCap#refWidth
104+
final double refWidth;
105+
106+
@override
107+
Object toJson() =>
108+
<Object>[_capTypeToJson(type), bitmapDescriptor.toJson(), refWidth];
54109
}

packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/pattern_item.dart

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,73 @@
44

55
import 'package:flutter/foundation.dart' show immutable;
66

7+
/// Enumeration of types of pattern items.
8+
enum PatternItemType {
9+
/// A dot used in the stroke pattern for a [Polyline].
10+
dot,
11+
12+
/// A dash used in the stroke pattern for a [Polyline].
13+
dash,
14+
15+
/// A gap used in the stroke pattern for a [Polyline].
16+
gap,
17+
}
18+
19+
String _patternItemTypeToJson(PatternItemType itemType) => switch (itemType) {
20+
PatternItemType.dot => 'dot',
21+
PatternItemType.dash => 'dash',
22+
PatternItemType.gap => 'gap',
23+
};
24+
725
/// Item used in the stroke pattern for a Polyline.
826
@immutable
927
class PatternItem {
10-
const PatternItem._(this._json);
28+
const PatternItem._(this.type);
1129

1230
/// A dot used in the stroke pattern for a [Polyline].
13-
static const PatternItem dot = PatternItem._(<Object>['dot']);
31+
static const PatternItem dot = PatternItem._(PatternItemType.dot);
1432

1533
/// A dash used in the stroke pattern for a [Polyline].
1634
///
1735
/// [length] has to be non-negative.
1836
static PatternItem dash(double length) {
1937
assert(length >= 0.0);
20-
return PatternItem._(<Object>['dash', length]);
38+
return VariableLengthPatternItem._(
39+
patternItemType: PatternItemType.dash, length: length);
2140
}
2241

2342
/// A gap used in the stroke pattern for a [Polyline].
2443
///
2544
/// [length] has to be non-negative.
2645
static PatternItem gap(double length) {
2746
assert(length >= 0.0);
28-
return PatternItem._(<Object>['gap', length]);
47+
return VariableLengthPatternItem._(
48+
patternItemType: PatternItemType.gap, length: length);
2949
}
3050

31-
final Object _json;
51+
/// The type of rendering used for an item in a pattern.
52+
final PatternItemType type;
53+
54+
/// Converts this object to something serializable in JSON.
55+
Object toJson() => <Object>[
56+
_patternItemTypeToJson(type),
57+
];
58+
}
59+
60+
/// A pattern item with a length, i.e. a dash or gap.
61+
@immutable
62+
class VariableLengthPatternItem extends PatternItem {
63+
const VariableLengthPatternItem._(
64+
{required PatternItemType patternItemType, required this.length})
65+
: super._(patternItemType);
66+
67+
/// The length in pixels of a dash or gap.
68+
final double length;
3269

3370
/// Converts this object to something serializable in JSON.
34-
Object toJson() => _json;
71+
@override
72+
Object toJson() => <Object>[
73+
_patternItemTypeToJson(type),
74+
length,
75+
];
3576
}

packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/google_maps_f
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.9.3
7+
version: 2.9.4
88

99
environment:
1010
sdk: ^3.3.0
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
7+
8+
void main() {
9+
TestWidgetsFlutterBinding.ensureInitialized();
10+
11+
test('buttCap', () {
12+
const Cap cap = Cap.buttCap;
13+
expect(cap.toJson(), equals(<Object>['buttCap']));
14+
});
15+
16+
test('roundCap', () {
17+
const Cap cap = Cap.roundCap;
18+
expect(cap.toJson(), equals(<Object>['roundCap']));
19+
});
20+
21+
test('squareCap', () {
22+
const Cap cap = Cap.squareCap;
23+
expect(cap.toJson(), equals(<Object>['squareCap']));
24+
});
25+
26+
test('customCap', () {
27+
final Cap cap = Cap.customCapFromBitmap(BitmapDescriptor.defaultMarker);
28+
expect(
29+
cap.toJson(),
30+
equals(<Object>[
31+
'customCap',
32+
<Object>['defaultMarker'],
33+
10.0
34+
]));
35+
});
36+
37+
test('customCapWithWidth', () {
38+
final Cap cap =
39+
Cap.customCapFromBitmap(BitmapDescriptor.defaultMarker, refWidth: 100);
40+
expect(
41+
cap.toJson(),
42+
equals(<Object>[
43+
'customCap',
44+
<Object>['defaultMarker'],
45+
100.0
46+
]));
47+
});
48+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
7+
8+
void main() {
9+
TestWidgetsFlutterBinding.ensureInitialized();
10+
11+
test('dot', () {
12+
const PatternItem item = PatternItem.dot;
13+
expect(item.toJson(), equals(<Object>['dot']));
14+
});
15+
16+
test('dash', () {
17+
final PatternItem item = PatternItem.dash(10.0);
18+
expect(item, isA<VariableLengthPatternItem>());
19+
expect(item.toJson(), equals(<Object>['dash', 10.0]));
20+
});
21+
22+
test('gap', () {
23+
final PatternItem item = PatternItem.gap(20.0);
24+
expect(item, isA<VariableLengthPatternItem>());
25+
expect(item.toJson(), equals(<Object>['gap', 20.0]));
26+
});
27+
}

0 commit comments

Comments
 (0)