简体   繁体   中英

Flutter SliverPersistentHeader inside CustomScrollView reaches maxScrollExtend before reach to the bottom widget

screenshot

I'm trying to implement vertically scrollable stacked card listview. When i use stack i can't scroll them smoothly. So i decided to use CustomScrollView and SliverPersistentHeader. Unfortunately SliverPersistentHeader list cant touch to the bottom widget when scrolling. Can anybody save my day ?

class StackedList extends StatefulWidget {
  @override
  _StackedListState createState() => _StackedListState();
}

class _StackedListState extends State<StackedList> {
  final List<Color> colors = Colors.primaries;
  static const _minHeight = 24.0;
  static const _maxHeight = 80.0;
  @override
  Widget build(BuildContext context) {
    List<Color> _colors = List();
    _colors.addAll(colors);

    return CustomScrollView(
      physics: ClampingScrollPhysics(),
      shrinkWrap: false,
      slivers: <Widget>[
        ..._colors
            .map(
              (color) => StackedListChild(
                minHeight: _minHeight,
                maxHeight: _maxHeight,
                floating: false,
                pinned: true,
                child: getCard(
                  cardId: _colors.indexOf(color).toString(),
                  color: color,
                ),
              ),
            )
            .toList(),
      ],
    );
  }


}

class StackedListChild extends StatelessWidget {
  final double minHeight;
  final double maxHeight;
  final bool pinned;
  final bool floating;
  final Widget child;

  SliverPersistentHeaderDelegate get _delegate => _StackedListDelegate(
      minHeight: minHeight, maxHeight: maxHeight, child: child);

  const StackedListChild({
    Key key,
    @required this.minHeight,
    @required this.maxHeight,
    @required this.child,
    this.pinned = false,
    this.floating = false,
  })  : assert(child != null),
        assert(minHeight != null),
        assert(maxHeight != null),
        assert(pinned != null),
        assert(floating != null),
        super(key: key);

  @override
  Widget build(BuildContext context) => SliverPersistentHeader(
      key: key, pinned: pinned, floating: floating, delegate: _delegate);
}

class _StackedListDelegate extends SliverPersistentHeaderDelegate {
  final double minHeight;
  final double maxHeight;
  final Widget child;

  _StackedListDelegate({
    @required this.minHeight,
    @required this.maxHeight,
    @required this.child,
  });

  @override
  double get minExtent => minHeight;

  @override
  double get maxExtent => math.max(maxHeight, minHeight);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return new SizedBox.expand(child: child);
  }

  @override
  bool shouldRebuild(_StackedListDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
        minHeight != oldDelegate.minHeight ||
        child != oldDelegate.child;
  }
}

Add a Sliver view at the end of the list with enough height to make enough room to scroll the SliverPersistentHeaders till last one.

slivers: <Widget>[
    ..._colors
        .map(
          (color) => StackedListChild(
            minHeight: _minHeight,
            maxHeight: _maxHeight,
            floating: false,
            pinned: true,
            child: getCard(
              cardId: _colors.indexOf(color).toString(),
              color: color,
            ),
          ),
        )
        .toList(),


   ///extra bottom padding sliver
   SliverToBoxAdapter(
        child: Container(
         height: MediaQuery.of(context).size.height,
         alignment:Alignment.center,

          ))
  ],

See the live demo here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM