Flutter RouteAware and RouteObserver with CupertinoPageRoute

4.5k Views Asked by At

Creating an app where navigation is done with CupertinoPageRoute instead of MaterialPageRoute. I have used RouteAware and RouteObserver which works perfectly fine with MaterialPageRoute. However I am using a CupertinoTabView which should be visible all the time for the user. I can perfectly navigate from screen to screen and the CupertinoTabView is always visible but I am not able to detect when the screen appeared again. Wondering what needs to be changed so I can detect when moving back to prior screen. Normally void didPopNext() does fire when going back to the initial screen.

final RouteObserver<PageRoute> routeObserver = new RouteObserver<PageRoute>();

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: First(),
      navigatorObservers: [routeObserver],
      routes: {},
    );
  }
}

class First extends StatefulWidget {
  @override
  _FirstState createState() => new _FirstState();
}

class _FirstState extends State<First> with RouteAware{
  @override
  Widget build(BuildContext context) {
    return new CupertinoTabScaffold(
      tabBuilder: (BuildContext context, int index) {
        //print("Index: $index");
        switch (index) {
          case 0:
            return new Home();
            break;
          case 1:
            return new Home2();
            break;
          default:
        }
      },
      tabBar: new CupertinoTabBar(
        backgroundColor: Colors.blueAccent[100],
        activeColor: Colors.deepOrange,
        inactiveColor: Colors.limeAccent,
        currentIndex: 0,
        onTap: (int i){if(i == 0){
        }},
        items: <BottomNavigationBarItem>[
          new BottomNavigationBarItem(
            icon: new Icon(Icons.home),
            title: new Text("Home"),
            backgroundColor: Color.fromRGBO(237, 124, 63, 1.0)),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.people),
            title: new Text("Home 2"),
            backgroundColor: Color.fromRGBO(237, 124, 63, 1.0)),
        ],
      ),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => new _HomeState();
}

class _HomeState extends State<Home> with RouteAware{
  @override
  void initState() {
    print("Appeared");
    super.initState();
  }

  @override
  void dispose() {
    print("Disposed");
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    print("Change dependencies!!!!");
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context));
  }

  @override
  void didPush() {
    // Route was pushed onto navigator and is now topmost route.
    print("Did Push!");
  }

  @override
  void didPopNext() {
    // Covering route was popped off the navigator.
    print("Did Pop Next");
  }

  @override
  void didPushNext() {
    print("Did Push Next");
    super.didPushNext();
  }

  @override
  Widget build(BuildContext context) {
    return new DefaultTextStyle(
      style: const TextStyle(
        fontFamily: '.SF UI Text',
        fontSize: 17.0,
        color: CupertinoColors.black,
      ),
      child: new CupertinoTabView(
        builder: (BuildContext context) {
          return new CupertinoPageScaffold(
            navigationBar: new CupertinoNavigationBar(
              middle: new Text("New Page"),
            ),
            child: new Center(
                child: new RaisedButton(
              child: new Text("Press me"),
              onPressed: () {
                Navigator.of(context).push(
                    new CupertinoPageRoute(builder: (BuildContext context) {
                  return new Home4();
                }));
              },
            )),
          );
        },
      ),
    );
  }
}

class Home2 extends StatefulWidget {
  @override
  _Home2State createState() => new _Home2State();
}

class _Home2State extends State<Home2> {
  @override
  Widget build(BuildContext context) {
    return new DefaultTextStyle(
      style: const TextStyle(
        fontFamily: '.SF UI Text',
        fontSize: 17.0,
        color: CupertinoColors.black,
      ),
      child: new CupertinoTabView(
        builder: (BuildContext context) {
          return new CupertinoPageScaffold(
            navigationBar: new CupertinoNavigationBar(
              middle: new Text("Page 2"),
            ),
            child: new Center(
              child: new RaisedButton(
                child: new Text("Go further"),
                onPressed: () {
                  print("Further pressed");
                  Navigator.of(context).push(
                    new CupertinoPageRoute(builder: (BuildContext context) {
                      return new Home3();
                    })
                  );
                },
              ),
            ),
          );
        },
      ),
    );
  }
}

class Home3 extends StatefulWidget {
  @override
  _Home3State createState() => new _Home3State();
}

class _Home3State extends State<Home3> {
  @override
  Widget build(BuildContext context) {
    return new DefaultTextStyle(
      style: const TextStyle(
        fontFamily: '.SF UI Text',
        fontSize: 17.0,
        color: CupertinoColors.black,
      ),
      child: new CupertinoTabView(
        builder: (BuildContext context) {
          return new CupertinoPageScaffold(
            navigationBar: new CupertinoNavigationBar(
              middle: new Text("Page 3"),
            ),
            child: new Center(
              child: new RaisedButton(
                child: new Text("Even further"),
                onPressed: () {
                  print("Even pressed");
                },
              ),
            ),
          );
        },
      ),
    );
  }
}

class Home4 extends StatefulWidget {
  @override
  _Home4State createState() => new _Home4State();
}

class _Home4State extends State<Home4> {
  @override
  Widget build(BuildContext context) {
    return new CupertinoPageScaffold(
      navigationBar: new CupertinoNavigationBar(
        middle: new Text("Page 4"),
        backgroundColor: Colors.amberAccent,
        actionsForegroundColor: Colors.black12,
      ),
      child: new Scaffold(
        body: new Column(
          children: <Widget>[
            new Padding(
              padding: const EdgeInsets.only(top: 80.0),
              child: new RaisedButton(
                child: new Text("Even further"),
                onPressed: () {
                  print("4 pressed");
                  Navigator.of(context).push(
                      new CupertinoPageRoute(builder: (BuildContext context) {
                    return new Home5();
                  }));
                },
              ),
            ),
            new Padding(
              padding: const EdgeInsets.only(top: 80.0),
              child: new RaisedButton(
                child: new Text("Back"),
                onPressed: () {
                  print("Back pressed");
                  Navigator.pop(context);
                },
              ),
            ),
          ],
        )
      )
    );
  }
}

class Home5 extends StatefulWidget {
  @override
  _Home5State createState() => new _Home5State();
}

class _Home5State extends State<Home5> {
  @override
  Widget build(BuildContext context) {
    return new CupertinoPageScaffold(
      navigationBar: new CupertinoNavigationBar(
        middle: new Text("Page 5"),
      ),
      child: new Center(
        child: new RaisedButton(
          child: new Text("Even further"),
          onPressed: () {
            print("5 pressed");
          },
        ),
      ),
    );
  }
}
0

There are 0 best solutions below