如何在flutter中的命名路由中传递多个参数 [英] How to pass multiple arguments in named route in flutter

查看:211
本文介绍了如何在flutter中的命名路由中传递多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个参数传递给命名路由.我已经尝试了多种方法,但到目前为止还没有成功.谁能告诉我如何实现这一目标?

I am trying to pass multiple arguments to a named route. I have tried multiple things but have not succeeded so far. Can anyone tell me how to achieve this?

routes.dart

routes.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:brandcare/views/pdf_view.dart';
import 'package:brandcare/views/reports_view.dart';

class RouteGenerator {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    final args = settings.arguments;
    switch (settings.name) {
      case 'reports':
        return CupertinoPageRoute(builder: (BuildContext context) => ReportsView());
      case 'pdf':
        return CupertinoPageRoute(builder: (BuildContext context) => PDFView());
      default:
        return CupertinoPageRoute(builder: (BuildContext context) => DashboardView());
    }
  }
}

reports_view.dart

onTap: () {
  Navigator.pushNamed(
    context, 'pdf',
    arguments: PDFView(
      reportTitle: 'January Report',
      reportFullPath: 'https://static.example.com/reports/123456.pdf'
    )
  );
},

我要访问多个参数的页面

The page where I want to access multiple arguments

pdf_view.dart

pdf_view.dart

class PDFView extends StatefulWidget {
  final String reportTitle;
  final String reportFullPath;

  PDFView({
    Key key,
    this.reportTitle,
    this.reportFullPath,
  }) : super(key: key);

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

class _PDFViewState extends State<PDFView> {
  String title = this.widget.reporTitle;
  String url = this.widget.reportFullPath;
}

推荐答案

您传递了错误的参数.您需要传递想要的特定参数对象.对于您的情况,您需要创建一个这样的文件:

You are passing the wrong argument. You need to pass a specific argument object which you want. For your case you need to create one like this:

class ScreenArguments {
  final String reportTitle;
  final String reportFullPath;

  ScreenArguments(this.reportTitle, this.reportFullPath);
}

现在在推入这样的路线时将此对象作为参数传递:

Now pass this object as argument while pushing a route like this:

Navigator.pushNamed(
      context,
      'pdf',
      arguments: ScreenArguments(
        'January Report',
         'https://static.example.com/reports/123456.pdf',
      ),
    );

现在,您需要在路由生成器中访问传递给该路由的参数.就是这样:

Now you need to access the arguments passed to the route in route generator. Just like this:

class RouteGenerator {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    final args = settings.arguments;
    switch (settings.name) {
      case 'reports':
        return CupertinoPageRoute(builder: (BuildContext context) => ReportsView());
      case 'pdf':
        return CupertinoPageRoute(builder: (BuildContext context) {
          ScreenArguments argument = args;
          return PDFView(
            reportTitle: argument.reportTitle,
            reportFullPath: argument.reportFullPath,
          );
        });
      default:
        return CupertinoPageRoute(builder: (BuildContext context) => DashboardView());
    }
  }
}

这可以解决您的问题

这篇关于如何在flutter中的命名路由中传递多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