Flutter 动态图标未在生产版本中显示
Flutter Dynamic Icons not showing on production build
我正在做一个项目,我必须使用从我们的网络服务读取的图标代码动态填充图标小部件。
问题是,当我处于调试模式时,一切正常,图标按预期显示,但一旦我构建生产版本 apk,它就不再显示一些图标了。这是我的小部件代码。
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
return Scaffold(
backgroundColor: CustomColors.primaryVariant,
appBar: AppBar(
title: Text(widget.title ?? ''),
backgroundColor: CustomColors.primary,
),
body: Container(
color: CustomColors.primary,
height: MediaQuery.of(context).size.height,
child: Stack(
children: [
Visibility(
visible: items.length > 0,
child: ListView.builder(
shrinkWrap: true,
itemCount: items.length,
itemBuilder: (context, index) {
var item = items[index];
return Card(
color: CustomColors.primaryVariant,
elevation: 5.0,
margin: new EdgeInsets.symmetric(vertical: .5),
child: Container(
child: ListTile(
leading: Container(
padding: EdgeInsets.only(right: 12.0),
decoration: new BoxDecoration(
border: new Border(
right: new BorderSide(
width: 1.0, color: Colors.white24))),
child: Icon(CustomIcon(item.code), color: Colors.white),
),
title: Text(
item.detail.capitalize(),
style: TextStyle(color: Colors.white, fontSize: 16),
),
subtitle: Text(
item.item,
style: TextStyle(color: Colors.white70, fontSize: 14),
),
),
),
);
},
),
),
Visibility(
visible: items.length == 0,
child: ListView.builder(
shrinkWrap: true,
itemCount: 4,
itemBuilder: (context, index) {
return Card(
color: CustomColors.primaryVariant,
elevation: 5.0,
margin: new EdgeInsets.symmetric(vertical: .5),
child: Container(
child: ListTile(
leading: Container(
padding: EdgeInsets.only(right: 12.0),
decoration: new BoxDecoration(
border: new Border(
right: new BorderSide(
width: 1.0, color: Colors.white24))),
child: ShimmerSkeleton(
height: 20,
width: 20,
),
),
title: SizedBox(
width: 60,
child: ShimmerSkeleton(
height: 20,
width: 60,
),
),
subtitle: Padding(
padding: EdgeInsets.only(top: 5),
child: ShimmerSkeleton(
width: 90,
),
),
),
),
);
},
),
),
Positioned(
bottom: 0,
left: MediaQuery.of(context).size.width * 0.5 - 130,
child: Container(
margin: EdgeInsets.symmetric(vertical: 25),
child: Opacity(
opacity: .8,
child: SizedBox(
height: 60,
width: MediaQuery.of(context).size.width,
child: ListTile(
leading: CircleAvatar(
radius: 26,
backgroundImage: AssetImage('assets/img/rodal.jpg'),
),
title: Text(
'SUPPORTED BY',
style: theme.textTheme.title.copyWith(
fontWeight: FontWeight.w600, fontSize: 14),
),
subtitle: Text(
'Rodaltech, SRL',
style: theme.textTheme.title.copyWith(
fontWeight: FontWeight.w600, fontSize: 20),
),
),
),
),
),
)
],
),
),
);
}
}
class CustomIcon extends IconData {
CustomIcon(int codePoint) : super(codePoint, fontFamily: 'MaterialIcons');
}
我的 ItemDetail 模型:
class ItemDetail {
ItemDetail({
this.code,
this.item,
this.detail,
});
int code;
String item;
String detail;
ItemDetail copyWith({
int code,
String item,
String detail,
}) =>
ItemDetail(
code: code ?? this.code,
item: item ?? this.item,
detail: detail ?? this.detail,
);
factory ItemDetail.fromJson(Map<String, dynamic> json) =>
ItemDetail(
code: json["code"] == null ? null : json["code"],
item: json["item"] == null ? null : json["item"],
detail: json["detail"] == null ? null : json["detail"],
);
Map<String, dynamic> toJson() => {
"code": code == null ? null : code,
"item": item == null ? null : item,
"detail": detail == null ? null : detail,
};
}
还添加了调试模式和生产模式的屏幕截图:
添加 --no-tree-shake-icons
到 flutter build,为我解决了这个问题。
我正在做一个项目,我必须使用从我们的网络服务读取的图标代码动态填充图标小部件。
问题是,当我处于调试模式时,一切正常,图标按预期显示,但一旦我构建生产版本 apk,它就不再显示一些图标了。这是我的小部件代码。
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
return Scaffold(
backgroundColor: CustomColors.primaryVariant,
appBar: AppBar(
title: Text(widget.title ?? ''),
backgroundColor: CustomColors.primary,
),
body: Container(
color: CustomColors.primary,
height: MediaQuery.of(context).size.height,
child: Stack(
children: [
Visibility(
visible: items.length > 0,
child: ListView.builder(
shrinkWrap: true,
itemCount: items.length,
itemBuilder: (context, index) {
var item = items[index];
return Card(
color: CustomColors.primaryVariant,
elevation: 5.0,
margin: new EdgeInsets.symmetric(vertical: .5),
child: Container(
child: ListTile(
leading: Container(
padding: EdgeInsets.only(right: 12.0),
decoration: new BoxDecoration(
border: new Border(
right: new BorderSide(
width: 1.0, color: Colors.white24))),
child: Icon(CustomIcon(item.code), color: Colors.white),
),
title: Text(
item.detail.capitalize(),
style: TextStyle(color: Colors.white, fontSize: 16),
),
subtitle: Text(
item.item,
style: TextStyle(color: Colors.white70, fontSize: 14),
),
),
),
);
},
),
),
Visibility(
visible: items.length == 0,
child: ListView.builder(
shrinkWrap: true,
itemCount: 4,
itemBuilder: (context, index) {
return Card(
color: CustomColors.primaryVariant,
elevation: 5.0,
margin: new EdgeInsets.symmetric(vertical: .5),
child: Container(
child: ListTile(
leading: Container(
padding: EdgeInsets.only(right: 12.0),
decoration: new BoxDecoration(
border: new Border(
right: new BorderSide(
width: 1.0, color: Colors.white24))),
child: ShimmerSkeleton(
height: 20,
width: 20,
),
),
title: SizedBox(
width: 60,
child: ShimmerSkeleton(
height: 20,
width: 60,
),
),
subtitle: Padding(
padding: EdgeInsets.only(top: 5),
child: ShimmerSkeleton(
width: 90,
),
),
),
),
);
},
),
),
Positioned(
bottom: 0,
left: MediaQuery.of(context).size.width * 0.5 - 130,
child: Container(
margin: EdgeInsets.symmetric(vertical: 25),
child: Opacity(
opacity: .8,
child: SizedBox(
height: 60,
width: MediaQuery.of(context).size.width,
child: ListTile(
leading: CircleAvatar(
radius: 26,
backgroundImage: AssetImage('assets/img/rodal.jpg'),
),
title: Text(
'SUPPORTED BY',
style: theme.textTheme.title.copyWith(
fontWeight: FontWeight.w600, fontSize: 14),
),
subtitle: Text(
'Rodaltech, SRL',
style: theme.textTheme.title.copyWith(
fontWeight: FontWeight.w600, fontSize: 20),
),
),
),
),
),
)
],
),
),
);
}
}
class CustomIcon extends IconData {
CustomIcon(int codePoint) : super(codePoint, fontFamily: 'MaterialIcons');
}
我的 ItemDetail 模型:
class ItemDetail {
ItemDetail({
this.code,
this.item,
this.detail,
});
int code;
String item;
String detail;
ItemDetail copyWith({
int code,
String item,
String detail,
}) =>
ItemDetail(
code: code ?? this.code,
item: item ?? this.item,
detail: detail ?? this.detail,
);
factory ItemDetail.fromJson(Map<String, dynamic> json) =>
ItemDetail(
code: json["code"] == null ? null : json["code"],
item: json["item"] == null ? null : json["item"],
detail: json["detail"] == null ? null : json["detail"],
);
Map<String, dynamic> toJson() => {
"code": code == null ? null : code,
"item": item == null ? null : item,
"detail": detail == null ? null : detail,
};
}
还添加了调试模式和生产模式的屏幕截图:
添加 --no-tree-shake-icons
到 flutter build,为我解决了这个问题。