美文网首页flutter
flutter页面跳转 Route 使用汇总

flutter页面跳转 Route 使用汇总

作者: 橘子有毒 | 来源:发表于2020-06-20 12:25 被阅读0次

一、push方式直接跳转

普通跳转

Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => Page1()));

带参数跳转和接收参数

      Navigator.push(context,
          MaterialPageRoute(
              builder: (BuildContext context) => Page2(),
              settings: RouteSettings(name: "路由名",arguments: "这是页面2传递过来的参数")
          ));
final String message = ModalRoute.of(context).settings.arguments as String;

构造函数直接传参

Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => Page3("这是传递过来的参数")));

//目标页面接收数据同上
final String message = ModalRoute.of(context).settings.arguments as String;

跳转后携带参数返回

      Future result = Navigator.of(context).push(MaterialPageRoute(
          builder: (ctx) {
            return Page3("传进来的参数,点击返回,携带参数返回");
          }
      ));

      //接收返回的参数
      result.then((res) {
        setState(() {
          _handleMessage = res;
        });
      });
//返回页面传递的参数
Navigator.of(context).pop("这是返回携带的参数");







二、命名路由跳转

先在入口页面配置路由

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      routes: WGRouter.routes,
      initialRoute: WGRouter.main,//初始页面路由可代替home
      onGenerateRoute: WGRouter.generateRoute,
      onUnknownRoute: WGRouter.unknownRoute,
//      home: MainPage(),
    );
  }
}
class WGRouter {

  static final main = 'main';
  static final pathPage1 = 'page_1';
  static final pathPage2 = 'page_2';
  static final pathPage3 = 'page_3';

  static final Map<String, WidgetBuilder> routes = {
    main: (ctx) => MainPage(),
    pathPage1: (ctx){
      return Page1();
    },
    pathPage2: (ctx) => Page2(),
  };


  static final RouteFactory generateRoute = (settings) {
    if (settings.name == pathPage3) {
      return MaterialPageRoute(
          builder: (ctx) {
            return Page3(settings.arguments);
          }
      );
    }
    return null;
  };

  static final RouteFactory unknownRoute = (settings) {
    return MaterialPageRoute(
        builder: (ctx) {
          return WGUnknownPage();
        }
    );
  };
}

命名路由跳转

Navigator.of(context).pushNamed(WGRouter.pathPage1);

命名路由携带参数跳转

Navigator.of(context).pushNamed(WGRouter.pathPage2,arguments:"命名路由携带的参数");

三、完整代码和展示图

image
class MainPage extends StatefulWidget {

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {

  final _titles = ['普通跳转','带参数普通跳转','构造函数直接传参数','携带参数返回','命名路由',"命名路由带参数",'generate路由','未定义的页面'];

  var _handleMessage = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('首页'),),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Text(_handleMessage),
            buildJumpButton(context,_titles[0]),
            buildJumpButton(context,_titles[1]),
            buildJumpButton(context,_titles[2]),
            buildJumpButton(context,_titles[3]),
            buildJumpButton(context,_titles[4]),
            buildJumpButton(context,_titles[5]),
            buildJumpButton(context,_titles[6]),
            buildJumpButton(context,_titles[7]),
          ],
        ),
      ),
    );
  }



  Container buildJumpButton(BuildContext context,String title) {
    return Container(
      width: 300,
      padding: EdgeInsets.only(top: 30),
      child: CupertinoButton(
        child: Text(title),
        color: Color.fromARGB(255, Random.secure().nextInt(255), Random.secure().nextInt(255), Random.secure().nextInt(255)),
        onPressed:()=>_jumpToPage(context,title),
      ),
    );
  }




  void _jumpToPage(BuildContext context,String title) {


    if(title == _titles[0]){
      Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => Page1()));
//      Navigator.push(context, CupertinoPageRoute(builder: (BuildContext context) => Page1()));
//      Navigator.push(context, PageRouteBuilder(builder: (BuildContext context) => Page1()));
      return;
    }

    if(title == _titles[1]){
      Navigator.push(context,
          MaterialPageRoute(
              builder: (BuildContext context) => Page2(),
              settings: RouteSettings(name: "路由名",arguments: "这是页面2传递过来的参数")
          ));
      return;
    }

    if(title == _titles[2]){
      Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => Page3("这是传递过来的参数")));
      return;
    }

    if(title == _titles[3]){
      Future result = Navigator.of(context).push(MaterialPageRoute(
          builder: (ctx) {
            return Page3("传进来的参数,点击返回,携带参数返回");
          }
      ));

      result.then((res) {
        setState(() {
          _handleMessage = res;
        });
      });
      return;
    }

    if(title == _titles[4]){
      Navigator.of(context).pushNamed(WGRouter.pathPage1);
      return;
    }

    if(title == _titles[5]){
      Navigator.of(context).pushNamed(WGRouter.pathPage2,arguments:"命名路由携带的参数");
      return;
    }

    if(title == _titles[6]){
      Navigator.of(context).pushNamed(WGRouter.pathPage3,arguments:"generate路由跳转");
      return;
    }

    if(title == _titles[7]){
      Navigator.of(context).pushNamed("天知道这是啥路径");
      return;
    }

  }
}

相关文章

  • Flutter - 页面跳转(路由)、传值

    Flutter 中页面跳转通过 Navigator 和 Route 来实现。 一、页面跳转 1,常规路由页面跳转...

  • flutter页面跳转 Route 使用汇总

    一、push方式直接跳转 普通跳转 带参数跳转和接收参数 构造函数直接传参 跳转后携带参数返回 二、命名路由跳转 ...

  • Flutter 之 导航

    在 Flutter 中,页面之间的跳转是通过 Route 和 Navigator 来管理的: Route 是页面的...

  • Flutter知识点: Route & Navigato

    App通常由多个页面组成,并且页面之间存在着跳转的关系。 Flutter中通过定义Route, 使用Navigat...

  • flutter路由管理方法Fluro使用(一)

    引言 对于flutter路由使用,前期我们需要能达到两个功能点: 使用route进行页面跳转,传递和接收参数; 使...

  • Flutter细节记录

    一、路由 flutter进行页面跳转是由路由来实现的(Route),Navigator负责Route的压入和弹出。...

  • Flutter--Route和Navigator(一)

    一、介绍 Flutter中的路由(Route)通俗的桨就是页面跳转,类似android的activity跳转,在F...

  • Flutter 中的Intents

    Flutter 中的 Intents 可以看看这篇 Flutter 页面跳转,携带参数的页面跳转的使用和说明点击跳...

  • Flutter—— 路由(Route)和导航(Navigator

    Flutter的页面,怎么进行跳转的呢?通过路由和导航呢。 一、路由和导航,初认识 言简意赅! 路由(Route)...

  • Flutter-路由

    一、概念 在flutter中路由也是用来处理页面跳转、页面数据传递等操作,主要是通过路由(Route)和导航器(N...

网友评论

    本文标题:flutter页面跳转 Route 使用汇总

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