是否可以在Icons或FontAwesomeIcons等中选择随机图标

时间:2019-01-11 17:28:51

标签: dart flutter

上下文:

我同时发现飞镖和飞镖。而且我只是出于自己的喜好而开发应用。

我的目标

我想制作一个愚蠢的应用程序,该应用程序每天都显示一个随机图标(例如一天中的单词)。

但是我目前不确定如何进行。在所有示例中,代码直接引用在诸如FontAwesomeIconsIcons之类的相应类中声明的IconData字段,以用于实体图标。

这些字段被声明为static const。为了说出将其放入可以随机选择索引的列表,最正确的访问方式是什么?

library font_awesome_flutter;

import 'package:flutter/widgets.dart';
import 'package:font_awesome_flutter/icon_data.dart';

// THIS FILE IS AUTOMATICALLY GENERATED!

class FontAwesomeIcons {
  static const IconData fiveHundredPx = const IconDataBrands(0xf26e);
  static const IconData accessibleIcon = const IconDataBrands(0xf368);
  static const IconData accusoft = const IconDataBrands(0xf369);
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';

/// Identifiers for the supported material design icons.
///
/// Use with the [Icon] class to show specific icons.
///
/// Icons are identified by their name as listed below.
///
/// To use this class, make sure you set `uses-material-design: true` in your
/// project's `pubspec.yaml` file in the `flutter` section. This ensures that
/// the MaterialIcons font is included in your application. This font is used to
/// display the icons. For example:
///
/// ```yaml
/// name: my_awesome_application
/// flutter:
///   uses-material-design: true
/// ```
///
/// See also:
///
///  * [Icon]
///  * [IconButton]
///  * [design.google.com/icons](https://design.google.com/icons/)
class Icons {
  Icons._();

  // Generated code: do not hand-edit.
  // See https://github.com/flutter/flutter/wiki/Updating-Material-Design-Fonts
  // BEGIN GENERATED

  /// <i class="material-icons md-36">360</i> &#x2014; material icon named "360".
  static const IconData threesixty = IconData(0xe577, fontFamily: 'MaterialIcons');

  /// <i class="material-icons md-36">3d_rotation</i> &#x2014; material icon named "3d rotation".
  static const IconData threed_rotation = IconData(0xe84d, fontFamily: 'MaterialIcons');

1 个答案:

答案 0 :(得分:1)

只需使用Icon(IconData())构造函数。

  final List<int> points = <int>[0xe0b0, 0xe0b1, 0xe0b2, 0xe0b3, 0xe0b4];
  final Random r = Random();

  Icon randomIcon() =>
    Icon(IconData(r.nextInt(points.length), fontFamily: 'MaterialIcons'));

使用所选字体中的某些字符值填充点表。

如果愿意,只需按以下名称创建图标列表:

  final List<IconData> iconData = <IconData>[Icons.call, Icons.school];

  Icon randomIcon2() => Icon(iconData[r.nextInt(iconData.length)]);