当新邮件到达收件箱选项卡时,我想在BottomNavigationBar的Icon小部件的右上角显示通知徽章(彩色大理石).它与https://developer.android.com/preview/features/notification-badges.html类似,但对于我的情况,它显示在应用程序中.
有关在现有图标上绘制叠加层以创建自定义 Icon类的提示吗?
小智 33
是.可以使用Stack和Positioned小部件堆叠两个图标来完成.
new BottomNavigationBarItem(
title: new Text('Home'),
icon: new Stack(
children: <Widget>[
new Icon(Icons.home),
new Positioned( // draw a red marble
top: 0.0,
right: 0.0,
child: new Icon(Icons.brightness_1, size: 8.0,
color: Colors.redAccent),
)
]
),
)
Run Code Online (Sandbox Code Playgroud)
ych*_*ych 31
计数徽章的另一种变体(用图标堆栈实现,并包装在容器文本中,当计数器增加时会拉伸):
BottomNavigationBarItem(
icon: new Stack(
children: <Widget>[
new Icon(Icons.notifications),
new Positioned(
right: 0,
child: new Container(
padding: EdgeInsets.all(1),
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(6),
),
constraints: BoxConstraints(
minWidth: 12,
minHeight: 12,
),
child: new Text(
'$_counter',
style: new TextStyle(
color: Colors.white,
fontSize: 8,
),
textAlign: TextAlign.center,
),
),
)
],
),
title: Text('Notifications'),
),
Run Code Online (Sandbox Code Playgroud)
如果您还想处理onTap一些飞溅,请使用此小部件,您可以根据需要进一步自定义它:
复制这个类:
class NamedIcon extends StatelessWidget {
final IconData iconData;
final String text;
final VoidCallback onTap;
final int notificationCount;
const NamedIcon({
Key key,
this.onTap,
@required this.text,
@required this.iconData,
this.notificationCount,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return InkWell(
onTap: onTap,
child: Container(
width: 72,
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Stack(
alignment: Alignment.center,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(iconData),
Text(text, overflow: TextOverflow.ellipsis),
],
),
Positioned(
top: 0,
right: 0,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.red),
alignment: Alignment.center,
child: Text('$notificationCount'),
),
)
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
Scaffold(
appBar: AppBar(
title: Text('AppBar'),
actions: [
NamedIcon(
text: 'Inbox',
iconData: Icons.notifications,
notificationCount: 11,
onTap: () {},
),
NamedIcon(
text: 'Mails',
iconData: Icons.mail,
notificationCount: 1,
onTap: () {},
),
],
),
)
Run Code Online (Sandbox Code Playgroud)
有一个很好的包 [0] 可以使这变得像使用以下而不是图标一样简单:
Badge(
badgeContent: Text('3'),
child: Icon(Icons.settings),
)
Run Code Online (Sandbox Code Playgroud)
0:https : //pub.dev/packages/badges
小智 6
new BottomNavigationBar(
type: BottomNavigationBarType.fixed,
fixedColor: const Color(0xFF2845E7),
items: [
new BottomNavigationBarItem(
icon: new Icon(
Icons.home,
),
title: new Text(
"Home",
),
),
new BottomNavigationBarItem(
icon: new Icon(
Icons.call,
),
title: new Text(
"Calls",
)),
new BottomNavigationBarItem(
icon: new Icon(
Icons.camera_alt,
),
title: new Text(
"Camera",
)),
new BottomNavigationBarItem(
icon: new Stack(children: <Widget>[
new Icon(Icons.favorite),
new Positioned(
top: -1.0,
right: -1.0,
child: new Stack(
children: <Widget>[
new Icon(
Icons.brightness_1,
size: 12.0,
color: const Color(0xFF2845E7),
),
],
))
]),
title: new Text(
"Stories",
)),
new BottomNavigationBarItem(
icon: new Icon(
Icons.account_circle,
),
title: new Text(
"Contacts",
)),
],
onTap: (){},
currentIndex: 0,
),
Run Code Online (Sandbox Code Playgroud)
小智 5
您还可以嵌套堆栈。例如,如果您想在shopping_cart图标上添加item_count,则可以执行以下操作:
icon: new Stack(
children: <Widget>[
new Icon(Icons.shopping_cart),
new Positioned(
top: 1.0,
right: 0.0,
child: new Stack(
children: <Widget>[
new Icon(Icons.brightness_1,
size: 18.0, color: Colors.green[800]),
new Positioned(
top: 1.0,
right: 4.0,
child: new Text(item_count,
style: new TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.w500)),
)
],
),
)
],
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9562 次 |
| 最近记录: |