buildWhen在block中的用法



我正在努力理解集团语句解决方案。但我不明白buildWhen的真正用途是什么。当值为真时,它将返回if条件的第一条语句。那么buildWhen是用来干什么的呢?

示例如下:

BlocBuilder<OrdersBloc, OrdersState>(
buildWhen: (context, state) {
return state is OrdersState.OrderCompleted
},
builder: (context, state) {
if (state is OrdersState.OrderCompleted) {
return Container(child: Text('Order Completed!'));
} else if (OrdersState.OrderInProgress) {
return Container(child: Text('In Progress'));
} else if (OrdersState.OrderRequested) {
return Container(child: Text('A customer placed an order!'));
} else {
return Container(child: Text('Waiting for an order'));
}
},
);

对于何时调用构建器函数的细粒度控制,可以提供可选的buildWhen。buildWhen获取前一个块状态和当前块状态并返回一个布尔值。如果buildWhen返回true,则将调用builder,并且将重建小部件。如果buildWhen返回false,则不会调用带有state的builder,也不会进行重建。

BlocBuilder<BlocA, BlocAState>(
buildWhen: (previousState, state) {
// return true/false to determine whether or not
// to rebuild the widget with state
},
builder: (context, state) {
// return widget here based on BlocA's state
}
)

点击链接

了解更多信息

最新更新