gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:30:46 26 4
gpt4 key购买 nike

我正在尝试将多个参数传递给命名路由。我尝试了多种方法,但到目前为止还没有成功。谁能告诉我如何实现这一目标?
路线.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;
}

最佳答案

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

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());
}
}
}
这将解决您的问题

关于flutter - 如何在 flutter 的命名路由中传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62771519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com