创建一个使用 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");
},
),
),
);
}
}