【问题标题】:how to make a list of icons in flutter, dart?如何在颤振,飞镖中制作图标列表?
【发布时间】:2021-05-06 18:45:28
【问题描述】:

我是 Android Studio 和 Flutter 的新手。 我正在尝试制作一个可以迭代到 ListView 中的图标列表。

我收到此错误: The element type 'IconData' can't be assigned to the list type 'Icon'.

为此:

   static const _iconTypes = <Icon>[
   Icons.cake,
   Icons.add_location_sharp,
   Icons.zoom_in_outlined,
   Icons.auto_awesome_motion,
   Icons.call_end_sharp,
   Icons.equalizer_rounded,
   Icons.wifi_lock,
   Icons.mail,
  ];

&lt;icon&gt; 在这里使用的数据类型不正确吗?

谢谢

【问题讨论】:

    标签: flutter android-studio dart


    【解决方案1】:

    应该是IconData

      static const _iconTypes = <IconData>[
        Icons.cake,
        Icons.add_location_sharp,
        Icons.zoom_in_outlined,
        Icons.auto_awesome_motion,
        Icons.call_end_sharp,
        Icons.equalizer_rounded,
        Icons.wifi_lock,
        Icons.mail,
      ];
    

    你可以像这样使用它

        ...
         Container(
          child: ListView(
            children: _iconTypes.map((icon) => Icon(icon)).toList(),
          ),
        )
        ...
    

    【讨论】:

      【解决方案2】:

      改为这样做:

      List<IconData> _iconTypes = [ 
       Icons.cake,
       Icons.add_location_sharp,
       Icons.zoom_in_outlined,
       Icons.auto_awesome_motion,
       Icons.call_end_sharp,
       Icons.equalizer_rounded,
       Icons.wifi_lock,
       Icons.mail,
      ];
      

      如果这仍然不起作用,请在问题中也包含您的ListView

      【讨论】:

        【解决方案3】:
            final  List<IconData> icons = [
            Icons.sentiment_very_dissatisfied,
            Icons.home,
            Icons.drafts,
            Icons.backspace
        ];
        

        我希望您可以轻松地在您的小部件中使用它。

        【讨论】:

          猜你喜欢
          • 2020-08-12
          • 2021-01-24
          • 1970-01-01
          • 2022-01-16
          • 2020-11-19
          • 1970-01-01
          • 2020-11-05
          • 2021-10-07
          • 1970-01-01
          相关资源
          最近更新 更多