1

I'm trying to use a DraggableScrollableSheet inside a BlocBuilder so that when I navigate to a page I can do something like this:

cubit.draggableScrollableController.animateTo(
    .5, // or 1.0
    duration: Duration(seconds: 10),
    curve: Curves.easeInOut);

and it will hopefully scroll nicely to the correct height for whatever particular page the user is on.

Anyway, I was trying to test this out so I put in the .animateTo function directly after it was attached to the DraggableScrollabelSheet but I get an error stating:

_AssertionError ('package:flutter/src/widgets/draggable_scrollable_sheet.dart': Failed assertion: line 170 pos 7: 'isAttached': DraggableScrollableController is not attached to a sheet. A DraggableScrollableController must be used in a DraggableScrollableSheet before any of its methods are called.)

return BlocBuilder<FrontPageContainerHeightCubit,
            FrontPageContainerHeightCubitState>(
        builder: (context, state) => Container(
            alignment: Alignment.bottomCenter,
            child: DraggableScrollableSheet(
                initialChildSize: services.screen.frontPageContainer
                    .asPercentage(state.frontPageContainerHeight),
                minChildSize: services.screen.frontPageContainer
                    .asPercentage(state.frontPageContainerHeight),
                maxChildSize: min(
                    1.0,
                    max(
                        services.screen.frontPageContainer
                            .asPercentage(state.frontPageContainerHeight),
                        services.screen.frontPageContainer.maxPercentage)),
                controller: cubit.draggableScrollableController, // <--- attached
                builder: (context, scrollController) {
                  cubit.setScrollableController(scrollController); 
                  cubit.draggableScrollableController.animateTo( // <--- test
                      services.screen.frontPageContainer.asPercentage(
                          services.screen.frontPageContainer.midHeight),
                      duration: Duration(seconds: 10),
                      curve: Curves.easeInOut);
                  return Stack(
                    alignment: Alignment.bottomCenter,
                    children: [const FrontDrop(), utilities.beamer.front()],
                  );
                }))

Now, I think this is failing simply because its happing inside the same build function. but it made me question the entire attempt. Is this the right way to achieve my goal?

MetaStack
  • 3,266
  • 4
  • 30
  • 67

0 Answers0