繁体   English   中英

将列表移动到多个dart文件中使用的单独的dart文件中?

[英]moving a list into a separate dart file that is used in multiple dart files?

是否可以将dart文件中的列表移动到自己的dart文件中,然后导入列表?

我只得到了四个主要墨水池按钮才能显示。 除此之外,我无法通过ancenstortype错误。 我相信小部件没有正确传递。

https://pastebin.com/9zvRXpnu

 List<BuyItem> buyItemList = [
      BuyItem('Add a pack of 10 for \$2.99', 'assets/scantron.png'),
      BuyItem('Add a pack of 5 for \$1.99', 'assets/pens.png'),
      BuyItem('Add one for \$1.49', 'assets/notebook.png'),
      BuyItem('Add one for \$3.49', 'assets/calculator.png'),
      BuyItem('Add one for \$0.99', 'assets/cliff bar.png'),
      BuyItem('Add one for \$1.25', 'assets/apple.png'),
      .
      .
      .
      Product('Notebook', 'assets/notebook.png', () {
        Navigator.of(context).push(ItemsBuyPage([buyItemList[2]]));
      }),
      Product('Calculator', 'assets/calculator.png', () {
        Navigator.of(context).push(ItemsBuyPage([buyItemList[3]]));
      }),
    ];

    List<Product> foodList = [
      Product('Cliff Bar', 'assets/cliff bar.png', () {
        Navigator.of(context).push(ItemsBuyPage([buyItemList[4]]));
      }),
      Produ
      Product('Coffee', 'assets/coffee.png', () {
        Navigator.of(context).push(ItemsBuyPage([buyItemList[15]]));
      }),
    ];

    List<MainButtons> buttonList = [
      .
      .
      .
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (BuildContext context) => BrowsePage(buttonList)));
    //}
}

一旦我按下四个主按钮中的一个,我就可以进入导航到的屏幕。

新代码

  List<MainButtons> buttonList = [
      MainButtons(
        Icons.domain,
        'Class Supplies',
        'Page(suppliesList)',
      ),
      MainButtons(
        Icons.local_dining,
        'Food and Snacks',
          'Page(foodList)'
      ),
      MainButtons(
        Icons.hot_tub,
        'Personal Supplies',
      'Page(toiletriesList)'
        ,
      ),
      MainButtons(
        Icons.local_cafe,
        'Drinks',
          'Page(drinksList)',
      ),
    ];

错误:无法将参数类型“String”分配给参数类型“Route”。

  MainButtons(this.image, this.name, this.onTap);

  final IconData image;
  final String name;
  final String onTap;
.
.
.
 onTap:  () {
          Navigator.of(context).push(onTap);
        },

是的,您可以将列表作为原始数据放入另一个文件中,如下所示:

List<Product> foodList = [
  Product('Cliff Bar', 'assets/cliff bar.png')
  Product('Coffee', 'assets/coffee.png'),
];

您必须删除那里的导航器,因为该文件中未知的上下文因为它不是Widget。 然后在您的Widget中,您可以使用InkWell小部件(允许按下事件)包裹列表并将导航器放在那里。

您也可以将整个Widget编写为另一个文件并导入它。 这样你就可以在该文件中获得上下文了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM