Маршрут трепетанияАвария и RouteObserver с КупертиноPageRoute - PullRequest
0 голосов
/ 03 сентября 2018

Создание приложения, в котором навигация осуществляется с помощью CupertinoPageRoute вместо MaterialPageRoute. Я использовал RouteAware и RouteObserver, который прекрасно работает с MaterialPageRoute. Однако я использую CupertinoTabView, который должен быть постоянно видимым для пользователя. Я могу отлично перемещаться от экрана к экрану, и CupertinoTabView всегда виден, но я не могу определить, когда экран появился снова. Хотите знать, что нужно изменить, чтобы я мог определить, когда возвращаюсь к предыдущему экрану. Обычно void didPopNext () срабатывает при возврате к начальному экрану.

    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");
          },
        ),
      ),
    );
  }
}
...