nick.tdr
nick.tdr

Reputation: 4933

DraggableScrollableSheet doesn't give the current position of the sheet when dragging

In flutter we have a widget DraggableScrollableSheet. Now I want the current position or the current size of the child while it is dragging. There is currently no way to get that value. It gives a ScrollController in its builder method but that is for when the list is scrolling and not when its being dragged. So is there another way by which I can track the current drag position of the list?

I tried adding NotificationListener but that only gives me dragstartnofification and dragendnotification and nothing for 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]);
                    }),
              ),
            );
          }),

Upvotes: 7

Views: 7846

Answers (1)

nick.tdr
nick.tdr

Reputation: 4933

I found the solution to this. Flutter team updated the ScrollableDraggableSheet class in the Flutter v1.7.3. They added a class called DraggableScrollableNotification which you can listen to to get the current extend of the child when dragging.

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]);
                  }),
            );
          }),
    )

in the above sample notification.extent will give you the current extent(position) of the child when dragging.

Note: this is currently in the master channel and not in the stable channel. Switch to the master channel by running flutter channel master and then flutter upgrade to make it work for now.

Upvotes: 33

Related Questions