I want to use my image from assets as an icon, and then I build my lists, but there are some errors that occurred when trying to add Image.asset
as a class to the icon as a variable with IconData
as data type, instead of using Icons as a class.
Any idea to fix this problem?
Thank you.
class Menu {
const Menu({this.icon, this.title});
final IconData icon;
final String title;
}
const List<Menu> menus = const <Menu>[
const Menu(title: 'menu_icon_1', icon: Image.asset('assets/menu/1-1.png')),
],
error: The argument type 'Image' can't be assigned to the parameter type 'IconData'. (argument_type_not_assignable at [sinergi] lib\home.dart:12)
please use ImageIcon
class Menu {
const Menu({this.icon, this.title});
final ImageIcon icon;
final String title;
}
const List<Menu> menus = const <Menu>[
const Menu(title: 'menu_icon_1', icon: ImageIcon(AssetImage('assets/menu/1-1.png'))),
];
To set the image to be displayed as the icon , you need to pass an ImageProvider
instance. For that purpose, you need to create an instance of any class that's a descendant of ImageProvider
such as AssetImage
, NetworkImage
, MemoryImage
, and ResizeImage
. The below example uses AssetImage
to load the image.
const List<Menu> menus = const <Menu>[
const Menu(title: 'menu_icon_1', icon: ImageIcon(AssetImage('assets/menu/1-1.png'))),
];
if you want default icon
class Menu {
const Menu({this.icon, this.title});
final IconData icon;
final String title;
}
const List<Menu> menus = const <Menu>[
const Menu(title: 'Trending', icon: Icons.trending_up),
const Menu(title: 'Favorite', icon: Icons.favorite),
const Menu(title: 'Search', icon: Icons.search),
const Menu(title: 'Category', icon: Icons.category),
const Menu(title: 'Settings', icon: Icons.settings),
];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.