ListView通过集团模式问题更新
我正在学习集体模式,总的来说,我的代码正在工作。但是我的问题是,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了这个问题。
由于该集团发出其他状态,例如“加载”(每个事件之后),圆形progressIndicator()小部件在很短的时间内构建:
加载成功后,触发了更新的状态“ myTaskReceivedstate”(带有新的更新数据)。
解决方案是:仅在ListView到达新数据时构建ListView,并避免在此组合中使用CignularProgressIndicator()或其他状态和小部件。
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:
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.