feat(url-launcher): Integrate url_launcher plugin for opening directions in maps
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:design_system/design_system.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import 'shift_location_map.dart';
|
||||
|
||||
/// A section displaying the shift's location, address, map, and "Get direction" action.
|
||||
@@ -69,18 +70,7 @@ class ShiftLocationSection extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
OutlinedButton.icon(
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
shift.locationAddress.isNotEmpty
|
||||
? shift.locationAddress
|
||||
: shift.location,
|
||||
),
|
||||
duration: const Duration(seconds: 3),
|
||||
),
|
||||
);
|
||||
},
|
||||
onPressed: () => _openDirections(context),
|
||||
icon: const Icon(
|
||||
UiIcons.navigation,
|
||||
size: UiConstants.iconXs,
|
||||
@@ -114,4 +104,30 @@ class ShiftLocationSection extends StatelessWidget {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _openDirections(BuildContext context) async {
|
||||
final destination = (shift.latitude != null && shift.longitude != null)
|
||||
? '${shift.latitude},${shift.longitude}'
|
||||
: Uri.encodeComponent(
|
||||
shift.locationAddress.isNotEmpty
|
||||
? shift.locationAddress
|
||||
: shift.location,
|
||||
);
|
||||
|
||||
final String url =
|
||||
'https://www.google.com/maps/dir/?api=1&destination=$destination';
|
||||
final uri = Uri.parse(url);
|
||||
|
||||
if (await canLaunchUrl(uri)) {
|
||||
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
||||
} else {
|
||||
if (context.mounted) {
|
||||
UiSnackbar.show(
|
||||
context,
|
||||
message: 'Could not open maps',
|
||||
type: UiSnackbarType.error,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ dependencies:
|
||||
equatable: ^2.0.5
|
||||
intl: ^0.20.2
|
||||
google_maps_flutter: ^2.14.2
|
||||
url_launcher: ^6.3.1
|
||||
firebase_auth: ^6.1.4
|
||||
firebase_data_connect: ^0.2.2+2
|
||||
|
||||
|
||||
Reference in New Issue
Block a user