google / flutter.widgets

https://pub.dev/packages/flutter_widgets
BSD 3-Clause "New" or "Revised" License
1.38k stars 488 forks source link

ItemScrollController.scrollTo() allows negative index input #504

Open hrafnthor opened 12 months ago

hrafnthor commented 12 months ago

Problem description

ItemScrollController.scrollTo() accepts a negative index input, which then gets passed over to the ItemBuilder function inside ScrollablePositionList, thus breaking the preconditions set by that function:

"Called to build children for the list with 0 <= index < itemCount".

Steps to reproduce

The following widget will showcase the behavior succinctly :

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

class TestWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final controller = ItemScrollController();

    final list = [1, 2, 3, 4];
    return ScrollablePositionedList.builder(
     itemScrollController: controller,
      itemCount: list.length,
      itemBuilder: (context, index) {
        final item = list[index];
        return ListTile(
          title: Text("$item"),
          onTap: () => controller.scrollTo(
            index: -1,
            duration: const Duration(milliseconds: 600),
          ),
        );
      },
    );
  }
}

Expected behavior

ItemScrollController.scrollTo() should validate the index it is given and throw immediately if the value is negative.

Actual behavior

The negative index is propagated to the item builder function inside the ScrollablePositionedList, causing any stacktraces raised to point incorrectly to a different call-site as the culprit, rather than any call-site where ItemScrollController.scrollTo() is invoked.

Environment

dart: 3.1.0 flutter: 3.13.2 scrollable_positioned_list: 0.3.8

Additional details