Skip to content

scrollTo() can not scroll to the right position #206

Open
@yhcharles

Description

@yhcharles

Problem description

package: scrollable_positioned_list
bug: scrollTo() can't scroll to the right position

Steps to reproduce

import 'package:flutter/material.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _itemCount = 500;
  int _current = 0;
  final ItemScrollController _scrollController = ItemScrollController();

  void _jump(int offset) {
    _current += offset;
    if (_current < 0) _current = 0;
    if (_current >= _itemCount) _current = _itemCount - 1;
    setState(() {});

    _scrollController.scrollTo(
        index: _current, duration: Duration(milliseconds: 100));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ScrollablePositionedList.builder(
          itemCount: _itemCount,
          itemScrollController: _scrollController,
          itemBuilder: (BuildContext context, int index) {
            return SelectableText(
              '$index',
              style: TextStyle(
                  fontSize: 24,
                  backgroundColor: index == _current
                      ? Theme.of(context).backgroundColor
                      : null),
            );
          }),
      bottomSheet: Row(
        children: [
          FlatButton(
            child: Icon(Icons.fast_rewind),
            onPressed: () {
              _jump(-100);
            },
          ),
          FlatButton(
            child: Icon(Icons.fast_forward),
            onPressed: () {
              _jump(100);
            },
          ),
          FlatButton(
            child: Icon(Icons.skip_previous),
            onPressed: () {
              _jump(-1);
            },
          ),
          FlatButton(
            child: Icon(Icons.skip_next),
            onPressed: () {
              _jump(1);
            },
          ),
        ],
      ),
    );
  }
}
  1. The above code implements a minimal example to reproduce the bug. It shows a list of 500 items. One of the items is marked as "current" item, and highlighted with different background color. At the bottom of the UI there're 4 buttons to jump forward/backward in the list: backward 100, forward 100, backward 1, forward 1. After jumped to a certain item, it should automatically highlight the new item and scroll the new item into current screen view.
  2. Started with the 1st item. Then jump "forward 1" 5 times, then jump "forward 100", then jump "forward 1"

Expected behavior

  • jump "forward 1", seems good, it puts item 1 on the top of the screen
  • jump "forward 1", seems good, it puts item 2 on the top of the screen
  • jump "forward 1", seems good, it puts item 3 on the top of the screen
  • jump "forward 1", seems good, it puts item 4 on the top of the screen
  • jump "forward 1", seems good, it puts item 5 on the top of the screen
  • jump "forward 100" also seems good, it puts item 105 on the top
  • jump "forward 1" again, bug here, it's expected to put item 106 on the top, but it seems jumped to some where else, and there's an exception shown in the debug console
════════ Exception caught by scheduler library ═════════════════════════════════════════════════════
The following UnsupportedError was thrown during a scheduler callback:
Unsupported operation: Infinity or NaN toInt

When the exception was thrown, this was the stack: 
#0      double.toInt (dart:core-patch/double.dart:192:36)
#1      double.round (dart:core-patch/double.dart:160:34)
#2      _PositionedListState._schedulePositionNotificationUpdate.<anonymous closure> (package:scrollable_positioned_list/src/positioned_list.dart:324:45)
#3      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
#4      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1063:9)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
[VERBOSE-2:profiler_metrics_ios.mm(186)] Error retrieving thread information: (ipc/send) invalid destination port

Actual behavior

Environment

package version: scrollable_positioned_list: ^0.1.7
flutter version:
Flutter 1.24.0-6.0.pre • channel dev • https://github.com/flutter/flutter.git
Framework • revision 13896b3bd1 (3 weeks ago) • 2020-10-28 19:43:19 -0700
Engine • revision 073263e39d
Tools • Dart 2.11.0 (build 2.11.0-260.0.dev)

Additional details

Screen record:
output

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp: scrollable_positioned_listRelated to package:scrollable_positioned_list

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions