android - flutter - 如何以编程方式自动滚动到具有动态高度的索引

标签 android flutter dart scroll flutter-layout

根据 Flutter 的 issue here , 目前不支持自动滚动到索引,其中每个单元格/项目都具有动态高度。我尝试了另一种解决方案,但没有效果。

那么,什么是使 ListView 具有动态高度的 AutoScroll 的临时解决方案?

有什么想法吗?

最佳答案

你的问题

so, What's a temporary solution to make ListView with AutoScroll for dynamic height?

屏幕记录

enter image description here

解决方案

滚动到索引包

This package provides the scroll to index mechanism for fixed/variable row height for Flutter scrollable widget.

This is a widget level library, means you can use this mechanism inside any Flutter scrollable widget.

用法

pubspec.yaml

 scroll_to_index: any

示例来自 quire-io/scroll-to-index: scroll to index with fixed/variable row height inside Flutter scrollable widget

用法 - Controller

controller.scrollToIndex(index, preferPosition: AutoScrollPosition.begin)

用法 - ListView

ListView(
scrollDirection: scrollDirection,
controller: controller,
children: randomList.map<Widget>((data) {
    final index = data[0];
    final height = data[1];
    return AutoScrollTag(
    key: ValueKey(index),
    controller: controller,
    index: index,
    child: Text('index: $index, height: $height'),
    highlightColor: Colors.black.withOpacity(0.1),
    );
}).toList(),
)

要求额外的 unixercoder

Is this for finite or infinite list ?

使用ListView.builder模拟无限列表

    ListView.builder(
    scrollDirection: scrollDirection,
    controller: controller,
    itemBuilder: (context, i) => Padding(
      padding: EdgeInsets.all(8),
      child: _getRow(i, (min + rnd.nextInt(max - min)).toDouble()),
    ),

22 次尝试中有 18 次正确,大约 (81%) 或接近它

屏幕记录

enter image description here

所以它不是100%支持

引用:

第一屏录制代码

//Copyright (C) 2019 Potix Corporation. All Rights Reserved.
//History: Tue Apr 24 09:29 CST 2019
// Author: Jerry Chen

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 = 100;
  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);
    controller.highlight(counter);
  }

  Widget _getRow(int index, double height) {
    return _wrapScrollTag(
      index: index,
      child: Container(
        padding: EdgeInsets.all(8),
        alignment: Alignment.topCenter,
        height: height,
        decoration: BoxDecoration(
          border: Border.all(
            color: Colors.lightBlue,
            width: 4
          ),
          borderRadius: BorderRadius.circular(12)
        ),
        child: 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),
  );
}

第二屏录制代码:

//Copyright (C) 2019 Potix Corporation. All Rights Reserved.
//History: Tue Apr 24 09:29 CST 2019
// Author: Jerry Chen

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> {
  final scrollDirection = Axis.vertical;
  var rnd = math.Random();
  int min = 50;
  int max = 200;
  AutoScrollController controller;
  @override
  void initState() {
    super.initState();
    controller = AutoScrollController(
        viewportBoundaryGetter: () =>
            Rect.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),
        axis: scrollDirection);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
        scrollDirection: scrollDirection,
        controller: controller,
        itemBuilder: (context, i) => Padding(
          padding: EdgeInsets.all(8),
          child: _getRow(i, (min + rnd.nextInt(max - min)).toDouble()),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _scrollToIndex,
        tooltip: 'Increment',
        child: Text(counter.toString()),
      ),
    );
  }

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

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

  Widget _getRow(int index, double height) {
    return _wrapScrollTag(
        index: index,
        child: Container(
          padding: EdgeInsets.all(8),
          alignment: Alignment.topCenter,
          height: height,
          decoration: BoxDecoration(
              border: Border.all(color: Colors.lightBlue, width: 4),
              borderRadius: BorderRadius.circular(12)),
          child: 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),
      );
}

关于android - flutter - 如何以编程方式自动滚动到具有动态高度的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57564811/

相关文章:

firebase - currentUser() 不是工作类型为什么我看到表达式没有计算为函数,所以它不能被调用

firebase - 如何使用 Dart 在 Flutter 中访问 Firestore clearPersistence() 方法

Dartlang 语法用 mixin 扩展类?

Android 应用与您的设备 android studio 不兼容

Flutter - 行内的多行标签

android - 如何使用Gradle一次构建多个APK?

flutter - 显示键盘时如何拦截 flutter 后退按钮

flutter - 此小部件的所有子项都必须在 Reorderable Listview 中有一个键

java - 如何在共享首选项中分配唯一的字符串值

android - 无法完全正确地获得 Spanable