Skip to content

Commit

Permalink
add rotate flag for layers
Browse files Browse the repository at this point in the history
  • Loading branch information
maRci002 committed Aug 26, 2020
1 parent 2c14e68 commit 9e550fe
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 116 deletions.
6 changes: 3 additions & 3 deletions example/lib/pages/plugin_api.dart
Expand Up @@ -46,8 +46,9 @@ class MyCustomPluginOptions extends LayerOptions {
MyCustomPluginOptions({
Key key,
this.text = '',
rebuild,
}) : super(key: key, rebuild: rebuild);
bool rotationEnabled = false,
Stream<Null> rebuild,
}) : super(key: key, rebuild: rebuild, rotationEnabled: rotationEnabled);
}

class MyCustomPlugin implements MapPlugin {
Expand All @@ -62,7 +63,6 @@ class MyCustomPlugin implements MapPlugin {
);
return Text(
options.text,
key: options.key,
style: style,
);
}
Expand Down
1 change: 1 addition & 0 deletions example/lib/pages/plugin_scalebar.dart
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:latlong/latlong.dart';

import '../widgets/drawer.dart';
import 'scale_layer_plugin_option.dart';

Expand Down
1 change: 1 addition & 0 deletions example/lib/pages/plugin_zoombuttons.dart
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:latlong/latlong.dart';

import '../widgets/drawer.dart';
import 'zoombuttons_plugin_option.dart';

Expand Down
8 changes: 4 additions & 4 deletions example/lib/pages/scale_layer_plugin_option.dart
Expand Up @@ -19,8 +19,9 @@ class ScaleLayerPluginOption extends LayerOptions {
this.lineColor = Colors.white,
this.lineWidth = 2,
this.padding,
rebuild,
}) : super(key: key, rebuild: rebuild);
bool rotationEnabled = false,
Stream<Null> rebuild,
}) : super(key: key, rebuild: rebuild, rotationEnabled: rotationEnabled);
}

class ScaleLayerPlugin implements MapPlugin {
Expand Down Expand Up @@ -69,8 +70,7 @@ class ScaleLayer extends StatelessWidget {
5
];

ScaleLayer(this.scaleLayerOpts, this.map, this.stream)
: super(key: scaleLayerOpts.key);
ScaleLayer(this.scaleLayerOpts, this.map, this.stream);

@override
Widget build(BuildContext context) {
Expand Down
8 changes: 4 additions & 4 deletions example/lib/pages/zoombuttons_plugin_option.dart
Expand Up @@ -24,8 +24,9 @@ class ZoomButtonsPluginOption extends LayerOptions {
this.zoomInIcon = Icons.zoom_in,
this.zoomOutColor,
this.zoomOutIcon = Icons.zoom_out,
rebuild,
}) : super(key: key, rebuild: rebuild);
bool rotationEnabled = false,
Stream<Null> rebuild,
}) : super(key: key, rebuild: rebuild, rotationEnabled: rotationEnabled);
}

class ZoomButtonsPlugin implements MapPlugin {
Expand All @@ -51,8 +52,7 @@ class ZoomButtons extends StatelessWidget {
final FitBoundsOptions options =
const FitBoundsOptions(padding: EdgeInsets.all(12.0));

ZoomButtons(this.zoomButtonsOpts, this.map, this.stream)
: super(key: zoomButtonsOpts.key);
ZoomButtons(this.zoomButtonsOpts, this.map, this.stream);

@override
Widget build(BuildContext context) {
Expand Down
20 changes: 20 additions & 0 deletions lib/src/core/util.dart
@@ -1,5 +1,7 @@
import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:tuple/tuple.dart';

var _templateRe = RegExp(r'\{ *([\w_-]+) *\}');
Expand Down Expand Up @@ -57,3 +59,21 @@ StreamTransformer<T, T> throttleStreamTransformerWithTrailingCall<T>(
sink.close();
});
}

Widget wrapLayer(
Widget layer, MapState map, LayerOptions options, bool setKey) {
var size = options.rotationEnabled ? map.size : map.originalSize;
var angle = options.rotationEnabled ? map.rotationRad : 0.0;

return OverflowBox(
key: setKey ? options.key : null,
minWidth: size.x,
maxWidth: size.x,
minHeight: size.y,
maxHeight: size.y,
child: Transform.rotate(
angle: angle,
child: layer,
),
);
}
30 changes: 15 additions & 15 deletions lib/src/layer/circle_layer.dart
Expand Up @@ -2,6 +2,7 @@ import 'dart:ui';

import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/core/util.dart';
import 'package:flutter_map/src/map/map.dart';
import 'package:latlong/latlong.dart' hide Path; // conflict with Path from UI

Expand All @@ -10,8 +11,9 @@ class CircleLayerOptions extends LayerOptions {
CircleLayerOptions({
Key key,
this.circles = const [],
rebuild,
}) : super(key: key, rebuild: rebuild);
bool rotationEnabled,
Stream<Null> rebuild,
}) : super(key: key, rebuild: rebuild, rotationEnabled: rotationEnabled);
}

class CircleMarker {
Expand Down Expand Up @@ -41,31 +43,29 @@ class CircleLayerWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final mapState = MapState.of(context);
return CircleLayer(options, mapState, mapState.onMoved);

return wrapLayer(
CircleLayer(options, mapState, mapState.onMoved),
mapState,
options,
false,
);
}
}

class CircleLayer extends StatelessWidget {
final CircleLayerOptions circleOpts;
final MapState map;
final Stream<Null> stream;
CircleLayer(this.circleOpts, this.map, this.stream)
: super(key: circleOpts.key);
CircleLayer(this.circleOpts, this.map, this.stream);

@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints bc) {
final size = Size(bc.maxWidth, bc.maxHeight);
return _build(context, size);
},
);
}

