Display Material Banner on app start without user interaction

Viewed 131

I have an app that can either be in demo mode or live mode. When the app is in demo mode, I would like to display a MaterialBanner to indicate to the user that the app is in demo mode. I was able to display the MaterialBanner when the user turns demo mode on with a button, but I cannot find a way to make it display after restarting the app and have it display on every page. It only appears on the home page. The app has to restart to load the demo database.

Is there a way to display a MaterialBanner without user interaction that appears on every page of the app?

1 Answers

Use ScaffoldMessenger.of(context).showMaterialBanner() to programmatically show the MaterialBanner. I believe you will need to do this for each unique scaffold you build.

@override
void initState() {
  super.initState();
  WidgetsBinding.instance
      .addPostFrameCallback((_) => _showBanner());
}

void _showBanner() {
  ScaffoldMessenger.of(context).showMaterialBanner(...);
}
Related