【问题标题】:How to display scrolling index for scrollController in flutter?如何在颤动中显示滚动控制器的滚动索引?
【发布时间】:2020-12-10 03:47:59
【问题描述】:

我想通过使用 scrollController 在 listView 的底部显示索引,就像在下图中显示的那样:

用户向下滚动或向上滚动后,左侧以红色突出显示的计数会根据用户的滚动方向增加/减少。

我想要实现的是自动更新显示项目的索引,在图片上用红色表示。因此,每当用户向下或向上滚动时,此索引都会根据显示项目的索引进行更新。

图片显示我已经到了第26项。每当我向下或向上滚动时,此索引都会更新。

我尝试使用为滚动事件发出的偏移量,但没有成功。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    方法是像你一样使用滚动控制器。

    您需要使用已知的项目大小和侦听器。

    // Declaring the controller and the item size
    ScrollController _scrollController;
    final itemSize = 100.0;
    
    // Initializing
    @override
    void initState() {
      _scrollController = ScrollController();
      _scrollController.addListener(_scrollListener);
      super.initState();
    }
    
    // Your list widget (must not be nested lists)
    ListView.builder(
          controller: _scrollController,
          itemCount: <Your list length>,
          itemExtent: itemSize,
          itemBuilder: (context, index) {
              return ListTile(<your items>);
          },
    ),
    
    // With the listener and the itemSize, you can calculate which item
    // is on screen using the provided callBack. Something like this:
    void _scrollListener() {
      setState(() {
        var index = (_scrollController.offset / itemSize).round() + 1;
      });
    }
    

    为 scrollController 添加监听器将调用每次滚动列表时提供的回调。您可以使用相同的逻辑处理列表的许多行为,包括识别触发侦听器的事件类型、滚动方向等。

    【讨论】:

    • 好吧,我使用了一些不同的解决方案。我需要方程式来计算项目大小以增加索引。我发现关于您的解决方案的一件事是我不知道 _controller 来自哪里,如果我知道它的初始化会更有帮助。
    • 对不起,都是一样的,控制器是ScrollController。我正在纠正答案。
    【解决方案2】:

    有一个名为 scroll to index 的库可以帮助您。您可以将 $index 显示在您的 toast 消息中。以下示例来自 lib 的作者:

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

    【讨论】:

      【解决方案3】:

      https://medium.com/flutter-community/create-shop-list-with-flutter-d13d3c20d68b 也许这个可以帮助你。源代码也可用。关于项目高度的简单数学可能会有所帮助。

      【讨论】:

        猜你喜欢
        • 2019-10-29
        • 2021-11-13
        • 2021-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-31
        • 2020-08-28
        • 2019-08-25
        相关资源
        最近更新 更多