文件结构:
内容承接 https://blog.csdn.net/u013227399/article/details/103858613
main.dart
import 'package:flutter/material.dart';
import 'routes/Routes.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// home: Tabs(),
// routes: {
// '/':(context)=>Tabs(),
// '/search':(context)=>SearchPage(),
// '/form':(context,{arguments})=>FormPage(),
// },
initialRoute: '/', //初始化路由
onGenerateRoute: onGenerateRoute,
);
}
}
routes.dart
import 'package:flutter/material.dart';
import '../pages/tabs.dart';
import '../pages/From.dart';
import '../pages/Search.dart';
final routes = {
'/': (context) => Tabs(),
'/search': (context) => SearchPage(),
'/form': (context, {arguments}) => FormPage(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;
}
}
};
home.dart
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
_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.pushNamed(context, '/search');
},
color: Theme.of(context).accentColor,
textTheme: ButtonTextTheme.primary,
),
SizedBox(height: 20),
RaisedButton(
child: Text('跳转到表单页面并传值'),
onPressed: () {
Navigator.pushNamed(context, '/form',arguments: {
'title':'传标题'
});
},
color: Theme.of(context).accentColor,
textTheme: ButtonTextTheme.primary,
),
],
);
}
}
from.dart
import 'package:flutter/material.dart';
class FormPage extends StatelessWidget {
final Map arguments;
FormPage({this.arguments});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("${arguments != null ? arguments['title'] : '默认标题'}"),
),
body: ListView(
children: <Widget>[
ListTile(
title: Text('表单'),
),
RaisedButton(
child: Text('返回'),
onPressed: (){
Navigator.of(context).pop();
},
),
],
),
);
}
}
效果展示: