Flutter 在Flatter中的汉堡菜单图标上添加徽章
嗨,请帮忙,我正在尝试在Flitter应用程序的汉堡菜单图标上添加一个红色徽章,如下所示Flutter 在Flatter中的汉堡菜单图标上添加徽章,flutter,dart,Flutter,Dart,嗨,请帮忙,我正在尝试在Flitter应用程序的汉堡菜单图标上添加一个红色徽章,如下所示 使用堆栈,类似的示例代码: 类通知图标扩展了无状态小部件{ 最终bool激活; 最后的双边缘折叠; const NotificationIcon({Key Key,this.active=true,this.marginTopLeft}) :super(key:key); @凌驾 小部件构建(构建上下文){ 最终颜色=活动颜色。白色:恒定颜色(0xff40a9ff); 最终保证金=marginTopLeft
使用
堆栈
,类似的示例代码:
类通知图标扩展了无状态小部件{
最终bool激活;
最后的双边缘折叠;
const NotificationIcon({Key Key,this.active=true,this.marginTopLeft})
:super(key:key);
@凌驾
小部件构建(构建上下文){
最终颜色=活动颜色。白色:恒定颜色(0xff40a9ff);
最终保证金=marginTopLeft±17;
返回容器(
宽度:24,
身高:24,
子:堆栈(
儿童:[
居中(
子:图标(
图标、通知、,
颜色:颜色,
),
),
定位(
右图:保证金,
顶部:页边距,
子:容器(
宽度:8,
身高:8,
装饰:盒子装饰(
颜色:主题。背景。原色,
形状:BoxShape.circle,
),
儿童:中心(
子:容器(
装饰:盒子装饰(
形状:BoxShape.circle,
颜色:颜色,
),
宽度:5,
身高:5,,
),
),
),
)
],
),
);
}
}
您可以通过徽章
插件来实现这一点
将其添加到程序包的“依赖项”下的pubspec.yaml文件中:
badges: ^1.1.4
然后进口,
import 'package:badges/badges.dart';
最后在AppBar
中添加Badge
AppBar(
leading: Badge(
position: BadgePosition.topEnd(top: 10, end: 10),
badgeContent: null,
child: IconButton(
icon: Icon(Icons.menu),
onPressed: () {},
),
),
title: Text('Badge Demo', style: TextStyle(color: Colors.black)),
backgroundColor: Colors.white,
)
使用
堆栈
小部件可以轻松完成。我已经给出了一个示例,说明了如何使用stack来实现这一点。请注意,您必须使用Scaffold.of(yourContext).openDrawer()
打开附加的抽屉(正如我在这里所做的),因为汉堡图标是手动放置在那里的
import 'package:flutter/material.dart';
void main() {
runApp(Example());
}
class Example extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Example",
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
leading: InkWell(
splashColor: Colors.lightBlue,
onTap: () => Scaffold.of(context).openDrawer(),
child: Center(
child: Container(
margin: EdgeInsets.only(left: 10),
width: 40,
height: 25,
child: Stack(
children: [
Icon(
Icons.menu,
color: Colors.grey[850],
),
Positioned(
left: 25,
top: 0,
child: Container(
width: 10,
height: 10,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
child: Center(
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.red,
),
width: 10,
height: 10,
),
),
),
)
],
),
),
),
),
title: Text("Hello"),
actions: [
],
),
body: Center(child: Text("Welcome")),
),
);
}
}
小部件构建(构建上下文){
最终GlobalKey _scaffoldKey=GlobalKey();
返回脚手架(
钥匙:_scaffoldKey,
抽屉:抽屉(),
appBar:appBar(
背景颜色:Colors.red,
标题:正文(
“一些文字”,
样式:TextStyle(颜色:Colors.white),
),
引导:InkWell(
颜色:颜色。浅蓝色,
onTap:(){
_scaffoldKey.currentState.openDrawer();
},
儿童:中心(
子:容器(
边距:仅限边距组(左:10),
宽度:40,
身高:25,
子:堆栈(
儿童:[
图标(
图标菜单,
颜色:颜色,白色,
),
定位(
左:25,
排名:0,
子:容器(
宽度:10,
身高:10,
装饰:盒子装饰(
颜色:颜色,红色,
形状:BoxShape.circle,
),
儿童:中心(
子:容器(
装饰:盒子装饰(
形状:BoxShape.circle,
颜色:颜色,红色,
),
宽度:10,
身高:10,
),
),
),
)
],
),
),
),
),
),
正文:安全区(
子:容器(
),
),
);
}
请用这个。如果你想显示号码,你也可以使用徽章
Widget build(BuildContext context) {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
return Scaffold(
key: _scaffoldKey,
drawer: Drawer(),
appBar: AppBar(
backgroundColor: Colors.red,
title: Text(
"Some Text",
style: TextStyle(color: Colors.white),
),
leading: InkWell(
splashColor: Colors.lightBlue,
onTap: () {
_scaffoldKey.currentState.openDrawer();
},
child: Center(
child: Container(
margin: EdgeInsets.only(left: 10),
width: 40,
height: 25,
child: Stack(
children: [
Icon(
Icons.menu,
color: Colors.white,
),
Positioned(
left: 25,
top: 0,
child: Container(
width: 10,
height: 10,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
child: Center(
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.red,
),
width: 10,
height: 10,
),
),
),
)
],
),
),
),
),
),
body: SafeArea(
child: Container(
),
),
);
}