GitHub:https://github.com/happy-python/flutter_demos/tree/master/navigation_demo


lib/main.dart
import 'package:flutter/material.dart';
import 'package:navigation_demo/pages/homepage.dart';
import 'package:navigation_demo/pages/adddatapage.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Flutter Tutorial",
theme: ThemeData(
primarySwatch: Colors.blue,
),
routes: {
"/": (BuildContext context) => HomePage(),
"/add": (BuildContext context) => AddDataPage(),
},
initialRoute: "/",
);
}
}
注意这里的 routes
和 initialRoute
属性。
在 lib/pages/homepage.dart
向 lib/pages/adddatapage.dart
导航的时候,使用 Navigator.pushNamed
替代了 Navigator.push
。
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => AddDataPage(),
)).then((value) {
print("接收返回的数据: $value");
});
Navigator.pushNamed(context, "/add").then((value) {
print("接收返回的数据: $value");
});
在 lib/pages/adddatapage.dart
向 lib/pages/homepage.dart
返回的时候,使用 Navigator.pop
可以向上一个页面传值。
// 返回数据
Navigator.pop(context, "数据");
网友评论