我可以使用MaterialApp
的主题来更改过渡动画。下面的示例将FadeTransition
替换为默认过渡。使用淡入淡出过渡时,感觉很慢,我无法弄清楚如何更改主题中设置的过渡的持续时间。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
final themeData = ThemeData(
pageTransitionsTheme: PageTransitionsTheme(builders: {
TargetPlatform.iOS: FadeTransitionBuilder(),
TargetPlatform.android: FadeTransitionBuilder(),
}),
);
class FadeTransitionBuilder extends PageTransitionsBuilder {
@override
Widget buildTransitions<T>(_, __, animation, ___, child) => FadeTransition(opacity: animation, child: child);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Mobile',
theme: themeData,
initialRoute: '/',
routes: routes,
);
}
}
答案 0 :(得分:2)
我可以使用命名路由来实现此目的,您需要在提供给MaterialApp构建器的Navigator实例中列出这些路由。 必须删除MaterialApp的initialRoute属性。
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/about':
return CustomPageRoute(
builder: (context) => About());
default:
return CustomPageRoute(
builder: (context) => Home());
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Mobile',
theme: themeData,
builder: (context, widget) {
return Navigator(
onGenerateRoute: generateRoute,
);
},
);
}
}
class CustomPageRoute extends MaterialPageRoute {
@override
Duration get transitionDuration => const Duration(milliseconds: 1000);
CustomPageRoute({builder}) : super(builder: builder);
}
答案 1 :(得分:0)
创建这个类:
class MyRoute extends MaterialPageRoute {
MyRoute({WidgetBuilder builder}) : super(builder: builder);
@override
Duration get transitionDuration => Duration(seconds: 3);
}
现在,而不是使用常规
Navigator.push(context, MaterialPageRoute(builder: (_) => Page2()));
使用
Navigator.push(context, MyRoute(builder: (_) => Page2()));