Saifallak
Saifallak

Reputation: 1349

how to Know which screen is on the top(active/mounted)

iam implementing app with Flutter that has a pin screen must be entered to access the app, pin shows every time you resume the app so iam using (didChangeAppLifecycleState) the problem here that if something interups the app like whatsapp notification the pin shows and thats cool ,, but if two messages comes , i get two pin codes and so one , once i got 27 pin codes i have to enter them to resume the app and that's not cool at all

so the question here , is there any method or way to know which screen is on the top of stack(Navigator Stack) ? i mean visible to the user right now, so i don't have to show pin again ,

thanks in advance

Upvotes: 0

Views: 743

Answers (1)

Kenneth Li
Kenneth Li

Reputation: 1742

Suppose you have 3 pages in your App, namely 'PageHome', 'Page1', 'PagePin'. Then:

  1. create a file 'GlobalVariables.dart', inside this file, create a class gv.

  2. Inside class gv, create a static var:

    static String strCurPage = 'PageHome';
    
  3. Import this dart file in all 3 pages.

  4. Before navigate to a new page, e.g. from 'PageHome' to 'PagePin', set:

    gv.strCurPage = 'PagePin';
    
  5. When the app resume from background, navigate to 'PagePin' only if gv.strCurPage != 'PagePin'.

Upvotes: 2

Related Questions