你可以在這裏使用同樣的方法。父窗口小部件可以有兩部分:導航器和永久視圖。通過推送路線,您將只更改導航器小部件。 演示:
import 'package:flutter/material.dart';
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
Route _onRoute(RouteSettings settings) {
final str = settings.name.split("/")[1];
final index = int.parse(str, onError: (s) => 0);
return new MaterialPageRoute(
builder: (BuildContext context) => new Home(index: index));
}
@override
Widget build(BuildContext context) {
return new Column(
children: <Widget>[
new Expanded(
child: new MaterialApp(
title: 'Flutter Demo',
onGenerateRoute: _onRoute,
),
),
new Container(
height: 44.0,
color: Colors.blueAccent,
child: new Center(
child: new Text("permanent view"),
),
)
],
);
}
}
class Home extends StatelessWidget {
Home({Key key, this.index}) : super(key: key);
final int index;
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(
title: new Text("View ${index}"),
),
body: new Center(
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Text("View ${index}"),
new FlatButton(
onPressed:() =>
Navigator.of(context).pushNamed("/${index + 1}"),
child: new Text("Push"))
],
),
),
);
}
void main() {
runApp(
new MyApp(),
);
}
太棒了!謝謝! – user3217522
如果你已經有了一個'MaterialApp',但是你需要一個你想擁有自己的導航棧的特定頁面,這會怎麼做呢?將'MaterialApp'嵌套在對方中會產生一些時髦的結果... –
您可以爲每個頁面創建新的導航器。作爲參考來看[試驗例](https://github.com/flutter/flutter/blob/c46347b6df27abdb91b8b7c63e90255fefc86a6e/packages/flutter/test/widgets/routes_test.dart#L193)或[標籤視圖插件](https://開頭github.com/flutter/flutter/blob/9909e773dc66608a866efa7388f39127509d0e1e/packages/flutter/lib/src/cupertino/tab_view.dart#L10) – Mogol