Skip to content

FixedExtentScrollController should be usable with a ListView having itemExtent specified #115659

@fischerscode

Description

@fischerscode

Use case

Whenever itemExtent is used in a ListView, it would be nice to control the scrolling using the item index instead of an offset.

Use case:
An app has a list of items and the list should be scrolled to the 5th item.

Proposal

Allow FixedExtentScrollController (or a controller with similar functionality specialized for lists, e.g. FixedExtentListScrollController) to be used with a ListView having itemExtent specified.

Currently FixedExtentScrollController can only be used with ListWheelScrollViews is thrown.

import 'package:flutter/material.dart';

final scrollController = FixedExtentScrollController();

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      home: Scaffold(
        body: ListView.builder(
            controller: scrollController, // this line does not work
            itemExtent: 20,
            itemBuilder: (context, index) =>
                Center(child: Text(index.toString()))),
      ),
    );
  }
}

In case #113431 gets implemented, itemExtentPerIndex should also allow using a FixedExtentScrollController.
Apparently I'm not the only one needing this: #25074 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions