ListView通过集团模式问题更新

发布于 2025-01-26 01:03:36 字数 2891 浏览 3 评论 0原文

我正在学习集体模式,总的来说,我的代码正在工作。但是我的问题是,BLOC构建器似乎并未正确重建listView,因为每次从集团就ListView数据进行更新时,滚动列表的位置就会丢失,因此列表最终到达最高位置。

据我所知,需要重建小部件的各个方面,而不是由集团和扑朔迷离来管理。这也将不应保留的滚动位置。还是我在这里错了?

 class SubPageTasksOverview extends StatelessWidget {
  const SubPageTasksOverview({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final themeData = Theme.of(context);

    return BlocListener<TaskBloc, TaskState>(
      listener: (context, state) {
        if (state is MyTaskInitializedState) {
          BlocProvider.of<TaskBloc>(context).add(MyTaskStartObservationEvent());
        }
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.max,
        children: [
          const Divider(
            thickness: 3,
            indent: 0,
            endIndent: 0,
            height: 20,
          ),
          ListTileElementAddNewTask(),
          Expanded(
            child: Container(
              padding: const EdgeInsets.all(10),
              child:
                  BlocBuilder<TaskBloc, TaskState>(builder: (context, state) {
                if (state is MyTaskReceivedState) {
                  //define the order by priority
                  state.tasks
                      .sort(((a, b) => a.priority.compareTo(b.priority)));
                  return ReorderableListView.builder(
                      scrollDirection: Axis.vertical,
                      itemCount: state.tasks.length,
                      itemBuilder: (BuildContext ctx, index) {
                        return ListTileElement(
                            key: Key(state.tasks[index].id.toString()),
                            myTask: state.tasks[index]);
                      },
                      onReorder: (oldIndex, newIndex) {
                        if (newIndex > oldIndex) {
                          newIndex = newIndex - 1;
                        }

                        //Add the project on the new Position

                        var projectToMove = state.tasks.removeAt(oldIndex);
                        state.tasks.insert(newIndex, projectToMove);

                        var index = 0;
                        state.tasks.forEach((element) {
                          element.priority = index++;
                        });

                        BlocProvider.of<TaskBloc>(context)
                            .add(MyTaskCreateEvent(myTaskList: state.tasks));
                      });
                } else {
                  return Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [CircularProgressIndicator()]);
                }
              }),
            ),
          ),
        ],
      ),
    );
  }
}

I am learning the Bloc patter and in general my code is working. But my problem is that the BLOC builder doesn't seem to rebuild the ListView properly, as every time it is updated from the BLOC with respect to the ListView data, the position of the scrolled list is lost and thus the list ends up at the top position.

As far as I know the aspects which widgets need to be rebuilt and which not are managed by BLOC and Flutter. This also incudes the scrolling position that should be retained. Or am I wrong here?

 class SubPageTasksOverview extends StatelessWidget {
  const SubPageTasksOverview({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final themeData = Theme.of(context);

    return BlocListener<TaskBloc, TaskState>(
      listener: (context, state) {
        if (state is MyTaskInitializedState) {
          BlocProvider.of<TaskBloc>(context).add(MyTaskStartObservationEvent());
        }
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.max,
        children: [
          const Divider(
            thickness: 3,
            indent: 0,
            endIndent: 0,
            height: 20,
          ),
          ListTileElementAddNewTask(),
          Expanded(
            child: Container(
              padding: const EdgeInsets.all(10),
              child:
                  BlocBuilder<TaskBloc, TaskState>(builder: (context, state) {
                if (state is MyTaskReceivedState) {
                  //define the order by priority
                  state.tasks
                      .sort(((a, b) => a.priority.compareTo(b.priority)));
                  return ReorderableListView.builder(
                      scrollDirection: Axis.vertical,
                      itemCount: state.tasks.length,
                      itemBuilder: (BuildContext ctx, index) {
                        return ListTileElement(
                            key: Key(state.tasks[index].id.toString()),
                            myTask: state.tasks[index]);
                      },
                      onReorder: (oldIndex, newIndex) {
                        if (newIndex > oldIndex) {
                          newIndex = newIndex - 1;
                        }

                        //Add the project on the new Position

                        var projectToMove = state.tasks.removeAt(oldIndex);
                        state.tasks.insert(newIndex, projectToMove);

                        var index = 0;
                        state.tasks.forEach((element) {
                          element.priority = index++;
                        });

                        BlocProvider.of<TaskBloc>(context)
                            .add(MyTaskCreateEvent(myTaskList: state.tasks));
                      });
                } else {
                  return Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [CircularProgressIndicator()]);
                }
              }),
            ),
          ),
        ],
      ),
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

微暖i 2025-02-02 01:03:36

我找到了这个问题。

由于该集团发出其他状态,例如“加载”(每个事件之后),圆形progressIndicator()小部件在很短的时间内构建:

else {return Column(mainAxisAlignment: MainAxisAlignment.center,
                      children: [CircularProgressIndicator()]);}

加载成功后,触发了更新的状态“ myTaskReceivedstate”(带有新的更新数据)。

解决方案是:仅在ListView到达新数据时构建ListView,并避免在此组合中使用CignularProgressIndicator()或其他状态和小部件。

buildWhen: (previous, current) {
                return current is MyTaskReceivedState;

I found the issue.

Since the BLOC emits other states like "Loading" (after each event) the CircularProgressIndicator() widget is build for a very short period:

else {return Column(mainAxisAlignment: MainAxisAlignment.center,
                      children: [CircularProgressIndicator()]);}

After the loading was successful the updated state "MyTaskReceivedState" was triggered (with new updated data).

Solution is: only to build the ListView when new data for the ListView arrive and to avoid using the CircularProgressIndicator() or other States and Widgets in this combination.

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