Flutter 如何在颤振应用程序中将png文件用作图标?
为了能够使用我的图标,我将它们上传到,但它只接受svg文件。因为我的图标是.png格式的,所以我使用转换器将它们转换成svg。转换完成后,我上传了它们,但它们似乎没有被flattericon.com正确识别,但我无法在应用程序上看到图标 我的应用程序中有一个自定义的底部导航栏,它只接受iconData作为参数。所以我必须使用一个实际的图标。我试着使用AppBar上的自定义图标,以便能够知道是否是底部导航栏导致了问题,但它是相同的Flutter 如何在颤振应用程序中将png文件用作图标?,flutter,svg,icons,png,Flutter,Svg,Icons,Png,为了能够使用我的图标,我将它们上传到,但它只接受svg文件。因为我的图标是.png格式的,所以我使用转换器将它们转换成svg。转换完成后,我上传了它们,但它们似乎没有被flattericon.com正确识别,但我无法在应用程序上看到图标 我的应用程序中有一个自定义的底部导航栏,它只接受iconData作为参数。所以我必须使用一个实际的图标。我试着使用AppBar上的自定义图标,以便能够知道是否是底部导航栏导致了问题,但它是相同的 我读到一些评论说svg的内容很重要。因为当我直接下载svg格式的随
我读到一些评论说svg的内容很重要。因为当我直接下载svg格式的随机图标并将其上传时,它在屏幕上被正确识别。我需要做什么?从我的旧项目中提取了一些代码: 基本思想是
图标
或活动图标
命名参数请求小部件,包括但不限于图标
小部件
BottomNavigationBarItem(
title: Text('REALTIME SCHEDULE'),
activeIcon: TrackIcon(colorValue: Colors.white70),
icon: TrackIcon(colorValue: Color(0xff424150)),
),
从我的旧项目中提取了一些代码: 基本思想是
图标
或活动图标
命名参数请求小部件,包括但不限于图标
小部件
BottomNavigationBarItem(
title: Text('REALTIME SCHEDULE'),
activeIcon: TrackIcon(colorValue: Colors.white70),
icon: TrackIcon(colorValue: Color(0xff424150)),
),
使用
图标:Image.asset(“assets/home.png”)
完整代码示例:
class MyTabBar extends StatefulWidget {
@override
_MyTabBarState createState() => _MyTabBarState();
}
class _MyTabBarState extends State<MyTabBar> with TickerProviderStateMixin {
int tabIndex=0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.white,
child: tabIndex ==0 ?BottomTabBarHome()
:tabIndex == 1? BottomTabBarMail(): BottomTabBarProfile()
),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Image.asset("assets/home.png", color: Colors.grey,),
activeIcon: Image.asset("assets/home.png", color: Colors.blue,),
title: Text('')
),
BottomNavigationBarItem(
icon: Image.asset("assets/mail.png", color: Colors.grey,),
activeIcon: Image.asset("assets/mail.png", color: Colors.blue,),
title: Text('')
),
BottomNavigationBarItem(
icon: Image.asset("assets/account.png", color: Colors.grey,),
activeIcon: Image.asset("assets/account.png", color: Colors.blue,),
title: Text('')
)
],
currentIndex: tabIndex,
selectedItemColor: Colors.blueAccent,
onTap: (int index){
setState(() {
tabIndex = index;
});
},
),
);
}
}
class BottomTabBarHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(" Home Screen"),
),
);
}
}
class BottomTabBarMail extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text("Mail Screen"),
),
);
}
}
class BottomTabBarProfile extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(" Profile Screen"),
),
);
}
}
类MyTabBar扩展StatefulWidget{
@凌驾
_MyTabBarState createState()=>\u MyTabBarState();
}
类_MyTabBarState使用TickerProviderStateMixin扩展状态{
int-tabIndex=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:容器(
颜色:颜色,白色,
子项:tabIndex==0?BottomTabBarHome()
:tabIndex==1?BottomTabBarMail():BottomTabBarProfile()
),
底部导航栏:底部导航栏(
项目:[
底部导航气压计(
图标:Image.asset(“assets/home.png”,颜色:Colors.grey,),
activeIcon:Image.asset(“assets/home.png”,颜色:Colors.blue,),
标题:文本(“”)
),
底部导航气压计(
图标:Image.asset(“assets/mail.png”,颜色:Colors.grey,),
activeIcon:Image.asset(“assets/mail.png”,颜色:Colors.blue,),
标题:文本(“”)
),
底部导航气压计(
图标:Image.asset(“assets/account.png”,颜色:Colors.grey,),
activeIcon:Image.asset(“assets/account.png”,颜色:Colors.blue,),
标题:文本(“”)
)
],
currentIndex:tabIndex,
selectedItemColor:Colors.blueAccent,
onTap:(int索引){
设置状态(){
tabIndex=索引;
});
},
),
);
}
}
类BottomTabBarHome扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
子:文本(“主屏幕”),
),
);
}
}
类BottomTabBarMail扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
子项:文本(“邮件屏幕”),
),
);
}
}
类BottomTabBarProfile扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
子项:文本(“配置文件屏幕”),
),
);
}
}
使用
图标:Image.asset(“assets/home.png”)
完整代码示例:
class MyTabBar extends StatefulWidget {
@override
_MyTabBarState createState() => _MyTabBarState();
}
class _MyTabBarState extends State<MyTabBar> with TickerProviderStateMixin {
int tabIndex=0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.white,
child: tabIndex ==0 ?BottomTabBarHome()
:tabIndex == 1? BottomTabBarMail(): BottomTabBarProfile()
),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Image.asset("assets/home.png", color: Colors.grey,),
activeIcon: Image.asset("assets/home.png", color: Colors.blue,),
title: Text('')
),
BottomNavigationBarItem(
icon: Image.asset("assets/mail.png", color: Colors.grey,),
activeIcon: Image.asset("assets/mail.png", color: Colors.blue,),
title: Text('')
),
BottomNavigationBarItem(
icon: Image.asset("assets/account.png", color: Colors.grey,),
activeIcon: Image.asset("assets/account.png", color: Colors.blue,),
title: Text('')
)
],
currentIndex: tabIndex,
selectedItemColor: Colors.blueAccent,
onTap: (int index){
setState(() {
tabIndex = index;
});
},
),
);
}
}
class BottomTabBarHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(" Home Screen"),
),
);
}
}
class BottomTabBarMail extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text("Mail Screen"),
),
);
}
}
class BottomTabBarProfile extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(" Profile Screen"),
),
);
}
}
类MyTabBar扩展StatefulWidget{
@凌驾
_MyTabBarState createState()=>\u MyTabBarState();
}
类_MyTabBarState使用TickerProviderStateMixin扩展状态{
int-tabIndex=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:容器(
颜色:颜色,白色,
子项:tabIndex==0?BottomTabBarHome()
:tabIndex==1?BottomTabBarMail():BottomTabBarProfile()
),
底部导航栏:底部导航栏(
项目:[
底部导航气压计(
图标:Image.asset(“assets/home.png”,颜色:Colors.grey,),
activeIcon:Image.asset(“assets/home.png”,颜色:Colors.blue,),
标题:文本(“”)
),
底部导航气压计(
图标:Image.asset(“assets/mail.png”,颜色:Colors.grey,),
activeIcon:Image.asset(“assets/mail.png”,颜色:Colors.blue,),
标题:文本(“”)
),
底部导航气压计(
图标:Image.asset(“assets/account.png”,颜色:Colors.grey,),
activeIcon:Image.asset(“assets/account.png”,颜色:Colors.blue,),
标题:文本(“”)
)
],
currentIndex:tabIndex,
selectedItemColor:Colors.blueAccent,
onTap:(int索引){
设置状态(){
tabIndex=索引;
});
},
),
);
}
}
类BottomTabBarHome扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
子:文本(“主屏幕”),
),
);
}
}
类BottomTabBarMail扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
子项:文本(“邮件屏幕”),
),
);
}
}
类BottomTabBarProfile扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
子项:文本(“配置文件屏幕”),
),
);
}
}
由于以上答案很有用(只需几行编辑),让我简化一下 使用这行代码可以使用PNG图标
图标:Image.asset(“assets/home.png”,颜色:Colors.grey,)
现在转到包含bottomNavigationBar颤振包文件的文件夹,编辑包本身或将文件复制到文件夹flatterApp/packages/your_package/[那些dart文件]
(第二个选项最好使用热重新加载进行测试)
注意:app
是fo的根