I created this multiple pages which has different widgets inside it like TextFormField, DropDown, checkBox etc. I created a widget that selects pages on basis of question type fetch from api.
This question selector widget is called inside ListView.builder. I am using cubit to manage state.Each page has it different corresponding cubit.
The problem is each time scroll the widget state is lost.

My ListView.builder
ListView.builder(
itemCount: state.question[0].groups![0].items!.length,
itemBuilder: (BuildContext context, int index) {
return fetchQuestionWidget(
question: state.question[0].groups![0].items![index],
hintText: "",
imageList: imageList ?? [],
);
});
My widget selector
Widget fetchQuestionWidget(
{JsonItems? question,
String? collectionId,
String? collectionCode,
String? hintText,
String? optionCode,
List<Options>? options,
bool? isParentWidget,
List<File>? imageList,
PageStorageKey? pageStorageKey}) {
if (question?.type == QuestionTypeConstant.kText) {
return TextFieldQuestionWidget(
question: question!,
hintText: hintText!,
);
} else if (question?.type == QuestionTypeConstant.kDate) {
return DatePickerQuestionWidget(
question: question!,
);
} else if (question?.type == QuestionTypeConstant.kSelection) {
if (question?.renderType == QuestionTypeConstant.kDropdown &&
!question!.multiple!) {
return DropDownQuestionWidget(
question: question,
options: options,
key: pageStorageKey,
);
} else if ((question?.renderType == QuestionTypeConstant.kDropdown &&
question!.multiple!) ||
question?.renderType == QuestionTypeConstant.kTable) {
return MultiSelectDropDownQuestionWidget(
question: question!,
);
} else if (question?.renderType == QuestionTypeConstant.kSelectionButton &&
question?.code != 'active') {
return SelectionButtonQuestionWidget(
question: question!,
);
} else if (question?.renderType == QuestionTypeConstant.kCheckBox) {
return QtCheckBox(
question: question!,
);
} else {
return Text('#${question?.type}');
}
} else if (question?.type == QuestionTypeConstant.kNumber) {
return NumberTextFieldQuestionWidget(
question: question!,
);
} else if (question?.type == QuestionTypeConstant.kHeading) {
return HeadingQuestionWidget(question: question!);
} else if (question?.type == QuestionTypeConstant.kPhoto) {
return PhotosQuestionWidget(
question: question!,
imageList: imageList,
);
} else {
return Text('#${question?.type}');
}
}