flutter icon indicating copy to clipboard operation
flutter copied to clipboard

Can't swipe to dismiss scrollable Bottom Sheet

Open cpboyd opened this issue 7 years ago • 47 comments

Per @dnfield https://github.com/flutter/flutter/issues/31739#issuecomment-511459403

Use case

See the Crane Material sample and Google I/O 2019

These both feature a bottom sheet that slides up (in the case of I/O, you select "Events" and the hit the "Filter" FAB) and scrolls.

With the I/O app, if you continue to swipe down when scrolled to the top, it will start to drag and then dismiss when a threshold is reached.

If the threshold is not reached (or you swipe upwards) then the bottom sheet will not be dismissed and it'll rebound to the top of the screen.

Proposal

I'd think this should be a part of DraggableScrollableSheet as mentioned in #31739, but maybe it makes sense to bake the logic into the Bottom Sheet itself?

I've tried both modal and non-modal bottom sheets with and without the DraggableScrollableSheet, and can't seem to make this work as is.

The current issue with the DraggableScrollableSheet is that you can't have the sheet be maximized all the time. (Since, as I mentioned in https://github.com/flutter/flutter/issues/31739#issuecomment-511241746, any swipe will immediately dismiss the BottomSheet rather than scroll the CustomScrollView)

cpboyd avatar Jul 16 '19 17:07 cpboyd

It would be super helpful if you had a gif/youtube/video of this behavior. I think I know what you're talking about, but I haven't used the apps you're referring to and I'm not sure which part to look for.

dnfield avatar Jul 16 '19 17:07 dnfield

@dnfield See https://www.youtube.com/watch?v=95MUkecOta4

I wasn't able to get it to "rebound" while recording, but I'm pretty sure I've seen it bounce back to the top if a certain threshold isn't met.

Edit: First dismiss is dragging from the app bar. Second dismiss is from dragging the list after reaching the top (similar behavior to DraggableScrollableSheet). I had to release (after reaching the top of the list) and then drag down again.

cpboyd avatar Jul 16 '19 19:07 cpboyd

I thought the bottom sheet was working this way - the first time you hit the top of the list it will bounce, the second time it will actually drag down. I feel like I'm missing something in the desired behavior though.

dnfield avatar Jul 16 '19 22:07 dnfield

@dnfield I've tried both

        _scaffoldKey.currentState.showBottomSheet(
          _buildBottomSheet,
        );

and

        showModalBottomSheet(
          context: context,
          builder: _buildBottomSheet,
          isScrollControlled: true,
        );

where _buildBottomSheet is a Widget Function(BuildContext) that contains a CustomScrollView.

Nothing seems to do the drag-to-dismiss except when wrapped in a DraggableScrollableSheet.

Do I need to pass in a special ScrollController?

cpboyd avatar Jul 17 '19 04:07 cpboyd

You have to use a DraggableScrollableSheet for that behavior to work.

dnfield avatar Jul 17 '19 04:07 dnfield

@dnfield Ok. So how do I get the DraggableScrollableSheet to rebound back to maxChildSize unless the minChildSize threshold is passed?

I'm currently using this:

DraggableScrollableSheet(
      builder: scroll,
      initialChildSize: 1.0,
      minChildSize: 0.9,
    );

Unfortunately, the user can wind up with it sticking between 1.0 and 0.9, which looks a little awkward.

cpboyd avatar Jul 17 '19 04:07 cpboyd

Unfortunately this isn't really documented well at all, and in trying to do it I'm finding some other bugs.

I think this should be possible, but it may take me a little while to get back to it. If you have a proposal for fixing it we could review a PR for it as well.

dnfield avatar Jul 17 '19 16:07 dnfield

Are there any updates on this?

rbluethl avatar Feb 07 '20 12:02 rbluethl

Hello @rbluethl, I built a package that supports scrollable ModalBottomSheets. https://github.com/jamesblasco/modal_bottom_sheet

Instead of showModalBottomSheet, it would be:

showMaterialModalBottomSheet(
  context: context,
  builder: (context, scrollController) => Container(),
})

Hope it can help you until this is officially implemented.

jamesblasco avatar Apr 15 '20 15:04 jamesblasco

Any update on this? showModalBottomSheet not able to dismiss when swipe down.

ksankumar avatar May 20 '20 17:05 ksankumar

Any news on this?

zeletrik avatar Oct 20 '20 16:10 zeletrik

Hello @rbluethl, I built a package that supports scrollable ModalBottomSheets. https://github.com/jamesblasco/modal_bottom_sheet

Instead of showModalBottomSheet, it would be:

showMaterialModalBottomSheet(
  context: context,
  builder: (context, scrollController) => Container(),
})

Hope it can help you until this is officially implemented.

I tried using this plugin, happen to fix the issue of dismiss on scroll; but it goes over the app bar when the scrollable body is too long like I found here

prayatna avatar Apr 08 '21 06:04 prayatna

Hey, @pranayairan your issue is a bit off-topic. For issues related to the package use https://github.com/jamesblasco/modal_bottom_sheet/issues/add

I will be happy to help you there solve this 🙂

jamesblasco avatar Apr 08 '21 09:04 jamesblasco

officially update on this ?

lbFlyBoy avatar May 21 '21 06:05 lbFlyBoy

Hello @rbluethl, I built a package that supports scrollable ModalBottomSheets. https://github.com/jamesblasco/modal_bottom_sheet

Instead of showModalBottomSheet, it would be:

showMaterialModalBottomSheet(
  context: context,
  builder: (context, scrollController) => Container(),
})

Hope it can help you until this is officially implemented.

Unfortunately, the package has some bugs and looks like it's not maintained anymore. The official fix is needed.

eli1stark avatar Dec 08 '21 16:12 eli1stark

I've managed to do it with the following :

showModalBottomSheet(
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(16),
  ),
  isScrollControlled: true,
  isDismissible: true,
  context: context,
  builder: (context){
    return  DraggableScrollableSheet(
        expand: false,
        initialChildSize: 1,
        // minChildSize: 1,
        snap: true,
        builder: (context, scrollController) {
          return SingleChildScrollView(
            controller: scrollController,
            children:[],
          );
        },
    );
  },
);

jeffaknine avatar Dec 27 '21 17:12 jeffaknine

I've managed to do it with the following :

showModalBottomSheet(
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(16),
  ),
  isScrollControlled: true,
  isDismissible: true,
  context: context,
  builder: (context){
    return  DraggableScrollableSheet(
        expand: false,
        initialChildSize: 1,
        // minChildSize: 1,
        snap: true,
        builder: (context, scrollController) {
          return SingleChildScrollView(
            controller: scrollController,
            children:[],
          );
        },
    );
  },
);

Unfortunately, unless there's a workaround, this removes the nice behaviour modalBottomSheet has of making the sheet the same height as its contents.

joshminton avatar Jan 05 '22 13:01 joshminton

I think we want to drag and tap the background to dismiss the sheet. However, we can't do these features at the same sheet.

for example, if we want to drag the background to dismiss the sheet, we can't tap the background to dismiss the sheet...

please see this video and code first I clicked the background (red area) but I couldn't dismiss it. second, I dragged to dismiss it.

Code sample
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('title'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () => showModalBottomSheet(
            barrierColor: Colors.yellow.withOpacity(0.5),
            backgroundColor: Colors.red.withOpacity(0.5),
            useRootNavigator: true,
            context: context,
            isDismissible: true,
            isScrollControlled: true,
            builder: (context) => DraggableScrollableSheet(
              expand: true,
              builder: (_, controller) => Container(
                color: Colors.white,
                child: ListView.builder(
                  controller: controller,
                  itemBuilder: (_, index) => ListTile(
                    title: Text(index.toString()),
                  ),
                ),
              ),
            ),
          ),
          child: const Text('open modal'),
        ),
      ),
    );
  }
}

https://user-images.githubusercontent.com/52235899/165458758-358aba3b-6f13-48d3-af8f-b869b50ef2d8.mov

takassh avatar Apr 27 '22 06:04 takassh

Any update on this? showModalBottomSheet is not able to dismiss when swiping down.

that's a very bad thing that lots of people have an issue.

even having an issue with the status bar even though I used the safe area.

https://user-images.githubusercontent.com/14329287/180138571-d1d0db35-c05f-47c3-a05b-3640fb7969ec.mp4

vishalpatel1327 avatar Jul 21 '22 05:07 vishalpatel1327

This is still a thing. Would be nice to have it in place

nivisi avatar Feb 20 '23 16:02 nivisi