滚动时不维护状态的页面
我创建了此多个页面,其中包含不同的小部件,例如TextFormField,下拉曲,复选框等。我创建了一个小部件,该小部件根据问题类型从API中选择页面。 此问题选择器小部件在ListView.builder Inside。我正在使用CUBIT来管理状态。每个页面具有不同的相应CUBIT。 问题是每次滚动时,小部件状态丢失。
我的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 ?? [],
);
});
我的小部件选择器
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}');
}
}
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}');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
改变肘状态的最佳场所是使用肘上的方法,然后可以发出状态变化。如果您的州听众正确设置,他们将更新并维护此新发射的数据。请检查您是否正确更新了您的Cubit状态并发出该状态的变化。然后检查您是否正在正确聆听这些状态的更改并根据需要重建小部件。
The best place to change the state of your cubits is with a method on the cubit, which can then emit the state change. If your state listeners are setup properly, they will update and maintain this newly emitted data. Please check that you are properly updating your cubit state and emitting that state change. Then check that you are properly listening for those state changes and rebuilding your widgets as necessary.