-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
center_fab_example.dart
90 lines (84 loc) · 2.88 KB
/
center_fab_example.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
86
87
88
89
90
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
import 'package:latlong2/latlong.dart';
class CenterFabExample extends StatefulWidget {
@override
_CenterFabExampleState createState() => _CenterFabExampleState();
}
class _CenterFabExampleState extends State<CenterFabExample> {
late AlignOnUpdate _alignPositionOnUpdate;
late final StreamController<double?> _alignPositionStreamController;
@override
void initState() {
super.initState();
_alignPositionOnUpdate = AlignOnUpdate.always;
_alignPositionStreamController = StreamController<double?>();
}
@override
void dispose() {
_alignPositionStreamController.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Center FAB Example'),
),
body: FlutterMap(
options: MapOptions(
initialCenter: const LatLng(0, 0),
initialZoom: 1,
minZoom: 0,
maxZoom: 19,
// Stop aligning the location marker to the center of the map widget
// if user interacted with the map.
onPositionChanged: (MapCamera camera, bool hasGesture) {
if (hasGesture && _alignPositionOnUpdate != AlignOnUpdate.never) {
setState(
() => _alignPositionOnUpdate = AlignOnUpdate.never,
);
}
},
),
// ignore: sort_child_properties_last
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName:
'net.tlserver6y.flutter_map_location_marker.example',
maxZoom: 19,
),
CurrentLocationLayer(
alignPositionStream: _alignPositionStreamController.stream,
alignPositionOnUpdate: _alignPositionOnUpdate,
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: FloatingActionButton(
onPressed: () {
// Align the location marker to the center of the map widget
// on location update until user interact with the map.
setState(
() => _alignPositionOnUpdate = AlignOnUpdate.always,
);
// Align the location marker to the center of the map widget
// and zoom the map to level 18.
_alignPositionStreamController.add(18);
},
child: const Icon(
Icons.my_location,
color: Colors.white,
),
),
),
),
],
),
);
}
}