3

I'm working with DraggableScrollableSheet in flutter and I want to know whether the sheet is collapsed or expanded. The builder of the DraggableScrollableSheet provide a scrollController to whom I attach a listener.

I see this helpful guide but they create a ScrollController, but in my case the scrollController is provided by the constructor. I don't understand how could I obtain their same behavior

Here's my code:

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizedBox.expand(
        child: myBottomSheet()
      ),
    );
  }
}

class myBottomSheet extends StatefulWidget{
  @override
  _myBottomSheetState createState() => _myBottomSheetState();
}

class _myBottomSheetState extends State<myBottomSheet> {
  double appbarSize=0.08;
  @override
  Widget build(BuildContext context) {
    return DraggableScrollableSheet(
      maxChildSize: 0.8,
      minChildSize: appbarSize,
      initialChildSize: appbarSize,
      builder: (BuildContext context, ScrollController scrollController) {
        scrollController.addListener(_scrollListener); // <----------------
        return Container(
          child: ListView.builder(
            controller: scrollController,
            itemCount: 25,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(title: Text('Item $index'));
            },
          ),
        );
      },
    );
  }

  _scrollListener(){
    //use controller such in the guide
  }

1 Answer 1

8

You just need to restructure the code. Define your _scrollListener method in the builder.

See below:

class _myBottomSheetState extends State<myBottomSheet> {
  double appbarSize=0.08;
  @override
  Widget build(BuildContext context) {
    return DraggableScrollableSheet(
      maxChildSize: 0.8,
      minChildSize: appbarSize,
      initialChildSize: appbarSize,
      builder: (BuildContext context, ScrollController scrollController) {
        _scrollListener(){
         //use controller such in the guide
        }

        scrollController.addListener(_scrollListener); // <----------------
        return Container(
          child: ListView.builder(
            controller: scrollController,
            itemCount: 25,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(title: Text('Item $index'));
            },
          ),
        );
      },
    );
  }
1
  • This looks very wrong: (1) you should only add a listener once, in initState (you are adding a listener on every call to build! You may end up adding hundreds of listeners for the same controller!); (2) you need to dispose of the listener in dispose. Aug 5 at 7:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.