命名路由跳转封装
import 'package:flutter/material.dart';
final routes = {
//不带参数路由
'/search':(context)=>Search(),
//带参数路由
'/search2':(context,{arguments})=>Search(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;
}
}
};
配置使用:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// home: Tabs(),
initialRoute: '/search',
onGenerateRoute: onGenerateRoute,
);
}
}
页面跳转:
Navigator.pushNamed(context, '/search');
Navigator.pushNamed(context, '/search2',arguments: {
"title":"传参数"
});
获取参数使用
class Search extends StatelessWidget {
final arguments;//用于接受参数
const Search({Key? key, this.arguments}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("${this.arguments != null ? this.arguments.title:'无传参'}"),
),
);
}
}
Comments | NOTHING