Flutter: Show different icons based on value
up vote
0
down vote
favorite
I have a list of objects each with an icon property as shown here:
List<Map<String, String>> _categories = [
'name': 'Sports',
'icon': 'directions_run',
,
'name': 'Politics',
'icon': 'gavel',
,
'name': 'Science',
'icon': 'wb_sunny',
,
];
I then have a widget that I am using inside of a ListView.builder() widget. Currently I am displaying a statically chosen icon to show with the text in my list. My question is how can I use the icon property in my objects to dynamically pick the icon that gets shown for each individual list item?
Widget _buildCategoryCards(BuildContext context, int index)
return Container(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Icon(Icons.directions_run),
SizedBox(width: 20.0),
Text(_categories[index]['name']),
],
),
),
),
);
dart flutter
add a comment |
up vote
0
down vote
favorite
I have a list of objects each with an icon property as shown here:
List<Map<String, String>> _categories = [
'name': 'Sports',
'icon': 'directions_run',
,
'name': 'Politics',
'icon': 'gavel',
,
'name': 'Science',
'icon': 'wb_sunny',
,
];
I then have a widget that I am using inside of a ListView.builder() widget. Currently I am displaying a statically chosen icon to show with the text in my list. My question is how can I use the icon property in my objects to dynamically pick the icon that gets shown for each individual list item?
Widget _buildCategoryCards(BuildContext context, int index)
return Container(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Icon(Icons.directions_run),
SizedBox(width: 20.0),
Text(_categories[index]['name']),
],
),
),
),
);
dart flutter
You can use index to decide which icon needs to be shown and then you can also use operator likeindex == 0 ? showThis: elseThis
– CopsOnRoad
Nov 12 at 2:47
How do I do that for each one though since it's a list?
– Garrett
Nov 12 at 11:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a list of objects each with an icon property as shown here:
List<Map<String, String>> _categories = [
'name': 'Sports',
'icon': 'directions_run',
,
'name': 'Politics',
'icon': 'gavel',
,
'name': 'Science',
'icon': 'wb_sunny',
,
];
I then have a widget that I am using inside of a ListView.builder() widget. Currently I am displaying a statically chosen icon to show with the text in my list. My question is how can I use the icon property in my objects to dynamically pick the icon that gets shown for each individual list item?
Widget _buildCategoryCards(BuildContext context, int index)
return Container(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Icon(Icons.directions_run),
SizedBox(width: 20.0),
Text(_categories[index]['name']),
],
),
),
),
);
dart flutter
I have a list of objects each with an icon property as shown here:
List<Map<String, String>> _categories = [
'name': 'Sports',
'icon': 'directions_run',
,
'name': 'Politics',
'icon': 'gavel',
,
'name': 'Science',
'icon': 'wb_sunny',
,
];
I then have a widget that I am using inside of a ListView.builder() widget. Currently I am displaying a statically chosen icon to show with the text in my list. My question is how can I use the icon property in my objects to dynamically pick the icon that gets shown for each individual list item?
Widget _buildCategoryCards(BuildContext context, int index)
return Container(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Icon(Icons.directions_run),
SizedBox(width: 20.0),
Text(_categories[index]['name']),
],
),
),
),
);
dart flutter
dart flutter
asked Nov 12 at 1:33
Garrett
422318
422318
You can use index to decide which icon needs to be shown and then you can also use operator likeindex == 0 ? showThis: elseThis
– CopsOnRoad
Nov 12 at 2:47
How do I do that for each one though since it's a list?
– Garrett
Nov 12 at 11:39
add a comment |
You can use index to decide which icon needs to be shown and then you can also use operator likeindex == 0 ? showThis: elseThis
– CopsOnRoad
Nov 12 at 2:47
How do I do that for each one though since it's a list?
– Garrett
Nov 12 at 11:39
You can use index to decide which icon needs to be shown and then you can also use operator like
index == 0 ? showThis: elseThis
– CopsOnRoad
Nov 12 at 2:47
You can use index to decide which icon needs to be shown and then you can also use operator like
index == 0 ? showThis: elseThis
– CopsOnRoad
Nov 12 at 2:47
How do I do that for each one though since it's a list?
– Garrett
Nov 12 at 11:39
How do I do that for each one though since it's a list?
– Garrett
Nov 12 at 11:39
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Change your List
to store an IconData
instead of a String
:
List<Map<String, IconData>> _categories = [
'name': 'Sports',
'icon': Icons.directions_run,
,
'name': 'Politics',
'icon': Icons.gavel,
,
'name': 'Science',
'icon': Icons.wb_sunny,
,
];
Then, call the IconData
from your build method:
Widget _buildCategoryCards(BuildContext context, int index)
return Container(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Icon(_categories[index]['icon']),
SizedBox(width: 20.0),
Text(_categories[index]['name']),
],
),
),
),
);
Note that this is not useful (even not effecient) to use a Map
to do what you want. You should use a custom class:
Class Category
String name;
IconData icon;
Category(this.name, this.icon);
And then replace your List
with this:
List<Category> _categories = [
Category('Sports', Icons.directions_run),
Category('Politics', Icons.gavel),
Category('Science', Icons.wb_sunny),
];
finally in your Widget:
children: <Widget>[
Icon(_categories[index].icon),
SizedBox(width: 20.0),
Text(_categories[index].name),
],
Thank you this worked. Why isn't it useful / efficient to use Map in this situation?
– Garrett
Nov 13 at 0:05
1
Short answer: AMap
is meant to manage a collection of key/value pair data. It is kinda the same as aList
, but instead of accessing data through an index, you access it through a key (which can be of any type you want). So in your case, you have a List containing 3 collections of 1 item each. There is no need to have a collection if you have only 1 item in it.
– Muldec
Nov 13 at 12:00
Makes sense, thanks
– Garrett
Nov 14 at 0:39
add a comment |
up vote
0
down vote
Keep the icon identifier from Icons class in the list instead of storing the text. But if you want to create a mapping from icon label to icon identifier.
Can you show that in code?
– Garrett
Nov 12 at 11:39
@UdeshUK Answers like this are best suited as comment.
– CopsOnRoad
Nov 12 at 14:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254963%2fflutter-show-different-icons-based-on-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Change your List
to store an IconData
instead of a String
:
List<Map<String, IconData>> _categories = [
'name': 'Sports',
'icon': Icons.directions_run,
,
'name': 'Politics',
'icon': Icons.gavel,
,
'name': 'Science',
'icon': Icons.wb_sunny,
,
];
Then, call the IconData
from your build method:
Widget _buildCategoryCards(BuildContext context, int index)
return Container(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Icon(_categories[index]['icon']),
SizedBox(width: 20.0),
Text(_categories[index]['name']),
],
),
),
),
);
Note that this is not useful (even not effecient) to use a Map
to do what you want. You should use a custom class:
Class Category
String name;
IconData icon;
Category(this.name, this.icon);
And then replace your List
with this:
List<Category> _categories = [
Category('Sports', Icons.directions_run),
Category('Politics', Icons.gavel),
Category('Science', Icons.wb_sunny),
];
finally in your Widget:
children: <Widget>[
Icon(_categories[index].icon),
SizedBox(width: 20.0),
Text(_categories[index].name),
],
Thank you this worked. Why isn't it useful / efficient to use Map in this situation?
– Garrett
Nov 13 at 0:05
1
Short answer: AMap
is meant to manage a collection of key/value pair data. It is kinda the same as aList
, but instead of accessing data through an index, you access it through a key (which can be of any type you want). So in your case, you have a List containing 3 collections of 1 item each. There is no need to have a collection if you have only 1 item in it.
– Muldec
Nov 13 at 12:00
Makes sense, thanks
– Garrett
Nov 14 at 0:39
add a comment |
up vote
1
down vote
accepted
Change your List
to store an IconData
instead of a String
:
List<Map<String, IconData>> _categories = [
'name': 'Sports',
'icon': Icons.directions_run,
,
'name': 'Politics',
'icon': Icons.gavel,
,
'name': 'Science',
'icon': Icons.wb_sunny,
,
];
Then, call the IconData
from your build method:
Widget _buildCategoryCards(BuildContext context, int index)
return Container(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Icon(_categories[index]['icon']),
SizedBox(width: 20.0),
Text(_categories[index]['name']),
],
),
),
),
);
Note that this is not useful (even not effecient) to use a Map
to do what you want. You should use a custom class:
Class Category
String name;
IconData icon;
Category(this.name, this.icon);
And then replace your List
with this:
List<Category> _categories = [
Category('Sports', Icons.directions_run),
Category('Politics', Icons.gavel),
Category('Science', Icons.wb_sunny),
];
finally in your Widget:
children: <Widget>[
Icon(_categories[index].icon),
SizedBox(width: 20.0),
Text(_categories[index].name),
],
Thank you this worked. Why isn't it useful / efficient to use Map in this situation?
– Garrett
Nov 13 at 0:05
1
Short answer: AMap
is meant to manage a collection of key/value pair data. It is kinda the same as aList
, but instead of accessing data through an index, you access it through a key (which can be of any type you want). So in your case, you have a List containing 3 collections of 1 item each. There is no need to have a collection if you have only 1 item in it.
– Muldec
Nov 13 at 12:00
Makes sense, thanks
– Garrett
Nov 14 at 0:39
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Change your List
to store an IconData
instead of a String
:
List<Map<String, IconData>> _categories = [
'name': 'Sports',
'icon': Icons.directions_run,
,
'name': 'Politics',
'icon': Icons.gavel,
,
'name': 'Science',
'icon': Icons.wb_sunny,
,
];
Then, call the IconData
from your build method:
Widget _buildCategoryCards(BuildContext context, int index)
return Container(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Icon(_categories[index]['icon']),
SizedBox(width: 20.0),
Text(_categories[index]['name']),
],
),
),
),
);
Note that this is not useful (even not effecient) to use a Map
to do what you want. You should use a custom class:
Class Category
String name;
IconData icon;
Category(this.name, this.icon);
And then replace your List
with this:
List<Category> _categories = [
Category('Sports', Icons.directions_run),
Category('Politics', Icons.gavel),
Category('Science', Icons.wb_sunny),
];
finally in your Widget:
children: <Widget>[
Icon(_categories[index].icon),
SizedBox(width: 20.0),
Text(_categories[index].name),
],
Change your List
to store an IconData
instead of a String
:
List<Map<String, IconData>> _categories = [
'name': 'Sports',
'icon': Icons.directions_run,
,
'name': 'Politics',
'icon': Icons.gavel,
,
'name': 'Science',
'icon': Icons.wb_sunny,
,
];
Then, call the IconData
from your build method:
Widget _buildCategoryCards(BuildContext context, int index)
return Container(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Icon(_categories[index]['icon']),
SizedBox(width: 20.0),
Text(_categories[index]['name']),
],
),
),
),
);
Note that this is not useful (even not effecient) to use a Map
to do what you want. You should use a custom class:
Class Category
String name;
IconData icon;
Category(this.name, this.icon);
And then replace your List
with this:
List<Category> _categories = [
Category('Sports', Icons.directions_run),
Category('Politics', Icons.gavel),
Category('Science', Icons.wb_sunny),
];
finally in your Widget:
children: <Widget>[
Icon(_categories[index].icon),
SizedBox(width: 20.0),
Text(_categories[index].name),
],
edited Nov 12 at 12:58
answered Nov 12 at 12:47
Muldec
3698
3698
Thank you this worked. Why isn't it useful / efficient to use Map in this situation?
– Garrett
Nov 13 at 0:05
1
Short answer: AMap
is meant to manage a collection of key/value pair data. It is kinda the same as aList
, but instead of accessing data through an index, you access it through a key (which can be of any type you want). So in your case, you have a List containing 3 collections of 1 item each. There is no need to have a collection if you have only 1 item in it.
– Muldec
Nov 13 at 12:00
Makes sense, thanks
– Garrett
Nov 14 at 0:39
add a comment |
Thank you this worked. Why isn't it useful / efficient to use Map in this situation?
– Garrett
Nov 13 at 0:05
1
Short answer: AMap
is meant to manage a collection of key/value pair data. It is kinda the same as aList
, but instead of accessing data through an index, you access it through a key (which can be of any type you want). So in your case, you have a List containing 3 collections of 1 item each. There is no need to have a collection if you have only 1 item in it.
– Muldec
Nov 13 at 12:00
Makes sense, thanks
– Garrett
Nov 14 at 0:39
Thank you this worked. Why isn't it useful / efficient to use Map in this situation?
– Garrett
Nov 13 at 0:05
Thank you this worked. Why isn't it useful / efficient to use Map in this situation?
– Garrett
Nov 13 at 0:05
1
1
Short answer: A
Map
is meant to manage a collection of key/value pair data. It is kinda the same as a List
, but instead of accessing data through an index, you access it through a key (which can be of any type you want). So in your case, you have a List containing 3 collections of 1 item each. There is no need to have a collection if you have only 1 item in it.– Muldec
Nov 13 at 12:00
Short answer: A
Map
is meant to manage a collection of key/value pair data. It is kinda the same as a List
, but instead of accessing data through an index, you access it through a key (which can be of any type you want). So in your case, you have a List containing 3 collections of 1 item each. There is no need to have a collection if you have only 1 item in it.– Muldec
Nov 13 at 12:00
Makes sense, thanks
– Garrett
Nov 14 at 0:39
Makes sense, thanks
– Garrett
Nov 14 at 0:39
add a comment |
up vote
0
down vote
Keep the icon identifier from Icons class in the list instead of storing the text. But if you want to create a mapping from icon label to icon identifier.
Can you show that in code?
– Garrett
Nov 12 at 11:39
@UdeshUK Answers like this are best suited as comment.
– CopsOnRoad
Nov 12 at 14:31
add a comment |
up vote
0
down vote
Keep the icon identifier from Icons class in the list instead of storing the text. But if you want to create a mapping from icon label to icon identifier.
Can you show that in code?
– Garrett
Nov 12 at 11:39
@UdeshUK Answers like this are best suited as comment.
– CopsOnRoad
Nov 12 at 14:31
add a comment |
up vote
0
down vote
up vote
0
down vote
Keep the icon identifier from Icons class in the list instead of storing the text. But if you want to create a mapping from icon label to icon identifier.
Keep the icon identifier from Icons class in the list instead of storing the text. But if you want to create a mapping from icon label to icon identifier.
answered Nov 12 at 7:56
UdeshUK
400413
400413
Can you show that in code?
– Garrett
Nov 12 at 11:39
@UdeshUK Answers like this are best suited as comment.
– CopsOnRoad
Nov 12 at 14:31
add a comment |
Can you show that in code?
– Garrett
Nov 12 at 11:39
@UdeshUK Answers like this are best suited as comment.
– CopsOnRoad
Nov 12 at 14:31
Can you show that in code?
– Garrett
Nov 12 at 11:39
Can you show that in code?
– Garrett
Nov 12 at 11:39
@UdeshUK Answers like this are best suited as comment.
– CopsOnRoad
Nov 12 at 14:31
@UdeshUK Answers like this are best suited as comment.
– CopsOnRoad
Nov 12 at 14:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254963%2fflutter-show-different-icons-based-on-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can use index to decide which icon needs to be shown and then you can also use operator like
index == 0 ? showThis: elseThis
– CopsOnRoad
Nov 12 at 2:47
How do I do that for each one though since it's a list?
– Garrett
Nov 12 at 11:39