vk_location_sharing 0.0.1
vk_location_sharing: ^0.0.1 copied to clipboard
A Flutter package for implementing location sharing with foreground streams and periodic background updates.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:vk_location_sharing/vk_location_sharing.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Location Sharing Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const LocationHome(),
);
}
}
class LocationHome extends StatefulWidget {
const LocationHome({super.key});
@override
State<LocationHome> createState() => _LocationHomeState();
}
class _LocationHomeState extends State<LocationHome> {
final _vkLocation = VkLocationSharing();
Position? _currentPosition;
bool _isForegroundRunning = false;
bool _isBackgroundRunning = false;
@override
void initState() {
super.initState();
_initBackground();
}
Future<void> _initBackground() async {
await _vkLocation.initializeBackground();
}
void _startForeground() async {
final stream = await _vkLocation.startForegroundSharing(
accuracy: LocationAccuracy.best,
distanceFilter: 5,
onLocationUpdate: (position) {
setState(() {
_currentPosition = position;
});
},
);
if (stream != null) {
setState(() {
_isForegroundRunning = true;
});
}
}
void _stopForeground() async {
await _vkLocation.stopForegroundSharing();
setState(() {
_isForegroundRunning = false;
});
}
void _startBackground() async {
try {
await _vkLocation.startBackgroundSharing();
setState(() {
_isBackgroundRunning = true;
});
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Background Sharing Started')),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: $e')),
);
}
}
}
void _stopBackground() async {
await _vkLocation.stopBackgroundSharing();
setState(() {
_isBackgroundRunning = false;
});
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Background Sharing Stopped')),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Location Sharing'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_currentPosition != null)
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text('Lat: ${_currentPosition!.latitude}'),
Text('Lng: ${_currentPosition!.longitude}'),
Text('Time: ${_currentPosition!.timestamp}'),
],
),
),
)
else
const Text('No location data yet'),
const SizedBox(height: 30),
ElevatedButton(
onPressed:
_isForegroundRunning ? _stopForeground : _startForeground,
style: ElevatedButton.styleFrom(
backgroundColor:
_isForegroundRunning ? Colors.red.shade100 : null,
),
child: Text(_isForegroundRunning
? 'Stop Foreground Sharing'
: 'Start Foreground Sharing'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed:
_isBackgroundRunning ? _stopBackground : _startBackground,
style: ElevatedButton.styleFrom(
backgroundColor:
_isBackgroundRunning ? Colors.orange.shade100 : null,
),
child: Text(_isBackgroundRunning
? 'Stop Background Sharing'
: 'Start Background Sharing'),
),
],
),
),
),
);
}
}