Widget _build(BuildContext context, Size size) {
return StreamBuilder<void>(
stream: stream, // a Stream<void> or null
builder: (BuildContext context, _) {
var size = circleOpts.rotationEnabled ? map.size : map.originalSize;

var circleWidgets = <Widget>[];
for (var circle in circleOpts.circles) {
var pos = map.project(circle.point);
Expand All @@ -85,7 +85,7 @@ class CircleLayer extends StatelessWidget {
circleWidgets.add(
CustomPaint(
painter: CirclePainter(circle),
size: size,
size: Size(size.x, size.y),
),
);
}
Expand Down
25 changes: 12 additions & 13 deletions lib/src/layer/group_layer.dart
@@ -1,5 +1,6 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/core/util.dart';
import 'package:flutter_map/src/map/map.dart';

/// [LayerOptions] that describe a layer composed by multiple built-in layers.
Expand All @@ -9,8 +10,9 @@ class GroupLayerOptions extends LayerOptions {
GroupLayerOptions({
Key key,
this.group,
rebuild,
}) : super(key: key, rebuild: rebuild);
bool rotationEnabled,
Stream<Null> rebuild,
}) : super(key: key, rebuild: rebuild, rotationEnabled: rotationEnabled);
}

class GroupLayerWidget extends StatelessWidget {
Expand All @@ -21,7 +23,13 @@ class GroupLayerWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final mapState = MapState.of(context);
return GroupLayer(options, mapState, mapState.onMoved);

return wrapLayer(
GroupLayer(options, mapState, mapState.onMoved),
mapState,
options,
false,
);
}
}

Expand All @@ -30,19 +38,10 @@ class GroupLayer extends StatelessWidget {
final MapState map;
final Stream<Null> stream;

GroupLayer(this.groupOpts, this.map, this.stream) : super(key: groupOpts.key);
GroupLayer(this.groupOpts, this.map, this.stream);

@override
Widget build(BuildContext context) {
return LayoutBuilder(
// TODO unused BoxContraints should remove?
builder: (BuildContext context, BoxConstraints bc) {
return _build(context);
},
);
}

Widget _build(BuildContext context) {
return StreamBuilder(
stream: stream,
builder: (BuildContext context, _) {
Expand Down
4 changes: 3 additions & 1 deletion lib/src/layer/layer.dart
Expand Up @@ -7,5 +7,7 @@ import 'package:flutter/foundation.dart';
class LayerOptions {
final Key key;
final Stream<Null> rebuild;
LayerOptions({this.key, this.rebuild});
final bool rotationEnabled;
LayerOptions({this.key, this.rebuild, bool rotationEnabled})
: rotationEnabled = rotationEnabled ?? true;
}
17 changes: 12 additions & 5 deletions lib/src/layer/marker_layer.dart
@@ -1,6 +1,7 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/core/bounds.dart';
import 'package:flutter_map/src/core/util.dart';
import 'package:flutter_map/src/map/map.dart';
import 'package:latlong/latlong.dart';

Expand All @@ -9,8 +10,9 @@ class MarkerLayerOptions extends LayerOptions {
MarkerLayerOptions({
Key key,
this.markers = const [],
rebuild,
}) : super(key: key, rebuild: rebuild);
bool rotationEnabled,
Stream<Null> rebuild,
}) : super(key: key, rebuild: rebuild, rotationEnabled: rotationEnabled);
}

class Anchor {
Expand Down Expand Up @@ -98,7 +100,13 @@ class MarkerLayerWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final mapState = MapState.of(context);
return MarkerLayer(options, mapState, mapState.onMoved);

return wrapLayer(
MarkerLayer(options, mapState, mapState.onMoved),
mapState,
options,
false,
);
}
}

Expand All @@ -107,8 +115,7 @@ class MarkerLayer extends StatelessWidget {
final MapState map;
final Stream<Null> stream;

MarkerLayer(this.markerOpts, this.map, this.stream)
: super(key: markerOpts.key);
MarkerLayer(this.markerOpts, this.map, this.stream);

bool _boundsContainsMarker(Marker marker) {
var pixelPoint = map.project(marker.point);
Expand Down
17 changes: 12 additions & 5 deletions lib/src/layer/overlay_image_layer.dart
Expand Up @@ -3,6 +3,7 @@ import 'dart:ui';

import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/core/util.dart';
import 'package:flutter_map/src/map/map.dart';

class OverlayImageLayerOptions extends LayerOptions {
Expand All @@ -11,8 +12,9 @@ class OverlayImageLayerOptions extends LayerOptions {
OverlayImageLayerOptions({
Key key,
this.overlayImages = const [],
rebuild,
}) : super(key: key, rebuild: rebuild);
bool rotationEnabled,
Stream<Null> rebuild,
}) : super(key: key, rebuild: rebuild, rotationEnabled: rotationEnabled);
}

class OverlayImage {
Expand All @@ -37,7 +39,13 @@ class OverlayImageLayerWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final mapState = MapState.of(context);
return OverlayImageLayer(options, mapState, mapState.onMoved);

return wrapLayer(
OverlayImageLayer(options, mapState, mapState.onMoved),
mapState,
options,
false,
);
}
}

Expand All @@ -46,8 +54,7 @@ class OverlayImageLayer extends StatelessWidget {
final MapState map;
final Stream<Null> stream;

OverlayImageLayer(this.overlayImageOpts, this.map, this.stream)
: super(key: overlayImageOpts.key);
OverlayImageLayer(this.overlayImageOpts, this.map, this.stream);

@override
Widget build(BuildContext context) {
Expand Down

0 comments on commit 9e550fe

Please sign in to comment.