我的 CircularProgressIndicator 重建了 2-3 次,即使状态没有多次发出,它也会使旋转动画每次重新启动。
我的 BlocBuilder:
BlocBuilder<DayDetailedCubit, DayDetailedState>(
builder: (context, state) {
if (state is Loading) {
print("loading");
// return LoadingIndicator();
return CircularProgressIndicator();
} else if (state is Loaded) {
return Expanded(
child: ListView(
children: [
SizedBox(
height: CustomPaddings.tabListSpacing * 0.6,
),
MoodSection(moodEntries: state.dayDetailed.moodEntries),
StressSection(stressEntries: state.dayDetailed.stressEntries,),
SectionAchievement(achievementEntries: state.dayDetailed.achievementEntries,),
SizedBox(
height: CustomPaddings.tabListSpacing,
),
],
),
);
} else if (state is Initial) {
return Text("initial");
} else if (state is Error) {
return Text("error");
}
return Text("unknown");
},
),
我正在从我的 Cubit 发出 Loading 状态:
Future<void> getDayDetailed(Timestamp timestamp) async {
emit(Loading());
print("cubit: get day detailed");
final failureOrDayDetailed = await getDayDetailedEntries(timestamp);
failureOrDayDetailed.fold(
(l) {
print("error");
emit(Error());
}, (dayDetailed) => emit(Loaded(dayDetailed)));
}
最后 getDayDetailed 仅从这里的初始化状态调用:
@override
void initState() {
super.initState();
BlocProvider.of<DayDetailedCubit>(context).getDayDetailed(widget.timestamp);
}
据我所知,即使多次发出状态,如果内容没有不同,Bloc 也不会重建,所以我不确定为什么我的 CircularProgressIndicator 会重建几次。