Skip to content

Instantly share code, notes, and snippets.

@radroot1
Created November 7, 2019 16:22
Show Gist options
  • Select an option

  • Save radroot1/20e9ddb183f638c83258fadf92dfb093 to your computer and use it in GitHub Desktop.

Select an option

Save radroot1/20e9ddb183f638c83258fadf92dfb093 to your computer and use it in GitHub Desktop.
SliverFillRemaining overscroll problem
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MainBody(),
);
}
}
class MainBody extends StatefulWidget {
@override
_MainBodyState createState() => _MainBodyState();
}
class _MainBodyState extends State<MainBody> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor:Colors.black,
body: CustomScrollView(
slivers: [
SliverList(
delegate: new SliverChildBuilderDelegate(
(BuildContext context, int index) {
return new Container(
color: Colors.white,
alignment: Alignment.center,
child: new Text('item $index'),
);
},
childCount: 10,
),
),
SliverFillRemaining(child: Container(color:Colors.white), hasScrollBody: false, fillOverscroll: true)
],
),
);
}
}
class CustomSliverFillRemaining extends SingleChildRenderObjectWidget {
/// Creates a sliver that fills the remaining space in the viewport.
const CustomSliverFillRemaining({
Key key,
Widget child,
this.hasScrollBody = true,
}) : assert(hasScrollBody != null),
super(key: key, child: child);
final bool hasScrollBody;
@override
_CustomRenderSliverFillRemaining createRenderObject(BuildContext context) => _CustomRenderSliverFillRemaining(hasScrollBody: hasScrollBody);
@override
void updateRenderObject(BuildContext context, _CustomRenderSliverFillRemaining renderObject) => renderObject.hasScrollBody = hasScrollBody;
}
class _CustomRenderSliverFillRemaining extends RenderSliverSingleBoxAdapter {
_CustomRenderSliverFillRemaining({
RenderBox child,
this.hasScrollBody = true,
}) : assert(hasScrollBody != null),
super(child: child);
bool hasScrollBody;
@override
void performLayout() {
final double extent = constraints.remainingPaintExtent
- min(constraints.overlap, 0.0)
// Adding the offset for when this SliverFillRemaining is not scrollable,
// so it will stretch to fill on overscroll.
+ (hasScrollBody ? 0.0 : constraints.scrollOffset);
if (child != null)
child.layout(constraints.asBoxConstraints(minExtent: extent, maxExtent: extent), parentUsesSize: true);
final double paintedChildSize = calculatePaintOffset(constraints, from: 0.0, to: extent);
assert(paintedChildSize.isFinite);
assert(paintedChildSize >= 0.0);
geometry = SliverGeometry(
// 0.0 can be applied here for cases when there is not scroll body since
// SliverFillRemaining will not have any slivers following it.
scrollExtent: hasScrollBody ? constraints.viewportMainAxisExtent : 0.0,
paintExtent: paintedChildSize,
maxPaintExtent: paintedChildSize,
hasVisualOverflow: extent > constraints.remainingPaintExtent || constraints.scrollOffset > 0.0,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment