Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

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]);
                    }),
              ),
            );
          }),
like image 763
nick.tdr Avatar asked Jun 08 '19 12:06

nick.tdr


1 Answers

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.

like image 176
nick.tdr Avatar answered Jan 03 '23 16:01

nick.tdr