美文网首页
flutter路由跳转

flutter路由跳转

作者: MasterPaul | 来源:发表于2019-05-10 11:47 被阅读0次

    跳转并移除当前所有页面

    
    Navigator.pushAndRemoveUntil(context,new MaterialPageRoute(builder: (BuildContext context) {
    
    return new GameScene(itemID: item['ID'],itemStyle: style,);
    
    }),
    
    (route) => route ==null);
    

    跳转并且替换当前页面

     Navigator.pushReplacement(context,new MaterialPageRoute(builder: (BuildContext context){
    
    return new GameScene(itemID: item['ID'],itemStyle: style,);
    
    }));
    

    封装跳转工具类,加入转场动画

    import 'package:flutter/material.dart';
    
    import 'package:flutter/animation.dart';
    
    
    
    class NavigationUtil{
    
    static push( BuildContext context,Widget page,{Function callback}){
    
    pushFromRight(context, page,callback);
    
    }
    
    static pushNoAnimate(BuildContext context,Widget page,{Function callback}){
    
    }
    
    static pushFromRight(BuildContext context,Widget page,Function callback){
    
    //自定义跳转动画
    
        PageRouteBuilder pageBuilder =new PageRouteBuilder(
    
    opaque:false,
    
    //        transitionDuration: const Duration(microseconds: 2000),
    
            pageBuilder:(BuildContext context, _, __) {
    
    return page;
    
    } ,
    
    transitionsBuilder: (
    
    _, Animation animation, __, Widget child,
    
    ) {
    
    return new SlideTransition(
    
    position:new Tween(begin:const Offset(1.0,0.0),
    
    end: Offset.zero,
    
    ).animate(animation), child: child,
    
    );
    
    });
    
    Navigator.of(context).push(pageBuilder).then((data){
    
    callback !=null && callback(data);
    
    });
    
    }
    
    static pushFromBottom(BuildContext context,Widget page,{Function callback}){
    
    //自定义跳转动画
    
        PageRouteBuilder pageBuilder =new PageRouteBuilder(
    
    opaque:false,
    
    pageBuilder:(BuildContext context, _, __) {
    
    return page;
    
    } ,
    
    transitionsBuilder: (
    
    _, Animation animation, __, Widget child,
    
    ) {
    
    return  new SlideTransition(
    
    position:new Tween(begin:const Offset(0.0,1.0),
    
    end: Offset.zero,
    
    ).animate(animation), child: child,
    
    );
    
    });
    
    Navigator.of(context).push(pageBuilder).then((data){
    
    if(callback !=null){
    
    callback(data);
    
    }
    
    });;
    
    }
    
    static reset(BuildContext context,Widget page){
    
    //销毁所有页面,并跳转到page页
    
        Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context){
    
    return page;
    
    }), (Route route)=>false);
    
    }
    
    static popToPage(BuildContext context,Widget page){
    
    //销毁所有除了page的所有页面
    
        Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context){
    
    return page;
    
    }), (Route route)=>false);
    
    }
    
    }
    
    

    相关文章

      网友评论

          本文标题:flutter路由跳转

          本文链接:https://www.haomeiwen.com/subject/qjnooqtx.html