Flutter 中的 ScrollToRowAtIndex 等价物

ScrollToRowAtIndex equivalent in Flutter

背景: 我最近开始使用 flutter/dart 进行开发,您可以猜到我来自 iOS 背景。在 UITableView 中滚动到 iOS 中的某个给定 indexPath(部分,行)非常容易,我相信 Android 中的情况也类似,但实现相同的效果确实很痛苦在 Flutter 中。

问题: 有人能给我指出一个解决方案吗?在 ListView 中,我可以顺利滚动到给定的索引?

我尝试过的: 我一直在尝试不同的包,比如 (https://pub.dev/packages/flutter_section_table_view),因为我需要一个分段列表视图,它带有一个 section/row 的动画,但它取决于每一行的准确高度并创建一个地图将滚动视图跳转或动画化到该高度值。鉴于该列表视图中的项目是动态的并且可以根据某些操作扩展或收缩,因此实现起来并不容易。我也试图通过计算高度等来处理这个问题,但这会影响滚动的性能。这就是为什么我正在寻找一个带有一些预先计算好的地图的解决方案,它可以像我们在网页中那样滚动到标签或散列。

您可以复制粘贴 运行 下面的完整代码
您可以使用包 https://pub.dev/packages/scroll_to_index
您可以用 AutoScrollTag 包装 widget 并调用 controller.scrollToIndex
代码片段

await controller.scrollToIndex(98,
        preferPosition: AutoScrollPosition.begin);
...     
Widget _wrapScrollTag({int index, Widget child}) => AutoScrollTag(
        key: ValueKey(index),
        controller: controller,
        index: index,
        child: child,
        highlightColor: Colors.black.withOpacity(0.1),
      );

工作演示

完整代码

import 'dart:math' as math;

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Scroll To Index Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Scroll To Index Demo'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  static const maxCount = 10000;
  final random = math.Random();
  final scrollDirection = Axis.vertical;

  AutoScrollController controller;
  List<List<int>> randomList;

  @override
  void initState() {
    super.initState();
    controller = AutoScrollController(
        viewportBoundaryGetter: () =>
            Rect.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),
        axis: scrollDirection);
    randomList = List.generate(maxCount,
        (index) => <int>[index, (1000 * random.nextDouble()).toInt()]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView(
        scrollDirection: scrollDirection,
        controller: controller,
        children: randomList.map<Widget>((data) {
          return Padding(
            padding: EdgeInsets.all(8),
            child: _getRow(data[0], math.max(data[1].toDouble(), 50.0)),
          );
        }).toList(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _scrollToIndex,
        tooltip: 'Increment',
        child: Text(counter.toString()),
      ),
    );
  }

  int counter = -1;
  Future _scrollToIndex() async {
    setState(() {
      counter++;

      if (counter >= maxCount) counter = 0;
    });

    //await controller.scrollToIndex(counter, preferPosition: AutoScrollPosition.begin);
    await controller.scrollToIndex(98,
        preferPosition: AutoScrollPosition.begin);
    controller.highlight(counter);
  }

  Widget _getRow(int index, double height) {
    return _wrapScrollTag(
        index: index,
        child: ListTile(title: Text('index: $index, height: $height')));
  }

  Widget _wrapScrollTag({int index, Widget child}) => AutoScrollTag(
        key: ValueKey(index),
        controller: controller,
        index: index,
        child: child,
        highlightColor: Colors.black.withOpacity(0.1),
      );
}