简体   繁体   English

Flutter:如何将 PageRoute Animation 与 Navigator.of(context).pushNamed 结合起来

[英]Flutter: How to combine PageRoute Animation with Navigator.of(context).pushNamed

I want to use animated page route on my flutter project.我想在我的颤振项目中使用动画页面路由。 Now all of my routes are Named Route and I don't wanna change them.现在我所有的路线都是命名路线,我不想改变它们。 Is there any way I can use Page Route Animation with named route?有什么方法可以将页面路由动画与命名路由一起使用吗? Like: If I am going from PageOne() to PageTwo() using Navigator.of(context).pushNamed(PageTwo.routename) , I don't wanna see default transition, May be I want to use scale animation or fade animation.喜欢:如果我使用Navigator.of(context).pushNamed(PageTwo.routename)PageOne()PageTwo() ,我不想看到默认过渡,可能是我想使用缩放动画或淡入淡出动画。 Is there any way to do that?有没有办法做到这一点?

onTap: () {
  Navigator.of(context).pushNamed(
     ProductsSearch.routeName,
     arguments: ScreenArguments(null, null, null, null, null, null, true, false),
  );
},

As your Question I have solved this demo answer try once it will work .作为你的问题,我已经解决了这个演示答案,尝试一下它会起作用。

 import 'package:flutter/material.dart';

 void main() {
 runApp(
 MaterialApp(
  home: HomePage(),
   onGenerateRoute: (RouteSettings settings) {
    final SecondHome args = settings.arguments;
    switch (settings.name) {
      case '/':
        return SlideRightRoute(widget:HomePage());
        break;
      case '/second':
      return 
    SlideRightRoute(widget:SecondHome(args.data1,args.data2,args.boolvalue));
       break;
      }
    },
   ),
 );
}



class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Home'),
  ),
  body: new Center(
    child: RaisedButton(
      onPressed: () {
       Navigator.of(context).pushNamed(
          '/second',
          arguments:SecondHome("data1","data2",true),
        );
      },
      child: Text('Second Home'),
     ),
    ),
  );
 }
}



class SecondHome extends StatelessWidget {
String data1;
String data2;
bool boolvalue;
SecondHome(this.data1,this.data2,this.boolvalue);
@override
Widget build(BuildContext context) {
 print("Secoendhomedata${data1}");
  return Scaffold(
  appBar: AppBar(
    title: Text('Second Home'),
  ),
  body: new Center(
    child: RaisedButton(
      onPressed: () {
        Navigator.pop(context);
      },
      child: Text('Go Back'),
      ),
     ),
    );
   }
 }

class SlideRightRoute extends PageRouteBuilder {
final Widget widget;
SlideRightRoute({this.widget})
  : super(
pageBuilder: (BuildContext context, Animation<double> animation,
    Animation<double> secondaryAnimation) {
  return widget;
},
transitionsBuilder: (BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child) {
  return new SlideTransition(
    position: new Tween<Offset>(
      begin: const Offset(1.0, 0.0),
      end: Offset.zero,
    ).animate(animation),
    child: child,
    );
   },
 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM