ajlocation 1.0.10 ajlocation: ^1.0.10 copied to clipboard
Plugin to allow you get users location for iOS and Android. You can also get the location updates in background mode. Allows you to check status of currently selected authorization.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:ajlocation/ajlocation.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Timer _timer;
int seconds = 0;
@override
void initState() {
super.initState();
}
void _startTimer() {
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
setState(() {
seconds += 1;
});
});
}
void _stopTimer() {
if (_timer != null) {
_timer.cancel();
_timer = null;
}
}
Future requestAlways() async {
var response = await AJLocation.requestAuthorization(type: AuthorizationType.always);
print(response.toString());
}
Future requestOnly() async {
var response = await AJLocation.requestAuthorization(type: AuthorizationType.inUse);
print(response.toString());
}
Future currentStatus() async {
var response = await AJLocation.currentAuthorizationStatus;
print(response.toString());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: <Widget>[
FlatButton(
child: Text("Request Authorization Always/Background"),
onPressed: () {
requestAlways();
},
),
FlatButton(
child: Text("Request Authorization Only when in use"),
onPressed: () {
requestOnly();
},
),
FlatButton(
child: Text("Authorization Status"),
onPressed: () {
currentStatus();
},
),
FlatButton(
child: Text("Set configuration"),
onPressed: () {
AJLocation.setConfiguartion(accuracy: AccuracyType.nearestTenMeters, distanceFilter: 5.0);
},
),
FlatButton(
child: Text("Current Location"),
onPressed: () {
AJLocation.getCurrentLocation();
},
),
FlatButton(
child: Text("Start updating location"),
onPressed: () {
seconds = 0;
_startTimer();
AJLocation.startUpdatingLocation();
},
),
FlatButton(
child: Text("Stop updating location"),
onPressed: () {
_stopTimer();
AJLocation.stopUpdatingLocation();
},
),
FlatButton(
child: Text("Listen to changes"),
onPressed: () {
AJLocation.onLocationChange.listen((data) {
print(data.timestamp);
});
},
),
Container(
margin: EdgeInsets.only(top: 64),
child: Text(seconds.toString(), style: TextStyle(fontSize: 24)),
)
],
)
),
),
);
}
}