showConfirmationDialogue method Null safety

void showConfirmationDialogue(
  1. {required BuildContext context,
  2. required String title,
  3. required String message,
  4. required String confirmationActionText,
  5. required Future<void> onConfirm(
      )}
    )

    Shows a confirmation dialogue to allow the user to confirm some action. For example, so save a message or save a screen's settings.

    The confirmationActionText is required to customise the text in the confirmation button.

    One action is to 'Cancel' which closes the dialogue, doing nothing. The other action is 'confirmationActionText' which executes the confirmation action onConfirm.

    Implementation

    static void showConfirmationDialogue(
        {required BuildContext context,
        required String title,
        required String message,
        required String confirmationActionText,
        required Future<void> Function() onConfirm}) {
      showDialog(
        context: context,
        builder: (BuildContext context) => AlertDialog(
          title: Text(title),
          content: Text(message),
          actions: <Widget>[
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: const Text('Cancel'),
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all(Colors.black),
              ),
            ),
            TextButton(
              onPressed: () async {
                await onConfirm();
                Navigator.pop(context);
              },
              child: Text(confirmationActionText),
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all(Colors.green),
              ),
            ),
          ],
        ),
      );
    }