Flutter 购物车计数器在返回时未进行更新
我在购物车屏幕上向购物车添加商品,但当我返回到产品以添加更多商品时,底部导航栏上的计数器没有更新,据我所知,活动已经在堆栈中,因此不会刷新,我可以在本机android(java)上的invalidateOptionMenu()函数中执行此操作,该函数刷新appbar,但我不知道如何在颤振中实现,也不知道它的底部导航 我已经在backpressed function上尝试了此代码Flutter 购物车计数器在返回时未进行更新,flutter,dart,Flutter,Dart,我在购物车屏幕上向购物车添加商品,但当我返回到产品以添加更多商品时,底部导航栏上的计数器没有更新,据我所知,活动已经在堆栈中,因此不会刷新,我可以在本机android(java)上的invalidateOptionMenu()函数中执行此操作,该函数刷新appbar,但我不知道如何在颤振中实现,也不知道它的底部导航 我已经在backpressed function上尝试了此代码 Navigator.of(context).push( MaterialPageRoute
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context){
return MainScreen();
},
),
);
在Willposcope上,
任何帮助都将不胜感激。下面是主屏幕
body: PageView(
physics: NeverScrollableScrollPhysics(),
controller: _pageController,
onPageChanged: onPageChanged,
children: <Widget>[
Home(),
FavoriteScreen(),
SearchScreen(),
CartScreen(),
Profile(),
],
),
bottomNavigationBar: BottomAppBar(
child: new Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
SizedBox(width:7),
IconButton(
icon: Icon(
Icons.home,
size: 24.0,
),
color: _page == 0
? Theme.of(context).accentColor
: Theme
.of(context)
.textTheme.caption.color,
onPressed: ()=>_pageController.jumpToPage(0),
),
IconButton(
icon:Icon(
Icons.favorite,
size: 24.0,
),
color: _page == 1
? Theme.of(context).accentColor
: Theme
.of(context)
.textTheme.caption.color,
onPressed: ()=>_pageController.jumpToPage(1),
),
IconButton(
icon: Icon(
Icons.search,
size: 24.0,
color: Theme.of(context).primaryColor,
),
color: _page == 2
? Theme.of(context).accentColor
: Theme
.of(context)
.textTheme.caption.color,
onPressed: ()=>_pageController.jumpToPage(2),
),
IconButton(
icon: IconBadge(
icon: Icons.shopping_cart,
size: 24.0,
),
color: _page == 3
? Theme.of(context).accentColor
: Theme
.of(context)
.textTheme.caption.color,
onPressed: ()=>_pageController.jumpToPage(3),
),
IconButton(
icon: Icon(
Icons.person,
size: 24.0,
),
color: _page == 4
? Theme.of(context).accentColor
: Theme
.of(context)
.textTheme.caption.color,
onPressed: ()=>_pageController.jumpToPage(4),
),
SizedBox(width:7),
],
),
color: Theme.of(context).primaryColor,
shape: CircularNotchedRectangle(),
),
floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
elevation: 4.0,
child: Icon(
Icons.search,
),
onPressed: ()=>_pageController.jumpToPage(2),
),
),
);
body:PageView(
物理学:NeverscrollableScroll物理学(),
控制器:_pageController,
onPageChanged:onPageChanged,
儿童:[
Home(),
FavoriteScreen(),
SearchScreen(),
CartScreen(),
Profile(),
],
),
bottomNavigationBar:BottomAppBar(
孩子:新的一排(
mainAxisSize:mainAxisSize.max,
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
尺寸箱(宽度:7),
图标按钮(
图标:图标(
我的家,
尺寸:24.0,
),
颜色:_page==0
主题。背景。强调颜色
:主题
.of(上下文)
.textTheme.caption.color,
按下:()=>\u页面控制器。跳转页面(0),
),
图标按钮(
图标:图标(
我的最爱,
尺寸:24.0,
),
颜色:_page==1
主题。背景。强调颜色
:主题
.of(上下文)
.textTheme.caption.color,
按下:()=>\u页面控制器。跳转页面(1),
),
图标按钮(
图标:图标(
Icons.search,
尺寸:24.0,
颜色:主题。背景。原色,
),
颜色:_page==2
主题。背景。强调颜色
:主题
.of(上下文)
.textTheme.caption.color,
按下:()=>\u页面控制器。跳转页面(2),
),
图标按钮(
图标:IconBadge(
图标:Icons.shopping\u cart,
尺寸:24.0,
),
颜色:_page==3
主题。背景。强调颜色
:主题
.of(上下文)
.textTheme.caption.color,
按下:()=>\u页面控制器。跳转页面(3),
),
图标按钮(
图标:图标(
一个人,
尺寸:24.0,
),
颜色:_page==4
主题。背景。强调颜色
:主题
.of(上下文)
.textTheme.caption.color,
按下:()=>\u页面控制器。跳转页面(4),
),
尺寸箱(宽度:7),
],
),
颜色:主题。背景。原色,
形状:CircularNotchedRectangle(),
),
浮动ActionButtonImator:FloatingActionButtonImator.scaling,
floatingActionButtonLocation:floatingActionButtonLocation.centerDocked,
浮动操作按钮:浮动操作按钮(
标高:4.0,
子:图标(
Icons.search,
),
按下:()=>\u页面控制器。跳转页面(2),
),
),
);
伊康巴奇
class IconBadge extends StatefulWidget {
final IconData icon;
final double size;
static int counteer;
IconBadge({Key key, @required this.icon, @required this.size})
: super(key: key);
@override
_IconBadgeState createState() => _IconBadgeState();
}
class _IconBadgeState extends State<IconBadge> {
//List _users;
static int count ;
Future countForBadge() async{
var db = new DatabaseHelper();
count = await db.getCount();
print("Count: $count");
//print("khAN NNNN $counteer");
}
@override
void initState() {
// TODO: implement initState
super.initState();
countForBadge();
}
@override
Widget build(BuildContext context) {
if (count == null){
count = 0;
}
print("Count lande: $count");
return Stack(
children: <Widget>[
Icon(
widget.icon,
size: widget.size,
),
Positioned(
right: 0.0,
child: Container(
padding: EdgeInsets.all(1),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(6),
),
constraints: BoxConstraints(
minWidth: 13,
minHeight: 13,
),
child: Padding(
padding: EdgeInsets.only(top: 1),
child:Text(
"$count",
style: TextStyle(
color: Colors.white,
fontSize: 8,
),
textAlign: TextAlign.center,
),
),
),
),
],
);
}
}
类IconBadge扩展StatefulWidget{
最终的Iconda图标;
最终双倍尺寸;
静态整数计数器;
IconBadge({Key Key,@required this.icon,@required this.size})
:super(key:key);
@凌驾
_IconBadgeState createState()=>_IconBadgeState();
}
类_IconBadgeState扩展状态{
//列出用户;
静态整数计数;
Future countForBadge()异步{
var db=new DatabaseHelper();
count=await db.getCount();
打印(“计数:$Count”);
//打印(“khAN NNNN$counteer”);
}
@凌驾
void initState(){
//TODO:实现initState
super.initState();
countForBadge();
}
@凌驾
小部件构建(构建上下文){
如果(计数=null){
计数=0;
}
打印(“Count lande:$Count”);
返回堆栈(
儿童:[
图标(
widget.icon,
大小:widget.size,
),
定位(
右:0.0,
子:容器(
填充:边缘设置。全部(1),
装饰:盒子装饰(
颜色:颜色,红色,
边界半径:边界半径。圆形(6),
),
约束:BoxConstraints(
最小宽度:13,
身高:13,
),
孩子:填充(
填充:仅限边缘设置(顶部:1),
子:文本(
“$count”,
样式:TextStyle(
颜色:颜色,白色,
字体大小:8,
),
textAlign:textAlign.center,
),
),
),
),
],
);
}
}
我用一个简单的容器()替换了一些屏幕。
如果我理解您的问题,这是使用VoidCallBack
的解决方案:
主飞镖
import'包装:徽章/徽章.dart';
进口“包装:颤振/材料.省道”;
导入“包装:发布\柜台\购物车/购物车\屏幕.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
国际网页;
int _计数器=0;
页面控制器_页面控制器;
@凌驾
无效初始值