smart_location 0.0.2
smart_location: ^0.0.2 copied to clipboard
Smart Location is a Flutter package that provides an all-in-one solution for location handling, including real-time updates, last known location, geofencing, distance & bearing calculations, backgroun [...]
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:smart_location/smart_location.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: ExampleScreen(),
debugShowCheckedModeBanner: false,
);
}
}
class ExampleScreen extends StatefulWidget {
const ExampleScreen({super.key});
@override
State<ExampleScreen> createState() => _ExampleScreenState();
}
class _ExampleScreenState extends State<ExampleScreen> {
String _status = 'Press the button to get location.';
Future<void> _fetchLocation() async {
setState(() => _status = 'Fetching location...');
try {
await SmartLocation.ensureReady();
final loc = await SmartLocation.current();
setState(() => _status = 'Success!\nLatitude: ${loc.latitude}\nLongitude: ${loc.longitude}');
} on LocationDisabledException {
setState(() => _status = 'Please enable GPS then try again.');
} on PermissionDeniedException {
setState(() => _status = 'Location permission denied.');
} on PermissionPermanentlyDeniedException {
setState(() => _status = 'Permission permanently denied. Please open your phone settings.');
} catch (e) {
setState(() => _status = 'Error: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Smart Location Test')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.location_on, size: 64, color: Colors.blue),
const SizedBox(height: 20),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 32.0),
child: Text(
_status,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 18),
),
),
const SizedBox(height: 40),
ElevatedButton(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
textStyle: const TextStyle(fontSize: 18)
),
onPressed: _fetchLocation,
child: const Text('Get Current Location'),
),
],
),
),
);
}
}