flutter - 颤振如何打开带有参数的页面
问题描述
我正在使用此代码打开带有参数的页面:
_modalBottomSheetMenu() {
wsModalBottom(
context,
title: trans(context, "Categories"),
bodyWidget: ListView.separated(
itemCount: _categories.length,
separatorBuilder: (cxt, i) => Divider(),
itemBuilder: (BuildContext context, int index) => ListTile(
title: Text(parseHtmlString(_categories[index].name)),
onTap: () {
Navigator.pop(context);
Navigator.pushNamed(context, "/browse-category",
arguments: _categories[index])
.then((value) => setState(() {}));
},
),
),
);
}
但现在我只想直接在页面上设置参数。我已经尝试过了,但它不起作用:
BrowseCategoryPage(key: 1),
我在浏览类别页面中得到了这段代码:
BrowseCategoryPage({Key key}) : super(key: key);
我能做些什么来解决这个问题?
解决方案
我认为一个好方法是轻松使用get包。
第一种方式 -
Get.to(Second(), arguments: ["First", "Second"]);
然后你可以像这样在 Second() 屏幕中接收数据,
var data = Get.arguments;
注意:data
这里将是一个数组,因为我们在参数中传递了一个数组。因此,要获取第一个参数,您可以使用data[0]
,要获取第二个参数,您可以使用data[1]
.
第二种方式 - 只需为参数创建另一个类模型,就像这样,
class SecondScreenModel{
final String first,second;
SecondScreenModel({this.first,this.second});
}
然后做这样的事情,
Get.to(Second(),arguments: SecondScreenModel(first: "aaa",second:"bbb"));
在第二屏接收数据,
SecondScreenModel model = Get.arguments;
推荐阅读
- python - TypeError:“张量”对象不能解释为整数,张量流扫描
- c - 打印字母表中未使用的字符
- react-native - 在不同堆栈之间反应原生导航
- azure-sdk-python - Azure Forms Recognizer - 保存输出结果 SDK Python
- ajax - 如果购物车总数小于 woocommerce 购物车页面中的特定数量,如何动态隐藏按钮
- python - Folium - 提取 mxm 米的 nxn 图像
- sql - 每组所需的最大行数
- mysql - 如何修复正在搜索行并缓慢更新的数据库
- regex - 在我的正则表达式中无法匹配带有以下 \ 和所有符号的单词
- python - genfromtxt 在 Jupyter 中打开表的问题