[英]How to display an image in the BottomNavigationBar in Flutter?
I need to display an image in the navigation bar like this:我需要在导航栏中显示这样的图像:
but I can't because NavigationBarItem class needs an icon, so when I use ImageIcon widget I lose the colors.但我不能,因为 NavigationBarItem class 需要一个图标,所以当我使用 ImageIcon 小部件时,我丢失了 colors。 There are multiple colors in the image.
图像中有多个 colors。
How to show the image in there or a way to make a colorful icon?如何在其中显示图像或制作彩色图标的方法?
And when I use当我使用
BottomNavigationBarItem(
icon: ImageIcon(
NetworkImage('https://picsum.photos/250?image=9'),
),
title: Text('Image'),
),
ImageIcon(
AssetImage("images/icon_more.png"),
color: Color(0xFF3A5A98),
),
instead of代替
new BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
change to this改成这个
BottomNavigationBarItem(
icon: ImageIcon(
AssetImage("images/icon_more.png"),
color: Color(0xFF3A5A98),
),
title: Text('Home'),
),
so apparently the solution is to use Image.asset('image_path') for icon/activeIcon in BottomNavigationBarItem if you want to display images as bottom bar navigation icons with original color .所以显然解决方案是使用 Image.asset('image_path') for icon/activeIcon in BottomNavigationBarItem 如果您想将图像显示为具有原始颜色的底栏导航图标。
However, it does give an error if your items: const <BottomNavigationBarItem>[]
has the keyword const
in it.但是,如果您的
items: const <BottomNavigationBarItem>[]
中包含关键字const
,它确实会出错。 If you remove it you should be able to use Image.asset
instead of ImageIcon
(which tints to color)如果你删除它,你应该能够使用
Image.asset
而不是ImageIcon
(着色着色)
As @asbah-riyas mentioned it can be done by providing the ImageIcon
widget to the icon
argument of BottomNavigationBarItem
.正如@asbah-riyas 提到的,可以通过将
ImageIcon
小部件提供给BottomNavigationBarItem
的icon
参数来完成。 Although you don't need to specify the color
in it虽然您不需要在其中指定
color
You can check or play with it in codepen here.您可以在此处的 codepen 中检查或使用它。
The NavigationBarItem's image property needs a Widget, not an Icon, so I could just use this. NavigationBarItem 的图像属性需要一个小部件,而不是一个图标,所以我可以使用它。
BottomNavigationBarItem(icon: Image.asset('images/symbol.png')),
The correct answer is BottomNavigationBarItem(icon: Image.asset('images/symbol.png')),正确答案是 BottomNavigationBarItem(icon: Image.asset('images/symbol.png')),
Provided you place your png image is images folder and mentioned same as images/image.png in your pubspeq.yaml under assets如果您将您的 png 图像放置在 images 文件夹中,并且在您的资产下的 pubspeq.yaml 中提及的 images/image.png 相同
You can use this instead:您可以改用它:
BottomNavigationBarItem(
icon: Image.asset(" your image here "),
),
label: 'home',
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.