如何在flutter中的命名路由中传递多个参数

问题描述 投票:0回答:4

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

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 事件
onTap: () {
  Navigator.pushNamed(
    context, 'pdf',
    arguments: PDFView(
      reportTitle: 'January Report',
      reportFullPath: 'https://static.example.com/reports/123456.pdf'
    )
  );
},

我想要访问多个参数的页面

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;
}
flutter routes
4个回答
38
投票

你传递了错误的论点。您需要传递您想要的特定参数对象。对于您的情况,您需要创建一个这样的:

class ScreenArguments {
  final String reportTitle;
  final String reportFullPath;

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

现在将此对象作为参数传递,同时推送如下所示的路线:

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

现在您需要访问传递给路由生成器中的路由的参数。就像这样:

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());
    }
  }
}

这可以解决你的问题


28
投票

我通过传递不同参数的列表来实现它,如下所示:

Navigator.of(context).pushNamed("/someRoute", arguments:[8, "a string"]);

然后在路线生成器中我像这样转发它们:

// ...    
case "/someRoute":
  List<dynamic> args = settings.arguments;;
  return MaterialPageRoute(builder: (_) => SomeScreen(someInt: args[0], someString: args[1])); 

7
投票

您可以在目标小部件屏幕中创建参数类,并将其对象作为来自源类的参数传递。

示例:

// above will be the code for your target screen/widget as usual below will be the respected argument class for the same target screen/widget

    class WeatherRouteArguments {
      WeatherRouteArguments({ required this.city, required this.country });
      final String city;
      final String country;
    
      bool get isGermanCapital {
        return country == 'Germany' && city == 'Berlin';
      }
    }

使用参数调用屏幕,您可以执行以下操作:

void _showWeather() {
  Navigator.pushNamed(
    context,
    '/weather',
    arguments: WeatherRouteArguments(city: 'Berlin', country: 'Germany'),
  );
}

我想你知道如何在目标小部件类的构建中获取参数,可以按如下方式完成:

 final args = ModalRoute.of(context)!.settings.arguments as WeatherRouteArguments;

参考:https://api.flutter.dev/flutter/widgets/Navigator/pushNamed.html


0
投票

我更喜欢这种方式

发送

Navigator.pushNamed(context, "/secondpage", arguments: {'index': 5, 'title': "App title"});

获取并转发

 case "/secondpage":
 final args = settings.arguments as Map<String, dynamic>;
 return MaterialPageRoute(builder: (_) => SecondPage(args));

收到

print("index: ${widget.args['index']}");
print("title: ${widget.args['title']}");
© www.soinside.com 2019 - 2024. All rights reserved.