DraggableScrollableSheet 在拖动时不给出工作表的当前位置

nic*_*tdr 5 flutter

在 flutter 中,我们有一个小部件DraggableScrollableSheet。现在我想要孩子在拖动时的当前位置或当前大小。目前无法获得该值。它在其 builder 方法中提供了一个 ScrollController ,但这是在列表滚动时而不是在拖动时使用的。那么还有另一种方法可以跟踪列表的当前拖动位置吗?

我尝试添加 NotificationListener ,但这只给了我 dragstartnofification 和 dragendnotification 而没有给 dragupdate :

DraggableScrollableSheet(
          initialChildSize: .70,
          minChildSize: .70,
          builder:
              (BuildContext context, ScrollController scrollcontroller) {
            return NotificationListener(
              onNotification: (notification) {
                print("$notification");
              },
              child: Container(
                child: ListView.builder(
                    controller: scrollcontroller,
                    itemCount: widget.songs.length,
                    itemBuilder: (BuildContext context, int index) {
                      return buildSongRow(widget.songs[index]);
                    }),
              ),
            );
          }),
Run Code Online (Sandbox Code Playgroud)

nic*_*tdr 24

我找到了解决方案。Flutter 团队在 Flutter v1.7.3 中更新了 ScrollableDraggableSheet 类。他们添加了一个名为 DraggableScrollableNotification 的类,您可以在拖动时收听该类以获取子项的当前扩展。

NotificationListener<DraggableScrollableNotification>(
      onNotification: (notification) {

        print("${notification.extent}");
      },
      child: DraggableScrollableSheet(
          initialChildSize: .70,
          minChildSize: .70,
          builder:
              (BuildContext context, ScrollController scrollcontroller) {
            return Container(
              child: ListView.builder(
                  controller: scrollcontroller,
                  itemCount: widget.songs.length,
                  itemBuilder: (BuildContext context, int index) {
                    return buildSongRow(widget.songs[index]);
                  }),
            );
          }),
    )
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,notification.extent将在拖动时为您提供孩子的当前范围(位置)。

注意:这是目前在主通道中,而不是在稳定通道中。通过运行 flutter channel master切换到master 通道 ,然后flutter upgrade使其现在工作。