Home > Blockchain >  How to open a standalone screen in flutter?
How to open a standalone screen in flutter?

Time:02-04

I am creating an alarm clock application, and I want to show a full screen page to allow the user to dismiss the alarm when it triggers. Thats all working well but the issue arises when I want to close that page.

What I have tried

Currently, when the alarm triggers, I am pushing that page onto the navigation stack to make it visible:

App.navigatorKey.currentState?.pushNamedAndRemoveUntil(
  alarmNotificationRoute,
  (route) {
    return (route.settings.name != '/alarm-notification') ||
        route.isFirst;
  },
);

And then pop it when user presses "Dismiss":

if (App.navigatorKey.currentState?.canPop() ?? false) {
     App.navigatorKey.currentState?.pop();
}

My App routing code:

class App extends StatefulWidget {
  const App({super.key});

  static final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(      
      ...
      navigatorKey: App.navigatorKey,
      initialRoute: '/',
      onGenerateRoute: (settings) {
        switch (settings.name) {
          case '/':
            return MaterialPageRoute(builder: (context) => const RootScreen());

          case '/alarm-notification':
            return MaterialPageRoute(
              builder: (context) {
                return AlarmNotificationScreen();
              },
            );

          default:
            assert(false, 'Page ${settings.name} not found');
            return null;
        }
      },
    );
  }
}

Current behavior

Now when I pop, it returns to the default route of the flutter app '/', even when the alarm triggered while the app was closed.

Expected behavior

The behavior I want is as follows:

  1. If the app was in the foreground when alarm triggered, pressing dismiss should go back to the last screen (this is already working as expected)
  2. If the app was in the background or closed when alarm triggered, pressing dismiss should send the app to background
  3. If android decides to show a Heads Up Notification instead of a full page intent. pressing dismiss should do nothing

Thoughts

I am thinking that the cleanest way to do so would be to launch a standalone page/activity, which we can just close when we press dismiss. Is there anyway to do such a thing? I am fine with it being an android-only solution.

CodePudding user response:

There appears to be a minimize_app package that does the "close to background" behavior you want. From there it's simply a matter of tracking where the page was navigated from and using conditional logic.

A possible implementation:

import 'package:minimize_app/minimize_app.dart';

...

// Set this variable when the app is opened via the alarm trigger
if (appWasInBackground) {
  MinimizeApp().minimizeApp();
} else if (App.navigatorKey.currentState?.canPop() ?? false) {
  App.navigatorKey.currentState?.pop();
}
  • Related