路由
Flutter 中的路由就是页面跳转。通过 Navigator 组件管理路由导航。
并提供了管理堆栈的方法。
如:Navigator.push 和 Navigator.pop
- 普通push路由例子
import 'package:flutter/material.dart';
import '../Search.dart';
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("跳转到搜索页面"),
onPressed: () {
//路由跳转
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => SearchPage()));
},
color: Theme.of(context).accentColor,
textTheme: ButtonTextTheme.primary),
SizedBox(height: 20),
],
);
}
}
import 'package:flutter/material.dart';
class SearchPage extends StatelessWidget {
const SearchPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("搜索页面"),
),
body: Text("搜索页面内容区域"),
);
}
}
- pop例子
import 'package:flutter/material.dart';
class FormPage extends StatelessWidget {
String title;
FormPage({this.title = "表单"});
@override
Widget build(BuildContext context) {
return Scaffold(
//浮动按钮
floatingActionButton: FloatingActionButton(
child: Text('返回'),
onPressed: () {
//返回上一页
Navigator.of(context).pop();
},
),
appBar: AppBar(
title: Text(this.title),
),
body: ListView(
children: <Widget>[
ListTile(
title: Text('我是表单页面'),
),
ListTile(
title: Text('我是表单页面'),
),
ListTile(
title: Text('我是表单页面'),
),
ListTile(
title: Text('我是表单页面'),
)
],
),
);
}
}
命名路由
需要定义在根组件中 MaterialApp
import 'package:flutter/material.dart';
import 'routes/Routes.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// home:Tabs(),
initialRoute: '/', //初始化的时候加载的路由
onGenerateRoute: onGenerateRoute);
}
}
import 'package:flutter/material.dart';
import '../pages/Tabs.dart';
import '../pages/Form.dart';
import '../pages/Search.dart';
import '../pages/Product.dart';
import '../pages/ProductInfo.dart';
//配置路由
final routes = {
//不需要传参
'/': (context) => Tabs(),
'/form': (context) => FormPage(),
'/product': (context) => ProductPage(),
//接受传参
'/productinfo': (context, {arguments}) =>
ProductInfoPage(arguments: arguments),
'/search': (context, {arguments}) => SearchPage(arguments: arguments),
};
//固定写法
var onGenerateRoute = (RouteSettings settings) {
// 统一处理
final String name = settings.name;
final Function pageContentBuilder = routes[name];
if (pageContentBuilder != null) {
if (settings.arguments != null) {
final Route route = MaterialPageRoute(
builder: (context) =>
pageContentBuilder(context, arguments: settings.arguments));
return route;
} else {
final Route route =
MaterialPageRoute(builder: (context) => pageContentBuilder(context));
return route;
}
}
};
- 不需要传参的命名路由
Navigator.pushNamed(context, '/form');
- 需要传参的命名路由跳转
Navigator.pushNamed(context, '/search',arguments: {
"id":123
});
网友评论