Flutter SVG as ImageIcon
4,126
You actually can't do it as mentioned here by Flutter SVG creator that PictureProvider
and ImageProvider
are incompatible and ImageIcon
needs an ImageProvider
.
However, you don't actually need to do it, and you probably won't as well. You can refactor your code to use:
SvgPicture.asset(item.icon, height: 24, width: 24)
And nothing else. That should be enough, no need for more complexity.
Author by
ruclip
Updated on December 22, 2022Comments
-
ruclip 11 minutes
My designer has switched from PNG to SVG files, 'cause our icons are to pixelated.
How I can use a SVG as an ImageIcon?.
Previous I have used this for PNG files:
ImageIcon(AssetImage('assets/images/ic_home.png')
I'm using
flutter_svg
(SvgPicture.asset(item.icon, height: 24, width: 24)
)Thank you in advance for your help!
-
ruclip over 2 yearsThanks for your comment and the link. How is your code example different with mine?
-
Miguel Ruivo over 2 yearsIt isn't. I'm just explaining you exactly that you should keep it that way.
-
ruclip over 2 yearsThe Problem is I need to tint the icon after creating. (TabBar). For that I need an Icon.
-
Miguel Ruivo over 2 yearsYou can wrap your
SvgPicture
with aColorFiltered
widget for that. -
ruclip over 2 yearsHow will ColorFiltered help? I can also change the color of the SVG direclty (
SvgPicture.asset(item.icon, height: 24, width: 24, color: Colors.grey)
). I'm searching some way to do it direclty with TabBar.TabBar( labelColor: AppColors.green, unselectedLabelColor: AppColors.greyDark,);
-
Miguel Ruivo over 2 yearsYou can tint your child widget with it.
-
ruclip over 2 yearsI solved the problem with rebuilding widget when tabcontroller index has changed. ``` _tabColorListener = () { if (_tabController.indexIsChanging) setState(() {}); };``` ``` for (int i = 0; i < navigationItems.length; i++) { tabs.add(SizedBox( height: kBottomNavigationBarHeight, child: Tab( icon: SvgPicture.asset(navigationItems[i].icon, height: 24, width: 24, color: i == _tabController.index ? AppColors.green : AppColors.greyDark), ), )); }``` Thanks a lot for your help and ideas @Miguel Ruivo