簡體   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