Last active
September 27, 2024 13:36
-
-
Save christocracy/a0464846de8a9c27c7e9de5616082878 to your computer and use it in GitHub Desktop.
Flutter Background Geolocation hello_world.dart https://shop.transistorsoft.com/blogs/news
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
/// flutter_background_geolocation Hello World | |
/// https://github.com/transistorsoft/flutter_background_geolocation | |
//// | |
// For pretty-printing location JSON. Not a requirement of flutter_background_geolocation | |
// | |
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' | |
as bg; | |
JsonEncoder encoder = const JsonEncoder.withIndent(' '); | |
// | |
//// | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
late bool _isMoving; | |
late bool _enabled; | |
late String _motionActivity; | |
late String _odometer; | |
late String _content; | |
@override | |
void initState() { | |
_isMoving = false; | |
_enabled = false; | |
_content = ''; | |
_motionActivity = 'UNKNOWN'; | |
_odometer = '0'; | |
// 1. Listen to events (See docs for all 12 available events). | |
bg.BackgroundGeolocation.onLocation(_onLocation); | |
bg.BackgroundGeolocation.onMotionChange(_onMotionChange); | |
bg.BackgroundGeolocation.onActivityChange(_onActivityChange); | |
bg.BackgroundGeolocation.onProviderChange(_onProviderChange); | |
bg.BackgroundGeolocation.onConnectivityChange(_onConnectivityChange); | |
// 2. Configure the plugin | |
bg.BackgroundGeolocation.ready(bg.Config( | |
desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH, | |
distanceFilter: 10.0, | |
stopOnTerminate: false, | |
startOnBoot: true, | |
debug: true, | |
logLevel: bg.Config.LOG_LEVEL_VERBOSE, | |
reset: true)) | |
.then((bg.State state) { | |
setState(() { | |
_enabled = state.enabled; | |
_isMoving = state.isMoving == true; | |
}); | |
}); | |
} | |
void _onClickEnable(enabled) { | |
if (enabled) { | |
bg.BackgroundGeolocation.start().then((bg.State state) { | |
print('[start] success $state'); | |
setState(() { | |
_enabled = state.enabled; | |
_isMoving = state.isMoving == true; | |
}); | |
}); | |
} else { | |
bg.BackgroundGeolocation.stop().then((bg.State state) { | |
print('[stop] success: $state'); | |
// Reset odometer. | |
bg.BackgroundGeolocation.setOdometer(0.0); | |
setState(() { | |
_odometer = '0.0'; | |
_enabled = state.enabled; | |
_isMoving = state.isMoving == true; | |
}); | |
}); | |
} | |
} | |
// Manually toggle the tracking state: moving vs stationary | |
void _onClickChangePace() { | |
setState(() { | |
_isMoving = !_isMoving; | |
}); | |
print('[onClickChangePace] -> $_isMoving'); | |
bg.BackgroundGeolocation.changePace(_isMoving).then((bool isMoving) { | |
print('[changePace] success $isMoving'); | |
}).catchError((e) { | |
print('[changePace] ERROR: ${e.code}'); | |
}); | |
} | |
// Manually fetch the current position. | |
void _onClickGetCurrentPosition() { | |
bg.BackgroundGeolocation.getCurrentPosition( | |
persist: false, // <-- do not persist this location | |
desiredAccuracy: 0, // <-- desire best possible accuracy | |
timeout: 30000, // <-- wait 30s before giving up. | |
samples: 3 // <-- sample 3 location before selecting best. | |
) | |
.then((bg.Location location) { | |
print('[getCurrentPosition] - $location'); | |
}).catchError((error) { | |
print('[getCurrentPosition] ERROR: $error'); | |
}); | |
} | |
//// | |
// Event handlers | |
// | |
void _onLocation(bg.Location location) { | |
print('[location] - $location'); | |
final String odometerKM = (location.odometer / 1000.0).toStringAsFixed(1); | |
setState(() { | |
_content = encoder.convert(location.toMap()); | |
_odometer = odometerKM; | |
}); | |
} | |
void _onMotionChange(bg.Location location) { | |
print('[motionchange] - $location'); | |
} | |
void _onActivityChange(bg.ActivityChangeEvent event) { | |
print('[activitychange] - $event'); | |
setState(() { | |
_motionActivity = event.activity; | |
}); | |
} | |
void _onProviderChange(bg.ProviderChangeEvent event) { | |
print('$event'); | |
setState(() { | |
_content = encoder.convert(event.toMap()); | |
}); | |
} | |
void _onConnectivityChange(bg.ConnectivityChangeEvent event) { | |
print('$event'); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: | |
AppBar(title: const Text('Background Geolocation'), actions: <Widget>[ | |
Switch(value: _enabled, onChanged: _onClickEnable), | |
]), | |
body: SingleChildScrollView(child: Text(_content)), | |
bottomNavigationBar: BottomAppBar( | |
child: Container( | |
padding: const EdgeInsets.only(left: 5.0, right: 5.0), | |
child: Row( | |
mainAxisSize: MainAxisSize.max, | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
children: <Widget>[ | |
IconButton( | |
icon: const Icon(Icons.gps_fixed), | |
onPressed: _onClickGetCurrentPosition, | |
), | |
Text('$_motionActivity · $_odometer km'), | |
MaterialButton( | |
minWidth: 50.0, | |
color: _isMoving ? Colors.red : Colors.green, | |
onPressed: _onClickChangePace, | |
child: Icon(_isMoving ? Icons.pause : Icons.play_arrow, | |
color: Colors.white)) | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I already purchased the key for flutter_background_geolocation, but it still shows 'Location not available.