-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy path37.flutter_google_maps_marker_mode.dart
85 lines (75 loc) · 2.88 KB
/
37.flutter_google_maps_marker_mode.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:ig_posts/season_2/63.transperant_design_pattern.dart';
import 'package:ig_posts/utils/colors.dart';
import 'package:provider/provider.dart';
class MarkerNotifier extends ChangeNotifier {
bool _singletonMarker = false;
bool get singletonMarker => _singletonMarker;
List<Marker> _markers = [];
List<Marker> get markers => _markers;
void setMarkerOnMap({required LatLng latLng}) {
if (_singletonMarker) {
_markers.clear();
}
MarkerId markerId = MarkerId(latLng.toString());
Marker marker = Marker(markerId: markerId, position: latLng);
_markers.add(marker);
notifyListeners();
}
void toggleMarkerMode() {
_singletonMarker = !_singletonMarker;
notifyListeners();
}
}
class FlutterMapsMarkerView extends StatefulWidget {
@override
State<FlutterMapsMarkerView> createState() => _FlutterMapsMarkerViewState();
}
class _FlutterMapsMarkerViewState extends State<FlutterMapsMarkerView> {
final Completer<GoogleMapController> _controller = Completer();
late GoogleMapController googleMapController;
MarkerNotifier markerNotifier({required bool renderUI}) =>
Provider.of<MarkerNotifier>(context, listen: renderUI);
@override
Widget build(BuildContext context) {
bool isSingleMarkerMode = markerNotifier(renderUI: true).singletonMarker;
return Scaffold(
appBar: CustomAppBar(title: "Advanced markers"),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: ActionChip(
backgroundColor: isSingleMarkerMode
? KConstantColors.blueColor
: KConstantColors.purpleColor,
label: Text(
!isSingleMarkerMode ? "Multiple mode" : "Single mode",
style: KConstantTextstyles.light(fontSize: 12),
),
onPressed: () {
markerNotifier(renderUI: false).toggleMarkerMode();
}),
backgroundColor: KConstantColors.bgColor,
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
child: Container(
child: GoogleMap(
onTap: (latLng) {
markerNotifier(renderUI: false)
.setMarkerOnMap(latLng: latLng);
},
myLocationEnabled: true,
markers: Set.from(markerNotifier(renderUI: true).markers),
mapToolbarEnabled: true,
initialCameraPosition: CameraPosition(
target: LatLng(12.9716, 77.5946), zoom: 16),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
googleMapController = controller;
}),
),
)),
);
}
}