showRestartBanner function

void showRestartBanner(
  1. BuildContext context, {
  2. required OnRestartCallback onRestart,
  3. String message = 'An update has been downloaded and will apply on restart.',
})

Shows a MaterialBanner telling the tester a patch has been downloaded and will apply on the next restart, with a "Restart now" action.

Call this after AirBuildCodePush.update completes (i.e. once AirBuildCodePush.checkForUpdate has returned UpdateStatus.restartRequired, or right after a successful update() call) — not before, since the patch isn't downloaded yet.

final status = await codePush.checkForUpdate();
if (status == UpdateStatus.outdated) {
  await codePush.update();
  if (context.mounted) {
    showRestartBanner(context, onRestart: () {
      // e.g. Phoenix.rebirth(context), or your own app-restart logic.
    });
  }
}

Implementation

void showRestartBanner(
  BuildContext context, {
  required OnRestartCallback onRestart,
  String message = 'An update has been downloaded and will apply on restart.',
}) {
  ScaffoldMessenger.of(context)
    ..hideCurrentMaterialBanner()
    ..showMaterialBanner(
      MaterialBanner(
        content: Text(message),
        actions: [
          TextButton(
            onPressed: () {
              ScaffoldMessenger.of(context).hideCurrentMaterialBanner();
            },
            child: const Text('Later'),
          ),
          FilledButton(
            onPressed: () {
              ScaffoldMessenger.of(context).hideCurrentMaterialBanner();
              onRestart();
            },
            child: const Text('Restart now'),
          ),
        ],
      ),
    );
}