0

So I have a list of teams, each team has a different icon. I'm saving the icons as String in Firebase because my idea was to do this: For example for team blue; instead of using Icons.blue I would use MyCustomIcons.myBlue whereas myBlue is a String I read from the database. How can I get this to work with the dot operator?

I don't know how else I would dynamicaly use different icons for each team.

Sorry if this is stupid. I'm very new to programming.

Some code:

            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: EdgeInsets.only(left: 10.0, right: 10.0),
                  child: Text(
                    teams[teamsCounter].name,
                    style: widget.teamNameTextStyle,
                    textAlign: TextAlign.center,
                  ),
                ),
                Icon(
                  Icons.gamepad, // this way this is static for all teams. 
// what I would like to do is something like: MyCustomIcons.widget.teams[teamsCounter].icon,
                  size: widget.iconSize,
                  color: widget.iconColor,
                ),
              ],
            ),
Nerdenberg
  • 41
  • 12
  • it would be helpful if you provided some code that you've tried, and some context of where your codes fits into the rest of your app. – pianoman102 Jun 04 '21 at 16:20
  • @pianoman102 I edited the post and added some code. Thanks for replying – Nerdenberg Jun 04 '21 at 16:28
  • Ok I see what you're saying. I would check this out: https://stackoverflow.com/a/65804325/7514409. You would want to store the integer constants in firebase instead of a string. `Icon` takes a parameter of `IconData` so you can't pass a string (https://api.flutter.dev/flutter/widgets/Icon/Icon.html). So in the end you could do something like `Icon(IconData(teams[teamsCounter].iconVal), size: widget.iconSize, color: widget.iconColor)` – pianoman102 Jun 04 '21 at 16:38

1 Answers1

0

Ok I see what you're saying. I would check this out: https://stackoverflow.com/a/65804325/7514409. You would want to store the integer constants in firebase instead of a string. Icon takes a parameter of IconData so you can't pass a string (https://api.flutter.dev/flutter/widgets/Icon/Icon.html). So in the end you could do something like

Icon(
    IconData(teams[teamsCounter].iconVal, fontFamily: 'MaterialIcons'), 
    size: widget.iconSize, 
    color: widget.iconColor
)
pianoman102
  • 541
  • 8
  • 22