location 1.3.2 location: ^1.3.2 copied to clipboard
A Flutter plugin for location with Callbacks.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:location/location.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
Map<String, double> _currentLocation;
StreamSubscription<Map<String, double>> _locationSubscription;
Location _location = new Location();
String error;
bool currentWidget = true;
Image image1;
@override
void initState() {
super.initState();
initPlatformState();
_locationSubscription =
_location.onLocationChanged.listen((Map<String,double> result) {
setState(() {
_currentLocation = result;
});
});
}
// Platform messages are asynchronous, so we initialize in an async method.
initPlatformState() async {
Map<String, double> location;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
location = await _location.getLocation;
error = null;
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
error = 'Permission denied';
} else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
error = 'Permission denied - please ask the user to enable it from the app settings';
}
location = null;
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_currentLocation = location;
});
}
@override
Widget build(BuildContext context) {
List<Widget> widgets;
if (_currentLocation == null) {
widgets = new List();
} else {
widgets = [
new Image.network(
"https://maps.googleapis.com/maps/api/staticmap?center=${_currentLocation["latitude"]},${_currentLocation["longitude"]}&zoom=18&size=640x400&key=YOUR_API_KEY")
];
}
widgets.add(new Center(
child: new Text(_currentLocation != null
? '$_currentLocation\n'
: 'Error: $error\n')));
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Plugin example app'),
),
body: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: widgets,
)));
}
}