Is there a way to close a modal bottom sheet in flutter that contains a listview?

Viewed 1088

So basically I've been trying for a couple of days to allow users to close a modal bottom sheet when they get to the top of the ListView, when swiping on the ListView. However, when they swipe on the list view the widgets register as if I'm just trying to scroll up on the ListView. Is there a physics type for the ListView to close a modal bottom sheet when at the top or a different way to set up an ignore pointer for this? This is what I've come up with and I hope I explained myself well enough that this problem is understood.

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class ModalSheet extends StatefulWidget {
  @override
  _ModalSheetState createState() => _ModalSheetState();
}

ScrollController _scrollController = ScrollController();
bool close = false;

class _ModalSheetState extends State<ModalSheet> {
  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels < 1) {
        if (_scrollController.position.userScrollDirection ==
            ScrollDirection.forward) {
          setState(() {
            close = true;
          });
        } else {
          setState(() {
            close = false;
          });
        }
      } else {
        setState(() {
          close = false;
        });
      }
      print(_scrollController.position.pixels);
      print(_scrollController.position.userScrollDirection);
      print(close);
      // print(close);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Flexible(
            child: IgnorePointer(
              ignoring: close,
              child: ListView.builder(
                controller: _scrollController,
                physics: BouncingScrollPhysics(),
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text('tile: ${index + 1}'),
                  );
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}

EDIT: Thanks to @LearningJS888 for the package suggestion below to use this package! much appreciated

1 Answers

Can you elaborate what exactly you're trying to do? A normal way of closing ModalBottomSheet is by Navigator.pop(context).

Related