首页 > 解决方案 > 颤振如何打开带有参数的页面

问题描述

我正在使用此代码打开带有参数的页面:

_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);

我能做些什么来解决这个问题?

标签: flutterdart

解决方案


我认为一个好方法是轻松使用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;

推荐阅读