发表日期:2018-12 文章编辑:小灯 浏览次数:2546
class FirstScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("first Screen"), ), body: Center( child: RaisedButton( child: Text("this is first screen"), onPressed: () { //go to second screen Navigator.push(context, MaterialPageRoute(builder: (context)=>SecodeScreen())); }, )), ); } }
Screenshot_1545962905.png 如上图所示,第一个界面中包含了一个按钮,在按钮的点击事件中
Navigator.push(context, MaterialPageRoute(builder: (context)=>SecondScreen()));
这句代码的意思将页面SecondScreen压入路由栈,在Android开发中我们也是同样的使用一个回退栈管理我们的界面,既然有入栈操作,那么一定有出栈了,没错,下面看第二个界面的代码:
class SecondScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("second screen"), ), body: Center( child: RaisedButton( child: Text("go back"), onPressed: () { Navigator.pop(context); }, ), ), ); } }
我们使用了
Navigator.pop(context);
将该本页面弹出路由栈,从而返回到第一个页面。
以上就是例子可以在官方文档中找到,这只是最简单的路由跳转,但是平时我们开发经常需要在页面之间传值,所以下面我们来看看,flutter中路由如何传递参数。
class Todo{ final String title; final String desc; Todo(this.title,this.desc); }
然后我们创建一个todoList
class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( // This is the theme of your application. // // Try running your application with "flutter run". You'll see the // application has a blue toolbar. Then, without quitting the app, try // changing the primarySwatch below to Colors.green and then invoke // "hot reload" (press "r" in the console where you ran "flutter run", // or simply save your changes to "hot reload" in a Flutter IDE). // Notice that the counter didn't reset back to zero; the application // is not restarted. primarySwatch: Colors.blue, ), home: TodoScreen( todos: List.generate(10, (i) => Todo("title$i", "Desc$i"))), ); } } //列表页 class TodoScreen extends StatelessWidget { final List<Todo> todos; TodoScreen({Key key, @required this.todos}) : super(key: key);@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("todos"), ), body: ListView.builder( itemCount: todos.length, itemBuilder: (context, index) { return ListTile( title: Text(todos[index].title), onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => DetailScreen(todo: todos[index]))); }, ); }, ), ); } }//详情页 class DetailScreen extends StatelessWidget { final Todo todo; DetailScreen({Key key, @required this.todo}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(todo.title), ), body: Center( child: Text(todo.desc), ), ); } }
在代码里我们通过
Navigator.push( context, MaterialPageRoute( builder: (context) => DetailScreen(todo: todos[index]))); }, );
将todo传到了详情页,也就是通过构造函数传值,在flutter中一切都是widget,我们在DetailScreen里通过构造函数接收即可。
class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( // This is the theme of your application. // // Try running your application with "flutter run". You'll see the // application has a blue toolbar. Then, without quitting the app, try // changing the primarySwatch below to Colors.green and then invoke // "hot reload" (press "r" in the console where you ran "flutter run", // or simply save your changes to "hot reload" in a Flutter IDE). // Notice that the counter didn't reset back to zero; the application // is not restarted. primarySwatch: Colors.blue, ), home: HomeScreen(), ); } }class SelectionButton extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build return RaisedButton( child: Text('pick an option,any option'), onPressed: () { _navigateAndDisplaySelection(context); }, ); } } _navigateAndDisplaySelection(BuildContext context) async{ final result= await Navigator.push(context,MaterialPageRoute(builder: (context)=>SelectionScreen()) ); Scaffold.of(context) ..removeCurrentSnackBar() ..showSnackBar(SnackBar(content:Text("$result"))); } class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text('home screen'), ), body: Center(child: SelectionButton()), ); } } class SelectionScreen extends StatelessWidget{ @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text("pick on option"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Padding( padding: const EdgeInsets.all(8.0), child: RaisedButton( onPressed: (){ Navigator.pop(context,"yep"); }, child: Text("yep"), ), ), Padding( padding: const EdgeInsets.all(8.0), child: RaisedButton( onPressed: (){ Navigator.pop(context,"nope"); }, child: Text("nope"), ), ) ], ), ), ); }}
Pop方法的第二个参数是一个
T result
这样我们在出栈的时候可以将参数带回到上一个页面,在上一个页面中,我们这样来接收
_navigateAndDisplaySelection(BuildContext context) async{ final result= await Navigator.push(context,MaterialPageRoute(builder: (context)=>SelectionScreen()) ); Scaffold.of(context) ..removeCurrentSnackBar() ..showSnackBar(SnackBar(content:Text("$result"))); }
class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData(primarySwatch: Colors.blue, ), home: FirstScreen(), routes: { '/second':(context)=>SecondScreen() }, ); } }
routes
是一个map,用来管理我们定义的命名路由,之后我们就可以使用
Navigator.pushNamed(context, "/second");
来进行路由的跳转,但是命名路由的方式有一个缺点,就是不能直接携带参数,只能静态的在注册路由的时候这么写:
routes: { '/second':(context)=>SecondScreen('params') },
这样在传递一些动态的改变的参数时候就显得不方便。
class HeroApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Transition Demo', home: MainScreen(), ); } }class MainScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Main Screen'), ), body: GestureDetector( child: Hero( tag: 'imageHero', child: Image.network( 'https://raw.githubusercontent.com/flutter/website/master/src/_includes/code/layout/lakes/images/lake.jpg', ), ), onTap: () { Navigator.push(context, MaterialPageRoute(builder: (_) { return DetailScreen(); })); }, ), ); } }class DetailScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( child: Center( child: Hero( tag: 'imageHero', child: Image.network( 'https://raw.githubusercontent.com/flutter/website/master/src/_includes/code/layout/lakes/images/lake.jpg', ), ), ), onTap: () { Navigator.pop(context); }, ), ); } }
在不同页面通过给Hero设置相同的tag,使它们关联起来。
Flutter中路由的常用使用方式基本介绍完了,主要参考官方例子,如有不足,欢迎指正。
Flutte Doc
日期:2018-10 浏览次数:7247
日期:2018-12 浏览次数:4320
日期:2018-07 浏览次数:4869
日期:2018-12 浏览次数:4168
日期:2018-09 浏览次数:5491
日期:2018-12 浏览次数:9916
日期:2018-11 浏览次数:4798
日期:2018-07 浏览次数:4574
日期:2018-05 浏览次数:4852
日期:2018-12 浏览次数:4316
日期:2018-10 浏览次数:5133
日期:2018-12 浏览次数:6207
日期:2018-11 浏览次数:4454
日期:2018-08 浏览次数:4587
日期:2018-11 浏览次数:12624
日期:2018-09 浏览次数:5571
日期:2018-12 浏览次数:4825
日期:2018-10 浏览次数:4179
日期:2018-11 浏览次数:4523
日期:2018-12 浏览次数:6058
日期:2018-06 浏览次数:4003
日期:2018-08 浏览次数:5428
日期:2018-10 浏览次数:4453
日期:2018-12 浏览次数:4517
日期:2018-07 浏览次数:4356
日期:2018-12 浏览次数:4495
日期:2018-06 浏览次数:4376
日期:2018-11 浏览次数:4370
日期:2018-12 浏览次数:4243
日期:2018-12 浏览次数:5276
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.