滚动时不维护状态的页面

发布于 2025-01-24 23:22:08 字数 2642 浏览 0 评论 0原文

我创建了此多个页面,其中包含不同的小部件,例如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.
This is my ui

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2025-01-31 23:22:08

改变肘状态的最佳场所是使用肘上的方法,然后可以发出状态变化。如果您的州听众正确设置,他们将更新并维护此新发射的数据。请检查您是否正确更新了您的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文