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

oyjwcjzk  于 2023-02-25  发布在  Flutter
关注(0)|答案(2)|浏览(142)
    • 背景:**

我在探索flutter和dart,而且我做app只是为了自己的乐趣。

    • 我的目标**

我想做一个愚蠢的应用程序,每天显示一个随机的图标(就像一天中的一句话)。
但是我不知道现在该怎么做,在所有的例子中,代码都直接引用了IconData字段,这些字段是在各自的类中声明的,比如FontAwesomeIconsIcons
这些字段被声明为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');
pdkcd3nj

pdkcd3nj1#

只需使用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)]);
6kkfgxo0

6kkfgxo02#

IconData getRandomIcon(){
  final List<int> points = <int>[0xe0b0, 0xe0b1, 0xe0b2, 0xe0b3, 0xe0b4];
  final Random random = Random();
  const String chars = '0123456789ABCDEF';
  int length = 3;
  String hex = '0xe';
  while(length-- > 0) hex += chars[(random.nextInt(16)) | 0];
  return IconData(int.parse(hex), fontFamily: 'MaterialIcons');
}

相关问题