Flutter SliverPersistentHeader导致“滚动时底部溢出”

时间:2020-02-07 16:33:01

标签: flutter overflow flutter-sliver

我有一个SliverPersistentHeader,当用户滚动屏幕时会导致bottom overflowed

Bottom overflowed

我该如何解决?

    return CustomScrollView(
      slivers: <Widget>[
        SliverPersistentHeader(
          pinned: false,
          delegate: DynamicSliverHeaderDelegate(
            maxHeight: 256,
            minHeight: 186,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                _topo(context, grupo),
                _infoGrupo(context, grupo),
              ],
            ),
          ),
        ),

        // TODO: Lista do grupo 
        SliverFillViewport(
          delegate: SliverChildListDelegate(
            [
              Container(
                padding: EdgeInsets.only(top: 100),
                color: Colors.green.withOpacity(0.2),
                child: Column(
                  children: <Widget>[
                    Text('TODO... A construir'),
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );

// .....

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

  const DynamicSliverHeaderDelegate({
    @required this.child,
    this.maxHeight = 250,
    this.minHeight = 80,
  });

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return child;
  }

  // @override
  // bool shouldRebuild(DynamicSliverHeaderDelegate oldDelegate) => true;

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

  @override
  double get maxExtent => maxHeight;

  @override
  double get minExtent => minHeight;
}

2 个答案:

答案 0 :(得分:1)

通过将DynamicSliverHeaderDelegate列子元素与SingleChildScrollView´ with NeverScrollableScrollPhysics` physics 一起解决。

return CustomScrollView(
  slivers: <Widget>[
    SliverPersistentHeader(
      pinned: false,
      delegate: DynamicSliverHeaderDelegate(
        maxHeight: 256,
        minHeight: 186,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            _topo(context, grupo),
            _infoGrupo(context, grupo),
          ],
        ),
      ),
    ),
    // TODO: Lista do grupo 
    // ...
  ],
);

答案 1 :(得分:0)

完整的解决方法是:

return CustomScrollView(
  slivers: <Widget>[
    SliverPersistentHeader(
      pinned: false,
      delegate: DynamicSliverHeaderDelegate(
        maxHeight: 256,
        minHeight: 186,
        child: SingleChildScrollView(
          physics: const NeverScrollableScrollPhysics(),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _topo(context, grupo),
              _infoGrupo(context, grupo),
            ],
          ),
        ),
      ),
    ),
    // TODO: Lista do grupo
    // ...
  ],
);

}