【问题标题】:moving a list into a separate dart file that is used in multiple dart files?将列表移动到用于多个 dart 文件的单独 dart 文件中?
【发布时间】:2019-09-11 01:46:48
【问题描述】:

是否可以将列表从 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);
        },

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

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

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

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

    你也可以写一个完整的小部件作为另一个文件并导入它。这样您就可以在该文件中获得上下文

    【讨论】:

      猜你喜欢
      • 2019-09-09
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 2021-06-25
      相关资源
      最近更新 更多