【问题标题】:ScrollToRowAtIndex equivalent in FlutterFlutter 中的 ScrollToRowAtIndex 等价物
【发布时间】:2020-08-06 05:48:36
【问题描述】:

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

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

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

【问题讨论】:

  • 在问题中,您需要包括到目前为止您尝试过的内容。
  • 当然@JigarPatel,我刚刚添加了更多详细信息,说明我尝试了什么以及为什么它没有给出预期的结果,以及我现在正在寻找什么。谢谢!

标签: flutter flutter-layout flutter-animation


【解决方案1】:

您可以在下面复制粘贴运行完整代码
你可以使用包https://pub.dev/packages/scroll_to_index
你可以用AutoScrollTag 包裹widget 并调用controller.scrollToIndex
代码sn-p

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),
      );
}

【讨论】:

    猜你喜欢
    • 2017-11-07
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 2018-07-11
    • 2022-01-03
    相关资源
    最近更新 更多