在滚动控制器上添加监听器以 animateTo 偏移量不起作用

问题描述 投票:0回答:1

在尝试在 ReorderableListView 的标题上创建捕捉效果时,我编写了以下代码。但是

jobWidgetListener
函数没有将我的列表项动画到所需的偏移量。 在我的 StatefulWidget 的状态下,我有

 static const _processItemWidth = 150.0;

 void jobWidgetListener() {
    if (_reorderScrollController.hasClients) {

      if (_reorderScrollController.position.extentBefore < _processItemWidth * 0.8) {
        print('this');
        _reorderScrollController.animateTo(
          0.0,
          duration: const Duration(milliseconds: 350),
          curve: Curves.easeOut,
        );
      } else if (_reorderScrollController.position.extentBefore < _processItemWidth) {
        print('that');
        _reorderScrollController.animateTo(
          _processItemWidth + 8.0,
          duration: const Duration(milliseconds: 350),
          curve: Curves.easeOut,
        );
      }
    }
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) => _scrollToIndex(_activeProcessIndex)); // <- this one scrolls to active item in my list on rebuild and is working fine, so not providing details here
    _reorderScrollController.addListener(jobWidgetListener); // <-- Listener added here
  }

jobWidgetListener
函数中的打印语句正确打印在控制台中。 在构建功能中,我有

ReorderableListView.builder(
                header: Container(
                    width: _processItemWidth, decoration: BoxDecoration(color: Colors.blue)),
                scrollDirection: Axis.horizontal,
                scrollController: _reorderScrollController,
                cacheExtent: _processItemWidth * _activeProcessIndex > 300.0
                    ? _processItemWidth * _activeProcessIndex
                    : 300.0,
                onReorder: (int oldIndex, int newIndex) {
                  _updateProcessOrder(
                      oldIndex: oldIndex,
                      newIndex: newIndex,
                      oldProcess: widget.jobs.process,
                      jobID: widget.jobs.jid);
                },
                itemBuilder: (BuildContext context, int index) {
                  return ProcessItemWidget(
                      key: ValueKey(processItem[index].name),
                      width: _processItemWidth,
                      index: index,
                      processItem: processItem,
                      jid: widget.jobs.jid);
                },
                itemCount: widget.jobs.process.length,
              ),
flutter listview scrollcontroller
1个回答
0
投票

该控制器变量是否已初始化?

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.