showLocationDisclosureDialogAfterFrame function

Future<bool> showLocationDisclosureDialogAfterFrame()

Implementation

Future<bool> showLocationDisclosureDialogAfterFrame() async {
  bool? result;
  // Use a Completer to wait for the dialog result
  Completer<bool> completer = Completer<bool>();

  WidgetsBinding.instance.addPostFrameCallback((_) async {
    result = await showDialog<bool>(
          context: navigatorKey.currentState!.context,
          barrierDismissible: false, // User must tap button to close dialog
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text(localizedText('Location Permission Needed')),
              content: Text(localizedText(
                  'To enhance your experience, this app requests access to your location data to provide turn-by-turn directions, find nearby places, and offer personalized recommendations based on your current location.')),
              actions: <Widget>[
                TextButton(
                  child: Text(localizedText('Decline')),
                  onPressed: () => Navigator.of(context).pop(false),
                ),
                TextButton(
                  child: Text(localizedText('Agree')),
                  onPressed: () => Navigator.of(context).pop(true),
                ),
              ],
            );
          },
        ) ??
        false; // Default to false if dialog is dismissed

    completer.complete(result);
  });

  return completer.future; // Wait for the dialog result
}