【发布时间】:2021-07-21 04:41:15
【问题描述】:
我有这个应用程序,它有一个 sliverlist 和一个 sliverAppbar。我需要获取 sliverlist onscroll 中每个项目的当前滚动位置,并确定它是否已经越过 sliverAppbar 并使用项目的标题更新 sliverappbar。说从 Item1 开始,一旦它穿过 SliverAppBar,这意味着我正在查看 Item1 内容,当 Item2 穿过 SliverAppBar 时,使用标题 Item2 更新 SliverAppBar 以表示用户正在查看 Item2 内容
我正在尝试使用 NotificationListener<ScrollEndNotification> 来实现这一点,但我被困在第二个 NotificationListener 上,它应该向父级顶部发出通知
在这一行 ScrollEndNotification(metrics: , context:context).dispatch(context); 它会抛出一个错误,我应该提供一个 metrics 参数,我不知道该提供什么。
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
ScrollEndNotification(metrics: , context:context).dispatch(context);
return AutoScrollTag(
key: ValueKey(index),
controller: controller,
index: index,
child: Padding(
padding: const EdgeInsets.only(
top: 30.0, left: 20.0, right: 20.0),
child: Container(
color: Colors.red,
//height: 120.0
//height: A varying height
),),},); ),
完整的代码是
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[100],
body: NotificationListener<ScrollEndNotification>(
onNotification: (notification) {
if (notification is ScrollEndNotification) {
///Here I need to know what widget index bubbled the notification, its position
///on the screen and its index
//in the list, in order to do further implementation like update the
//SliverAppBar
print('$notification');
return true;
},
child: CustomScrollView(
controller: controller
slivers: <Widget>[
SliverAppBar(
title: Text(title),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
ScrollEndNotification(metrics: , context:context).dispatch(context);
return AutoScrollTag(
key: ValueKey(index),
controller: controller,
index: index,
child: Padding(
padding: const EdgeInsets.only(
top: 30.0, left: 20.0, right: 20.0),
child: Container(
color: Colors.red,
//height: 120.0
//height: A varying height
),),},); ),
此外,如果您对如何实现这一目标有更好的实施,请帮助我。简而言之,我需要跟踪某个项目何时从屏幕上滚出,并在 Sliverlist 中找到它的 index。请记住,item 有一个可变大小的容器,可根据其中的子项数量进行扩展。这是电子商务应用程序中常见的 UX 模式。例如,在用户向下滚动时查看菜单,并在标题穿过屏幕时更新用户正在查看的菜单。
为要点提供link,以便您了解完整的实现
【问题讨论】:
-
跟踪每个孩子的全球位置怎么样? How to track the top Item in the viewport for a list flutter
标签: flutter