Skip to content

Scrollable snapOffsetCallback() physics are wrong #6431

Closed

Description

@HansMuller

The demo below snaps scroll offsets to page boundaries. Scroll deceleration after a fling should match the platform, even when the scroll offset is snapped. It's pretty far off the mark at the moment.

class AnimationDemo extends StatefulWidget {
  static const String routeName = '/animation';

  @override
  AnimationDemoState createState() => new AnimationDemoState();
}

class AnimationDemoState extends State<AnimationDemo> {
  final GlobalKey<ScrollableState> _scrollableKey = new GlobalKey<ScrollableState>();
  final List<Color> colors = [
    Colors.red[100],
    Colors.red[200],
    Colors.red[300],
    Colors.red[400],
    Colors.red[500],
    Colors.red[600],
  ];

  Widget _createPage(int i) {
    final double pageWidth = MediaQuery.of(context).size.width;
    return new Container(
      decoration: new BoxDecoration(
        backgroundColor: colors[i]
      ),
      child: new SizedBox(
        width: pageWidth,
        child: new Center(
          child: new Text('Page $i'),
        ),
      ),
    );
  }

  double _snapScrollOffset(double flingScrollOffset, Size containerSize) {
    final double pageWidth = MediaQuery.of(context).size.width;
    final double currentScrollOffset = _scrollableKey.currentState.scrollOffset;
    final int currentPage = (currentScrollOffset / pageWidth).floor();

    // Don't snap overscrolls
    if (currentPage == 0 && flingScrollOffset < currentScrollOffset)
      return flingScrollOffset;
    if (currentPage == 5 && flingScrollOffset > currentScrollOffset)
      return flingScrollOffset;

    final int targetPage = (flingScrollOffset > currentScrollOffset)
      ? (flingScrollOffset / pageWidth).ceil()
      : (flingScrollOffset / pageWidth).floor();
    return targetPage.toDouble() * pageWidth;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Travel stream'),
      ),
      body: new ScrollableViewport(
        scrollableKey: _scrollableKey,
        scrollDirection: Axis.horizontal,
        snapOffsetCallback: _snapScrollOffset,
        child: new Row(
          children: [0, 1, 2, 3, 4, 5].map(_createPage).toList(),
        ),
      ),
    );
  }
}

Activity

  1. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    f: scrollingViewports, list views, slivers, etc.
    on Oct 20, 2016
  2. Hixie commented on Feb 17, 2017

    @Hixie
    Contributor

    This feature is gone now.

  3. added a commit that references this issue on Oct 4, 2018
  4. added a commit that references this issue on Oct 9, 2018
  5. added a commit that references this issue on Oct 9, 2018
  6. github-actions commented on Sep 5, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  7. locked as resolved and limited conversation to collaborators on Sep 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: scrollingViewports, list views, slivers, etc.frameworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions