语境:
我正在发现 flutter 和 dart。我做应用程序只是为了我自己的乐趣。
我的目标
我想制作一个愚蠢的应用程序,每天显示一个随机图标(如每日一词)。
但我现在不确定如何继续。在所有示例中,代码直接引用在各自的类中声明的 IconData 字段,例如FontAwesomeIcons
或Icons
材质图标。
这些字段被声明为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);
Run Code Online (Sandbox Code Playgroud)
// 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> — material icon named "360".
static const IconData threesixty = IconData(0xe577, fontFamily: 'MaterialIcons');
/// <i class="material-icons md-36">3d_rotation</i> — material icon named "3d rotation".
static const IconData threed_rotation = IconData(0xe84d, fontFamily: 'MaterialIcons');
Run Code Online (Sandbox Code Playgroud)
只需使用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'));
Run Code Online (Sandbox Code Playgroud)
用所选字体中字符的一些值填充点表。
如果您愿意,只需按名称创建图标列表,如下所示:
final List<IconData> iconData = <IconData>[Icons.call, Icons.school];
Icon randomIcon2() => Icon(iconData[r.nextInt(iconData.length)]);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3017 次 |
最近记录: |