Is it possible to select a random icon in Icons or FontAwesomeIcons, etc

Viewed 2158

Context:

I'm discovering both flutter and dart. And I'm only doing app for my own pleasure.

My Objective

I would like to make a stupid app that display a random icon on each day (like word of the day).

However I'm not sure how to proceed at this time. In all examples the code directly references the IconData field that are declared in respective classes like FontAwesomeIcons or Icons for material icons.

These fields are declared as static const. What would be the most correct way to access those fields in order to say put them in a list where I can randomly pick an index ?

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 Answers

Just use the Icon(IconData()) constructor.

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

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

populate your table of points with some values of the characters in the chosen font face.

If you prefer, just create a list of the icons by name like this:

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

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