123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import 'package:flutter/material.dart';
- import 'package:background_location/background_location.dart';
- import 'package:flutter/foundation.dart';
- class BgLocation extends StatefulWidget {
- static const routeName = 'bg-location';
- const BgLocation({super.key});
- @override
- State<BgLocation> createState() => _BgLocationState();
- }
- class _BgLocationState extends State<BgLocation> {
- String latitude = 'waiting...';
- String longitude = 'waiting...';
- String altitude = 'waiting...';
- String accuracy = 'waiting...';
- String bearing = 'waiting...';
- String speed = 'waiting...';
- String time = 'waiting...';
- @override
- void initState() {
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: const Text('Background Location Service'),
- ),
- body: Center(
- child: ListView(
- children: <Widget>[
- locationData('Latitude: $latitude'),
- locationData('Longitude: $longitude'),
- locationData('Altitude: $altitude'),
- locationData('Accuracy: $accuracy'),
- locationData('Bearing: $bearing'),
- locationData('Speed: $speed'),
- locationData('Time: $time'),
- ElevatedButton(
- onPressed: () async {
- await BackgroundLocation.setAndroidNotification(
- title: 'Background service is running',
- message: 'Background location in progress',
- icon: '@mipmap/ic_launcher',
- );
- await BackgroundLocation.setAndroidConfiguration(60000);
- await BackgroundLocation.startLocationService(
- distanceFilter: 20);
- BackgroundLocation.getLocationUpdates((location) {
- setState(() {
- latitude = location.latitude.toString();
- longitude = location.longitude.toString();
- accuracy = location.accuracy.toString();
- altitude = location.altitude.toString();
- bearing = location.bearing.toString();
- speed = location.speed.toString();
- time = DateTime.fromMillisecondsSinceEpoch(
- location.time!.toInt())
- .toString();
- });
- if (kDebugMode) {
- print('''\n
- Latitude: $latitude
- Longitude: $longitude
- Altitude: $altitude
- Accuracy: $accuracy
- Bearing: $bearing
- Speed: $speed
- Time: $time
- ''');
- }
- });
- },
- child: const Text('Start Location Service')),
- ElevatedButton(
- onPressed: () {
- BackgroundLocation.stopLocationService();
- },
- child: const Text('Stop Location Service')),
- ElevatedButton(
- onPressed: () {
- getCurrentLocation();
- },
- child: const Text('Get Current Location')),
- ],
- ),
- ),
- );
- }
- Widget locationData(String data) {
- return Text(
- data,
- style: const TextStyle(
- fontWeight: FontWeight.bold,
- fontSize: 18,
- ),
- textAlign: TextAlign.center,
- );
- }
- void getCurrentLocation() {
- BackgroundLocation().getCurrentLocation().then((location) {
- if (kDebugMode) {
- print('This is current Location ${location.toMap()}');
- }
- });
- }
- @override
- void dispose() {
- BackgroundLocation.stopLocationService();
- super.dispose();
- }
- }
|