wurong
6 months ago
9 changed files with 415 additions and 50 deletions
@ -0,0 +1,119 @@
|
||||
import 'dart:async'; |
||||
|
||||
import 'package:flutter/cupertino.dart'; |
||||
import 'package:flutter/material.dart'; |
||||
|
||||
import 'package:geolocator/geolocator.dart'; |
||||
|
||||
class SendLocationDialog extends StatefulWidget { |
||||
|
||||
const SendLocationDialog({ |
||||
Key key, |
||||
}) : super(key: key); |
||||
|
||||
@override |
||||
SendLocationDialogState createState() => SendLocationDialogState(); |
||||
} |
||||
|
||||
class SendLocationDialogState extends State<SendLocationDialog> { |
||||
bool disabled = false; |
||||
bool denied = false; |
||||
bool isSending = false; |
||||
Position position; |
||||
Object error; |
||||
|
||||
@override |
||||
void initState() { |
||||
super.initState(); |
||||
requestLocation(); |
||||
} |
||||
|
||||
Future<void> requestLocation() async { |
||||
if (!(await Geolocator.isLocationServiceEnabled())) { |
||||
setState(() => disabled = true); |
||||
return; |
||||
} |
||||
var permission = await Geolocator.checkPermission(); |
||||
if (permission == LocationPermission.denied) { |
||||
permission = await Geolocator.requestPermission(); |
||||
if (permission == LocationPermission.denied) { |
||||
setState(() => denied = true); |
||||
return; |
||||
} |
||||
} |
||||
if (permission == LocationPermission.deniedForever) { |
||||
setState(() => denied = true); |
||||
return; |
||||
} |
||||
try { |
||||
Position position; |
||||
try { |
||||
position = await Geolocator.getCurrentPosition( |
||||
desiredAccuracy: LocationAccuracy.best, |
||||
timeLimit: const Duration(seconds: 30), |
||||
); |
||||
} on TimeoutException { |
||||
position = await Geolocator.getCurrentPosition( |
||||
desiredAccuracy: LocationAccuracy.medium, |
||||
timeLimit: const Duration(seconds: 30), |
||||
); |
||||
} |
||||
setState(() => this.position = position); |
||||
} catch (e) { |
||||
setState(() => error = e); |
||||
} |
||||
} |
||||
|
||||
void sendAction() async { |
||||
setState(() => isSending = true); |
||||
final body = |
||||
'https://www.openstreetmap.org/?mlat=${position.latitude}&mlon=${position.longitude}#map=16/${position.latitude}/${position.longitude}'; |
||||
final uri = |
||||
'geo:${position.latitude},${position.longitude};u=${position.accuracy}'; |
||||
Navigator.of(context, rootNavigator: false).pop(); |
||||
} |
||||
|
||||
@override |
||||
Widget build(BuildContext context) { |
||||
Widget contentWidget; |
||||
if (position != null) { |
||||
// contentWidget = MapBubble( |
||||
// latitude: position.latitude, |
||||
// longitude: position.longitude, |
||||
// ); |
||||
contentWidget = Text("MapBubble"); |
||||
} else if (disabled) { |
||||
contentWidget = Text("locationDisabledNotice"); |
||||
} else if (denied) { |
||||
contentWidget = Text("locationPermissionDeniedNotice"); |
||||
} else if (error != null) { |
||||
contentWidget = |
||||
Text(error.toString()); |
||||
} else { |
||||
contentWidget = Row( |
||||
mainAxisSize: MainAxisSize.min, |
||||
mainAxisAlignment: MainAxisAlignment.center, |
||||
children: [ |
||||
const CupertinoActivityIndicator(), |
||||
const SizedBox(width: 12), |
||||
Text("obtainingLocation"), |
||||
], |
||||
); |
||||
} |
||||
return AlertDialog( |
||||
title: Text("shareLocation"), |
||||
content: contentWidget, |
||||
actions: [ |
||||
TextButton( |
||||
onPressed: Navigator.of(context, rootNavigator: false).pop, |
||||
child: Text("cancel"), |
||||
), |
||||
if (position != null) |
||||
TextButton( |
||||
onPressed: isSending ? null : sendAction, |
||||
child: Text("send"), |
||||
), |
||||
], |
||||
); |
||||
} |
||||
} |
Loading…
Reference in new issue