首页 > 解决方案 > Flutter - 选定的项目移动到 ListView 的顶部

问题描述

我一直在寻找一种将所选项目永久移动到 ListView 顶部的方法。

这可以通过按下元素iconbutton旁边的ListView, 并让该元素在 上移动到顶部来工作refresh

如果可能的话,我希望图标按钮在enabled启用时变为(即发光/亮起),disabled在禁用时变为(灰色,但仍可按下)。

我无法在这个问题中包含我的所有代码,因此所有这些_getListItemUi都可以在以下位置获得:https ://github.com/Jak3-02/myproject2

这是我当前的 ListView 的样子:

Widget _cryptoWidget() {
    return new Container(
        child: new Column(
          children: <Widget>[
            new Flexible(
              child: new ListView.builder(
                itemCount: _currencies.length,
                itemBuilder: (BuildContext context, int index) {
                  final int i = index ~/ 2;
                  final Crypto currency = _currencies[i];
                  final MaterialColor color = _colors[i % _colors.length];
                  if (index.isOdd) {
                    return new Divider();
                  }
                  return _getListItemUi(currency, color);
                },
              ),
            ),
          ],
        )
      );
  }

谢谢,所有想法都值得赞赏。:)

标签: androidioslistviewdartflutter

解决方案


这听起来很像一个课堂项目,所以我将为您指出正确的方向,让您自己解决这个问题。

请注意代码中的项目生成器。它读取 _currencies 列表并构建相应的小部件。如果您重新排列_currencies 中的项目并再次运行此代码会发生什么?

当 _currencies 列表更改时,如何让 _cryptoWidget 重绘?你会使用有状态小部件还是无状态小部件?当数据发生变化时,其中哪一个会重绘自己?

您想要一个可点击的图标,在启用和禁用时看起来会有所不同。您查看过小部件目录吗?

有了这些笔记,你应该可以很容易地解决这个问题。


推荐阅读