Flutter - How to hide or change widget as seen in Recent Apps overview

1,187

Solution 1

You should be using the Native Platform implementation of it. There is something called as window manager in android which you have to set it.

Paste this in MainActivity file and you should see it working.

@Override
protected void onPause() {
    super.onPause();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, 
     WindowManager.LayoutParams.FLAG_SECURE);

 }

@Override
protected void onResume() {
    super.onResume();
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
}

Solution 2

Are you saying this function is not called? If yes, you need to add the following,

@override
void initState() {
   super.initState();
   WidgetsBinding.instance.addObserver(this);
}

@override
void dispose() {
   WidgetsBinding.instance.removeObserver(this);
   super.dispose();
}
Share:
1,187
bbedward
Author by

bbedward

I'm a software engineer who is most comfortable in a Unix environment. Experience with: Java enterprise applications (JBoss, hibernate, etc - full stack) IBM Integration Bus IBM MQ Many relational DBs (Oracle SQL, mysql/mariadb, postgres, sqlite, mssql) Native Android applications. Modern web design stuff (html5, css3, JavaScript, jquery, bootstrap, socketio, etc) Python/Flask applications bash stuff asp.net in a former life Probably more

Updated on December 09, 2022

Comments

  • bbedward
    bbedward 11 months

    I'm developing a Flutter application that deals with cryptocurrency (a wallet). Some people have requested that the app takes some measures to hide their balance, etc.

    The one thing I can't figure out, however, is hiding information from the recent apps menu (like secure window mode on Android, or simply changing the interface so balance isn't visible.)

    I added a WidgetsBindingObserver to my main widget and tried something like:

    @override
    void didChangeAppLifecycleState(AppLifecycleState state) {
      switch (state) {
        case AppLifecycleState.paused:
          setState(() {
              _hideBalance = true;
          });
          super.didChangeAppLifecycleState(state);
          break;
        case AppLifecycleState.resumed:
          setState(() {
              _hideBalance = false;
          });
          super.didChangeAppLifecycleState(state);
          break;
        default:
          super.didChangeAppLifecycleState(state);
          break;
      }
    }
    
    @override
    void initState() {
       super.initState();
       WidgetsBinding.instance.addObserver(this);
    }
    
    @override
    void dispose() {
       WidgetsBinding.instance.removeObserver(this);
       super.dispose();
    }
    
    @override
    Widget build(BuildContext context) {
        return _hideBalance ? SizedBox() : Text("$12,234");
    }
    

    However, this has no effect on the recent apps which appears to be an exact snapshot from the moment the home button was pressed (on my OnePlus 6T at least). E.g. - I can still see the balance in the recent apps/app switcher overview.

    Is there a way to do this with flutter?

  • bbedward
    bbedward almost 5 years
    No sorry, the function is called - but it doesn't have an effect of the appearance in recent apps. E.g. _hideBalance hides the balance when it's true, if I removed the resumed case then the balance would be hidden still after resuming the app - it's just not hidden in the Android "recent apps" overview.
  • user1111
    user1111 almost 5 years
    The reason is, even though the state has changed, the build function is not called, therefore, the UI is not refreshed. I am not aware of a way to refresh the UI when the app is not active.