Home > front end >  Using custom assets as icons in Flutter
Using custom assets as icons in Flutter

Time:06-30

I want to use custom assets instead of standard material icons for my Flutter app but I can't seem to get it working. After searching google for a solution I found the ImageIcon widget that's supposed to provide exactly that but it throws the following error:

The argument type 'ImageIcon' can't be assigned to the parameter type 'IconData'.

I also tried (as per one other solution) to directly supply Image.asset('..') to the IconData field but that isn't assignable either.

This is my code:

IconData icon = ImageIcon(AssetImage('assets/images/myimage.png'))

Since I am using other packages I don't want to go the route of creating my own containers..

CodePudding user response:

IconData is not a Widget, it's data which an Icon widget uses to display the icon.

ImageIcon can't be assigned to a variable of type IconData because it is, itself, already a widget. Simply use the ImageIcon directly in your widget tree, where you would have used an Icon.

// If you have something like this:
Row(
  children: [
    Icon(iconData),
    Text("Icon title"),
  ]
);

// Replace with this:
Row(
  children: [
    IconImage(AssetImage('assets/images/myimage.png')),
    Text("Icon title"),
  ]
)

CodePudding user response:

ImageIcon cannot be assigned to IconData but you can use it as follows:

Container( child:ImageIcon( AssetImage("images/icon.png"), color: Colors.red, size: 24, ), )

  • Related