I'm looking for a widget that allows 2D scrolling like a UIScrollView.
I think, Flutter right now only supports 1D scrolling which is – unfortunate.
I tried to build something like a UIScrollView using a Stack with a Positioned child and a GestureDetector for detecting pan gestures. However, the illusion fails once the user tries to "fling" the view. I noticed that there is a velocity but I need help to translate this into the correct animation. Has anybody implemented such a scrolling behaviour already?
I also eventually need bouncing behaviour. Making sure that the offset doesn't get negative is easy but how do I access the stack's size and the child's size to determine the maximum offset? Is this possible without using RenderBoxes? Those are still quite mysterious for me. Is there a way to use a BouncingScrollPhysics which should do the trick for one dimension at least?
Here's my code:
class ScrollablePane extends StatefulWidget {
final Widget child;
const ScrollablePane({Key key, this.child}) : super(key: key);
@override
_ScrollablePaneState createState() => _ScrollablePaneState();
}
class _ScrollablePaneState extends State<ScrollablePane> {
Offset offset = Offset.zero;
@override
Widget build(BuildContext context) {
return Stack(
fit: StackFit.expand,
children: [
Positioned(
left: offset.dx,
top: offset.dy,
child: widget.child,
),
GestureDetector(
onPanUpdate: _onPanUpdate,
onPanEnd: _onPanEnd,
),
],
);
}
void _onPanUpdate(DragUpdateDetails d) {
setState(() {
offset += d.delta;
});
}
void _onPanEnd(DragEndDetails d) {
print(d.velocity.pixelsPerSecond);
}
}
Thanks in advance for any hint to complete the implementation.