Flutter在ListView中只重建一行

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

我的应用程序中有一个列表视图。每行都有一些按钮,只影响同一行。我不想刷新整个Listview。当我在点击按钮上调用setState()时,会导致整个Listview刷新。

如何在行内的同一排敲击按钮上刷新?

在我的代码中,点击按钮必须导致相同按钮的更改颜色:

....
....
....

List<mPlay> playList = new List();

....
....
....

@override
  Widget build(BuildContext context) {
    return new Directionality(
        textDirection: globals.textDirection,
        child: new Scaffold(
            key: globalKey,
            body: (playList != null && playList.length > 0)
                ? new SmartRefresher(
                    headerBuilder: _buildHeader,
                    enablePullDown: true,
                    enablePullUp: true,
                    onRefresh: _onRefresh,
                    onOffsetChange: _onOffsetCallback,
                    child: new ListView.builder(
                      padding: const EdgeInsets.all(0),
                      itemCount: playList.length,
                      itemBuilder: (context, i) {
                        return _buildRow(playList[i], i);
                      },
                    ))
                : EmptyWidget()));
  }

  Widget _buildRow(mPlay play, int index) {
  return new Card(
        child: new Container(
        child: new Row(children: <Widget>[
        new InkWell(
                            child: new IconButton(
                                icon: Icon(
                                  Icons.group_add,
                                  color: play.isContributed == "1"
                                      ? Colors.blueAccent
                                      : Colors.black,
                                ),                                
                                onPressed: () {
                                  play.isContributed =
                                      play.isContributed == "0" ? "1" : "0";
                                  setState(() {
                                    playList[index].isContributed =
                                        play.isContributed;
                                  });
                                }))
        ])
        ))


  }
listview flutter listviewitem setstate rebuild
1个回答
1
投票

您必须使用状态管理方法,如范围模型,BLoC,redux。 Checkout this post to get a proper idea

© www.soinside.com 2019 - 2024. All rights reserved.