Bloc version 8.0.1
In builder i display ListView.builder when data loaded, but i also need listener that show alert dialog, and to show it i change state, but after i change state when user click on button, blocbuilder see that state changed and change ListView.builder to other widget that says "error happend"
What can i do with it? Also i am new in bloc, may u give me detail answer about this problem.
this is my BlocConsumer
BlocConsumer<AddCheckListBloc, AddCheckListState>(
builder: (context, state) {
if (state is CheckListTemplateEmpty) {
return const Text("");
} else if (state is CheckListTemplateLoading) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (state is CheckListTemplateLoaded) {
return ListView.builder(
itemCount: state.loadedTemplates.length,
itemBuilder: (BuildContext context, int index) {
if (state.loadedTemplates[index].isPaid == "True") {
return InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(border: Border.all(color: const Color.fromARGB(255, 141, 166, 255), width: 1), borderRadius: BorderRadius.circular(10), color: Colors.grey),
width: 50,
margin: const EdgeInsets.only(left: 20, right: 20, top: 14),
height: 80,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
state.loadedTemplates[index].name,
style: const TextStyle(fontSize: 30),
),
Text(state.loadedTemplates[index].price.toString())
],
),
),
);
} else {
return InkWell(
onTap: () {
addCheckListBloc.add(ShowAlertDialogEvent(state.loadedTemplates[index]));
},
child: Container(
decoration: BoxDecoration(border: Border.all(color: const Color.fromARGB(255, 141, 166, 255), width: 1), borderRadius: BorderRadius.circular(10), color: Colors.white),
width: 50,
margin: const EdgeInsets.only(left: 20, right: 20, top: 14),
height: 80,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
state.loadedTemplates[index].name,
style: const TextStyle(fontSize: 30),
)
],
),
),
);
}
});
} else {
return const Center(
child: Text("Ошибка, информацию недоступна к загрузке с сервера"),
);
}
},
listener: (context, state) {
{
if (state is OpenAlertDialogState) {
SchedulerBinding.instance!.addPostFrameCallback((_) {
showDialog(
context: context,
builder: (BuildContext context) {
String description = "";
return AlertDialog(
title: const Text("Добавить чеклист?"),
content: Container(
decoration: BoxDecoration(border: Border.all(color: const Color.fromARGB(255, 141, 166, 255), width: 1), borderRadius: BorderRadius.circular(10), color: Colors.white),
child: TextField(
decoration: const InputDecoration.collapsed(hintText: "Описание", border: InputBorder.none),
maxLines: null,
onChanged: (String value) async {
description = value;
},
),
),
actions: [
TextButton(
onPressed: () {
// Сделать что-то, чтобы закрыть диалог вместе с блоком
},
child: const Text("Нет"),
),
TextButton(
onPressed: () {
addCheckListBloc.add(AddNewCheckList(state.checkListTemplate, description));
},
child: const Text("Да"),
)
],
);
});
});
}
if (state is CloseAlertDialogState) {
SchedulerBinding.instance!.addPostFrameCallback((timeStamp) {
Navigator.pop(context);
});
}
;
}
},
);
this is my Bloc
class AddCheckListBloc extends Bloc<AddCheckListEvent, AddCheckListState> {
final CheckListRepository _checkListRepository;
AddCheckListBloc({required CheckListRepository checkListRepository})
: _checkListRepository = checkListRepository,
super(CheckListTemplateLoading()) {
on<OnCheckListTemplateLoad>((event, Emitter<AddCheckListState> emit) => _loadCheckListTemplates(event, emit));
on<AddNewCheckList>((event, emit) => _addNewCheckList(event, emit));
on<ShowAlertDialogEvent>((event, emit) => _showAlertDialog(event, emit));
}
Future<void> _loadCheckListTemplates(OnCheckListTemplateLoad event, Emitter emit) async {
emit(CheckListTemplateLoading());
try {
final List<CheckListTemplate> loadedTemplates = await _checkListRepository.getCheckListTemplateList();
emit(CheckListTemplateLoaded(loadedTemplates: loadedTemplates));
} catch (e) {
print(e.toString());
emit(CheckListTemplateError());
}
}
Future<void> _addNewCheckList(AddNewCheckList event, Emitter<AddCheckListState> emit) async {
emit(AddingNewCheckList());
try {
final template = event.checkListTemplate;
List<CheckListPoint> _checkListPointList = [];
for (int i = 0; i < template.checkListPointList!.length; i++) {
CheckListPoint _checkListPoint = CheckListPoint(false, false, template.checkListPointList![i].requirement, template.checkListPointList![i].notes, false, template.checkListPointList![i].requirementDetails);
_checkListPointList.add(_checkListPoint);
}
CheckList _checkList = CheckList(template.name, event.description, template.advice, _checkListPointList, 0, template.checkListPointList!.length, 0, 0);
_checkListRepository.saveCheckList(_checkList);
emit(NewCheckListAdded());
} catch (e) {
emit(AddNewCheckListError());
}
}
Future<void> _showAlertDialog(ShowAlertDialogEvent event, Emitter emit) async {
emit(OpenAlertDialogState(event.checkListTemplate));
}
}