Flutter - 选定的项目移动到ListView顶部

4

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

通过按下ListView元素旁边的一个iconbutton,并在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);
                },
              ),
            ),
          ],
        )
      );
  }

谢谢,非常感激所有的想法。 :)


1
你可以拥有一个选定项目列表,这些项目作为ListView的第一级子项使用。 - Jacob Phillips
请问您能否向我展示如何将此代码添加到我的项目中?另外,我该如何让用户选择要添加到所选项目列表中的项目? - Jake
1个回答

3
这听起来很像一个课程项目,所以我会指引你找到正确的方向来自己解决问题。
注意你代码中的Item Builder。它读取_currencies列表并构建相应的小部件。如果你重新排列了_currencies中的项目并再次运行此代码,会发生什么? 当_currencies列表更改时,你如何使_cryptoWidget重新绘制?你会使用Stateful widget还是Stateless widget?这两者中哪一个在其数据更改时会重新绘制自身? 你想要一个可点击的图标,它在启用和禁用时外观不同。你看过Widget Catalog吗? 有了这些笔记,你应该能够轻松解决这个问题。

非常感谢您抽出时间来整理这个,它对我帮助很大。 :) - Jake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接