You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.7 KiB
137 lines
3.7 KiB
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:location_plugin/location_plugin.dart';
|
|
|
|
void main() {
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatefulWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
State<MyApp> createState() => _MyAppState();
|
|
}
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
final _locationPlugin = LocationPlugin();
|
|
String _status = 'Idle';
|
|
String _cityStatus = 'Idle';
|
|
Map<String, double>? _location;
|
|
CityInfo? _cityInfo;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_fetchLocation();
|
|
}
|
|
|
|
Future<void> _fetchLocation() async {
|
|
setState(() {
|
|
_status = 'Fetching location...';
|
|
});
|
|
try {
|
|
final location = await _locationPlugin.getCurrentLocation();
|
|
if (!mounted) return;
|
|
if (location == null) {
|
|
setState(() {
|
|
_status = 'Location unavailable';
|
|
_cityStatus = 'Skip city lookup';
|
|
_cityInfo = null;
|
|
});
|
|
return;
|
|
}
|
|
setState(() {
|
|
_location = location;
|
|
_status = 'Success';
|
|
_cityStatus = 'Fetching city info...';
|
|
});
|
|
await _fetchCityInfo(location['latitude']!, location['longitude']!);
|
|
} on PlatformException catch (e) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_status = 'Failed: ${e.code} ${e.message ?? ''}'.trim();
|
|
_cityStatus = 'Skip city lookup';
|
|
_cityInfo = null;
|
|
});
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_status = 'Failed: $e';
|
|
_cityStatus = 'Skip city lookup';
|
|
_cityInfo = null;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> _fetchCityInfo(double latitude, double longitude) async {
|
|
try {
|
|
final info = await _locationPlugin.getCityInfo(latitude: latitude, longitude: longitude);
|
|
if (!mounted) return;
|
|
print(info);
|
|
setState(() {
|
|
_cityInfo = info;
|
|
_cityStatus = info == null ? 'No city info' : 'Success';
|
|
});
|
|
} on CityInfoLookupException catch (e) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_cityInfo = null;
|
|
_cityStatus = 'Failed: ${e.code}';
|
|
});
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_cityInfo = null;
|
|
_cityStatus = 'Failed: $e';
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final latitude = _location?['latitude'];
|
|
final longitude = _location?['longitude'];
|
|
final cityCode = _cityInfo?.code;
|
|
final region = _cityInfo?.region.join(' / ');
|
|
return MaterialApp(
|
|
home: Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Location Plugin Example'),
|
|
),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text('Status: $_status'),
|
|
const SizedBox(height: 12),
|
|
if (latitude != null && longitude != null)
|
|
Text('Current position: $latitude, $longitude'),
|
|
const SizedBox(height: 12),
|
|
Text('City lookup: $_cityStatus'),
|
|
if (cityCode != null)
|
|
Text('City code: $cityCode'),
|
|
if (region != null && region.isNotEmpty)
|
|
Text('Region: $region'),
|
|
const SizedBox(height: 24),
|
|
ElevatedButton(
|
|
onPressed: _fetchLocation,
|
|
child: const Text('Refresh'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: (){
|
|
_fetchCityInfo(latitude!, longitude!);
|
|
},
|
|
child: const Text('Refresh'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|