自己做购物车的时候,需要每个item往上一层传数据。于是想到了flutter中notification。是用Notification主要是俩个步奏。一是自定义Notification,而是添加Notification的监听就可以了
例如:
自定义Notification
class CartOperationNotification extends Notification{
CartOperationNotification(this.goodsPrice,this.id,this.state);
final double goodsPrice;
final int id;
final int state;
}
在它子节点出添加通知监听
NotificationListener(
onNotification: (notification){
double goodsPrice = notification.goodsPrice;
var id = notification.id;
int state = notification.state;
print("获取到的价格是:${goodsPrice}");
_oprateCartNum(state,goodsPrice);
_update();
return false;
},
child: CartItemView()
),
NotificationListener(
onNotification: (notification){
double goodsPrice = notification.goodsPrice;
var id = notification.id;
int state = notification.state;
print("获取到的价格是:${goodsPrice}");
_oprateCartNum(state,goodsPrice);
_update();
return false;
},
子节点触发向上通知(在CartItemView()中的一部分代码)
Container(
alignment: Alignment.center,
child: RoundCheckBox(
value: _selectState,
onChanged: (checkState){
_selectState=checkState;
if(_selectState){
//widget.onChanged(1);
CartOperationNotification(25.0,1,1).dispatch(context);
}else{
//widget.onChanged(2);
CartOperationNotification(25.0,1,2).dispatch(context);
}
_update();
print("是否选中:${checkState}");
},
)
),