'DraggableScrollableSheet collapsing in several cases

I have a DraggableScrollableSheet, something like

DraggableScrollableSheet(
          initialChildSize: 0,
          snap: true,
          snapSizes: _snapSizes,
          minChildSize: 0,
          maxChildSize: _expandSize,
          controller: _sheetController,
          builder: (context, controller) => Material(
            child: ListView(
              physics: const ClampingScrollPhysics(),
              controller: controller,
              children: [
                SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [ ....
],
            ),
          ),
        )

The problem is

  • when i focus a textfield in DraggableScrollableSheet and keyboard opens - DraggableScrollableSheet hide immediately
  • when i open another one above it - same

no errors in stacktrace seems to be a regular behaviour.. i search a lot about this topic but no solution

any ideas please?



Solution 1:[1]

So i was able to fix it. I copied the DraggableScrollableSheet and updated the _replaceExtent method as follows:

void _replaceExtent() {
    _extent.dispose();
    _extent = _extent.copyWith(
      minSize: widget.minChildSize,
      maxSize: widget.maxChildSize,
      snap: widget.snap,
      snapSizes: _impliedSnapSizes(),
      // This line was updated
      initialSize: _extent.currentSize,
      onSizeChanged: _setExtent,
    );
    // Modify the existing scroll controller instead of replacing it so that
    // developers listening to the controller do not have to rebuild their listeners.
    _scrollController.extent = _extent;
    if (widget.snap) {
      // Trigger a snap in case snap or snapSizes has changed. We put this in a
      // post frame callback so that `build` can update `_extent.availablePixels`
      // before this runs-we can't use the previous extent's available pixels as
      // it may have changed when the widget was updated.
      WidgetsBinding.instance!.addPostFrameCallback((Duration timeStamp) {
        _scrollController.position.goBallistic(0);
      });
    }
  }

But be cautios. This breaks some tests so it might cause new issues if you try this. For me it's fine though.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lennart Tönjes