Flutter进阶—路由和导航
大部分应用程序都有多个屏幕或页面,并希望用户能从当前屏幕平滑过渡到另一个屏幕,Flutter的路由和导航功能可以帮助我们管理应用程序中的用户界面之间的命名和过渡。 管理多个用户界面有两个核心概念和类:路由( 使用导航器(Navigator)移动应用通常通过称为“屏幕”或“页面”的全屏元素显示其内容,在Flutter中,这些元素称为路由,它们由导航器( Future push(
BuildContext context,Route route
)
// 将给定的路由添加到最靠近给定上下文的导航器的历史记录,并过渡到它
bool pop(
BuildContext context,[
result
])
// 从导航器中弹出最靠近给定上下文的路由
显示屏幕路由虽然您可以直接创建导航器,但最常见的是使用由 NavigatorState of(
BuildContext context
)
// 最接近该类的实例包含给定上下文的状态
一个
默认情况下,当模态路由替换为另一路由时,上一个路由将保留在内存中,要在没有必要时释放所有资源,可以将 现在我们新建一个项目myapp,然后用下面代码替换main.dart文件的代码: import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',theme: new ThemeData(
primarySwatch: Colors.blue,),home: new MyHomePage(title: '应用程序首页'),);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key,this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void _openNewPage() {
setState(() {
Navigator.of(context).push(new MaterialPageRoute<Null>(
builder: (BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('新的页面')
),body: new Center(
child: new Text(
'点击浮动按钮返回首页',floatingActionButton: new FloatingActionButton(
onPressed: () {
Navigator.of(context).pop();
},child: new Icon(Icons.replay),);
},));
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),body: new Center(
child: new Text(
'点击浮动按钮打开新页面',floatingActionButton: new FloatingActionButton(
onPressed: _openNewPage,child: new Icon(Icons.open_in_new),);
}
}
通常没有必要提供一个使用 使用命名导航器路由移动应用程序经常管理大量路由,通常可以通过名称来引用它们。路由名称按惯例使用类似路径的结构,例如“/a/b/c”,应用程序的主页路由默认为“/”。 可以使用 import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',routes: <String,WidgetBuilder> {
'/a': (BuildContext context) => new MyPage(title: 'A 页面'),'/b': (BuildContext context) => new MyPage(title: 'B 页面'),'/c': (BuildContext context) => new MyPage(title: 'C 页面')
},this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),body: new Row(
children: <Widget>[
new RaisedButton(
child: new Text('A按钮'),onPressed: () { Navigator.of(context).pushNamed('/a'); },new RaisedButton(
child: new Text('B按钮'),onPressed: () { Navigator.of(context).pushNamed('/b'); },new RaisedButton(
child: new Text('C按钮'),onPressed: () { Navigator.of(context).pushNamed('/c'); },)
]
)
);
}
}
class MyPage extends StatelessWidget {
MyPage({Key key,this.title}) : super(key: key);
final String title;
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(title)
),);
}
}
如果应用程序只有一个页面,那么可以使用 路由可以返回一个值当路由被推送,用于获取用户的输入时,可以通过 例如,如果我们要求用户按“确定”确认操作,我们可以等待 import 'package:flutter/material.dart';
import 'dart:async';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _result = false;
Future<Null> _openNewPage() async {
bool value = await Navigator.of(context).push(new MaterialPageRoute<bool>(
builder: (BuildContext context) {
return new Center(
child: new GestureDetector(
child: new Text("确定"),onTap: () { Navigator.of(context).pop(true); },);
}
));
setState(() {
_result = value;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),body: new Center(
child: new Text(
'用户当前选择为 $_result',floatingActionButton: new FloatingActionButton(
onPressed: _openNewPage,);
}
}
如果用户按“确定”,则值将为真。如果用户退出路由,例如通过按 弹出路由路由不一定需要掩盖整个屏幕, 框架有创建和显示弹出路由的功能,例如: 还有一些创建弹出路由的控件,如 import 'package:flutter/material.dart';
import 'dart:async';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _result = false;
Future<Null> _openNewPage() async {
bool value = await showDialog(
context: context,barrierDismissible: true,child: new Center(
child: new GestureDetector(
child: new Text("确定"),)
);
setState(() {
_result = value;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),);
}
}
定制路由您可以创建自己的控件库路由类,比如
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key,this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void _openNewPage() {
Navigator.of(context).push(new PageRouteBuilder(
opaque: false,pageBuilder: (BuildContext context,_,__) {
return new Center(child: new Text('定制页面路由'));
},transitionsBuilder: (_,Animation<double> animation,__,Widget child) {
return new FadeTransition(
opacity: animation,child: new RotationTransition(
turns: new Tween<double>(begin: 0.5,end: 1.0).animate(animation),child: child,);
}
));
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),body: new Center(
child: new Text(
'点击浮动按钮打开定制页面',);
}
}
页面路由分为两部分:“页面”和“过渡”。页面成为传递给 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |