Last active
April 11, 2020 06:07
-
-
Save epsi95/98e6759aca5f5047a6a086e04b9c510e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_map/flutter_map.dart'; | |
import 'package:latlong/latlong.dart'; | |
import 'package:location/location.dart'; | |
class ReportCorona extends StatefulWidget { | |
@override | |
_ReportCoronaState createState() => _ReportCoronaState(); | |
} | |
class _ReportCoronaState extends State<ReportCorona> | |
with SingleTickerProviderStateMixin { | |
AnimationController controller; | |
Location location = Location(); | |
bool _serviceEnabled; | |
PermissionStatus _permissionGranted; | |
LocationData _locationData; | |
MapController _mapController = MapController(); | |
double baseTimeForMapAnimation = 0.0; | |
double deltaTimeForMapAnimation = 1 / 60; | |
List<Marker> _markers = [ | |
Marker( | |
width: 80.0, | |
height: 80.0, | |
point: LatLng(51.5, -0.09), | |
builder: (ctx) => Container( | |
child: FlutterLogo(), | |
), | |
) | |
]; | |
@override | |
void initState() { | |
controller = | |
AnimationController(vsync: this, duration: Duration(seconds: 1)); | |
controller.addStatusListener((status) { | |
if (status == AnimationStatus.completed) { | |
controller.reverse(); | |
} else if (status == AnimationStatus.dismissed) { | |
controller.forward(); | |
} | |
}); | |
controller.forward(); | |
controller.addListener(() { | |
setState(() {}); | |
}); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
controller.dispose(); | |
super.dispose(); | |
} | |
void initiateLocation() async { | |
_serviceEnabled = await location.serviceEnabled(); | |
if (!_serviceEnabled) { | |
_serviceEnabled = await location.requestService(); | |
if (!_serviceEnabled) { | |
return; | |
} | |
} | |
_permissionGranted = await location.hasPermission(); | |
if (_permissionGranted == PermissionStatus.denied) { | |
_permissionGranted = await location.requestPermission(); | |
if (_permissionGranted != PermissionStatus.granted) { | |
return; | |
} | |
} | |
_locationData = await location.getLocation(); | |
location.onLocationChanged.listen((LocationData currentLocation) { | |
// Use current location | |
updateLocation(currentLocation); | |
}); | |
} | |
void updateLocation(LocationData locationData) {} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
floatingActionButton: FloatingActionButton( | |
child: Text('+'), | |
onPressed: () { | |
baseTimeForMapAnimation = 0.0; | |
_mapController.onReady.then((result) { | |
LatLng topLeft = _mapController.bounds.northWest; | |
LatLng topRight = _mapController.bounds.northEast; | |
LatLng bottomRight = _mapController.bounds.southWest; | |
LatLng currentCenter = LatLng( | |
(topLeft.latitude + bottomRight.latitude) / 2, | |
(topLeft.longitude + topRight.longitude) / 2); | |
LatLng finalCenter = LatLng(51.5, -0.09); | |
double currentZoomLevel = _mapController.zoom; | |
double finalZoomLevel = 13.0; | |
controller.addListener(() { | |
if (baseTimeForMapAnimation <= 1) { | |
if (currentZoomLevel != finalZoomLevel) { | |
double intermediateZoomLevel = | |
(finalZoomLevel - currentZoomLevel) * | |
(baseTimeForMapAnimation * | |
baseTimeForMapAnimation * | |
(3.0 - 2.0 * baseTimeForMapAnimation)) + | |
currentZoomLevel; | |
double intermediateLat = | |
(finalCenter.latitude - currentCenter.latitude) * | |
(baseTimeForMapAnimation * | |
baseTimeForMapAnimation * | |
(3.0 - 2.0 * baseTimeForMapAnimation)) + | |
currentCenter.latitude; | |
double intermediateLon = | |
(finalCenter.longitude - currentCenter.longitude) * | |
(baseTimeForMapAnimation * | |
baseTimeForMapAnimation * | |
(3.0 - 2.0 * baseTimeForMapAnimation)) + | |
currentCenter.longitude; | |
print(intermediateZoomLevel); | |
_mapController.move(LatLng(intermediateLat, intermediateLon), | |
intermediateZoomLevel); | |
baseTimeForMapAnimation += deltaTimeForMapAnimation; | |
} | |
} else if (baseTimeForMapAnimation > 1) { | |
return; | |
} | |
}); | |
// if(currentZoomLevel > ) | |
// print("FAB tapped"); | |
// print(zoomAnimation.evaluate(animation)); | |
// print(controller.value); | |
// _mapController.move(LatLng(51.5, -0.09), 13.0); | |
}); | |
}, | |
), | |
body: FlutterMap( | |
mapController: _mapController, | |
options: MapOptions( | |
onTap: (LatLng tappedLocation) { | |
print(tappedLocation); | |
_markers.add( | |
Marker( | |
width: 80.0, | |
height: 80.0, | |
point: tappedLocation, | |
builder: (ctx) => Container( | |
child: FlutterLogo(), | |
), | |
), | |
); | |
setState(() {}); | |
}, | |
center: LatLng(51.5, -0.09), | |
zoom: 13.0, | |
), | |
layers: [ | |
TileLayerOptions( | |
urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", | |
subdomains: ['a', 'b', 'c'], | |
), | |
MarkerLayerOptions( | |
markers: _markers, | |
), | |
CircleLayerOptions( | |
circles: [ | |
CircleMarker( | |
point: LatLng(51.5, -0.09), | |
radius: 1000.0, | |
useRadiusInMeter: true, | |
color: Colors.red.withOpacity(0.5), | |
borderColor: Colors.red, | |
borderStrokeWidth: 5.0), | |
], | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment