flutter - flutter, PageRouteBuilder, 添加水平过渡
问题描述
iOS 的本机导航转换是 RightToLeft。ANDROID 的本机导航转换是从下到上。我想覆盖 Flutter 中的本机导航转换,以便在 iOS 和 ANDROID 中具有相同的从右到左转换。为此,我正在尝试使用 PageRouteBuilder 但没有运气让它工作。第一个代码块,我有一个非常基本的屏幕,可以工作......但本机。第二个片段包括我试图集成的导航转换代码。
我正在尝试修复的代码。
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
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> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//==============================================
// How would I force a horizontal transition?
mbNav001(context),
new Text(
'Screen 1',
),
],
),
),
);
}
}
//===================================================
Padding mbNav001(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(28.0),
child: new MaterialButton(
height: 80.0,
padding: EdgeInsets.all(50.0),
minWidth: double.infinity,
color: Theme.of(context).primaryColor,
textColor: Colors.white,
child: new Text(
"material button a",
style: new TextStyle(
fontSize: 20.0,
color: Colors.yellow,
),
),
splashColor: Colors.redAccent,
// ----- This line is giving me error...
onPressed: () {
print('click mb');
//===========================================================
// How to force a horizontal trans in Navigation?
//===========================================================
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
}
// expecting to find... :
),
);
}
//===================================================
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: Center(
child: RaisedButton(
onPressed: () {
// Navigate back to first screen when tapped!
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
}
这是我要添加的转换代码。
transitionsBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child) {
return SlideTransition(
position: new Tween<Offset>(
begin: const Offset(1.0, 0.0),
end: Offset.zero,
).animate(animation),
child: new SlideTransition(
position: new Tween<Offset>(
begin: Offset.zero,
end: const Offset(1.0, 0.0),
).animate(secondaryAnimation),
child: child,
),
);
},
);
Navigator.of(context).push(pageRoute);
解决方案
在我的案例中,使用 CupertinoPageRoute 而不是 MaterialPageRoute 解决了同样的问题,而无需尝试应用任何自定义动画。
Navigator.of(context).push(CupertinoPageRoute(builder: (context) => MyPanel()));
推荐阅读
- regex - Perl 正则表达式的含义?它是否捕捉到正确的东西,XML 到 TXT
- java - 如何修复 ScriptRunner 中的错误请求 - jira
- openssl - 如何从源代码安装中使用 openssl 1.0.2k 安装 Erlang 18
- facebook-graph-api - 如何通过 message_tags 过滤页面的帖子(API Graph Facebook v3.3)
- angular - 动态路由在 URL Angular 6 中显示搜索到的文本
- jquery - 如何让 jQuery 以数字方式检查数值?
- javascript - Apache Cordova Native Storage 从回调返回到外部函数
- vba - 执行代码时如何防止弹出框出现?
- cytoscape.js - 隐藏所有连接边时隐藏节点
- java - 使用 JOOQ 将小数秒保存到数据库中的 TIME 数据类型