简体   繁体   English

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

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

Is it possible to move a list from a dart file into its own dart file and then import the list? 是否可以将dart文件中的列表移动到自己的dart文件中,然后导入列表?

I have only gotten the four main inkwell buttons to be displayed. 我只得到了四个主要墨水池按钮才能显示。 Other than that I can't get past the ancenstortype error. 除此之外,我无法通过ancenstortype错误。 I believe the widget is not being passed on correctly. 我相信小部件没有正确传递。

https://pastebin.com/9zvRXpnu 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)));
    //}
}

Once I press one of the four main buttons, I should be able to go to the screen that is navigated to. 一旦我按下四个主按钮中的一个,我就可以进入导航到的屏幕。

NEW CODE 新代码

  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)',
      ),
    ];

ERROR: The argument type 'String' can't be assigned to the parameter type 'Route'. 错误:无法将参数类型“String”分配给参数类型“Route”。

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

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

Yes you can put the list as raw data into another file like this: 是的,您可以将列表作为原始数据放入另一个文件中,如下所示:

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

You have to delete the Navigator there because the context in not known in that file because its not a Widget. 您必须删除那里的导航器,因为该文件中未知的上下文因为它不是Widget。 Then in your Widget you can Wrap the list with an InkWell Widget (which allows an on press Event) and put the Navigator there. 然后在您的Widget中,您可以使用InkWell小部件(允许按下事件)包裹列表并将导航器放在那里。

You could also just write a whole Widget as the other File and import it. 您也可以将整个Widget编写为另一个文件并导入它。 That way you have the context in that file 这样你就可以在该文件中获得上下文了

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

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