Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在onTap(颤振列表项)中执行函数后如何更改图标颜色_Flutter - Fatal编程技术网

Flutter 在onTap(颤振列表项)中执行函数后如何更改图标颜色

Flutter 在onTap(颤振列表项)中执行函数后如何更改图标颜色,flutter,Flutter,要在点击时更改图标的颜色。默认情况下,如果项目已经是收藏夹,则图标为红色,而其他图标为默认颜色 如果用户点击图标使其成为最受欢迎的或不受欢迎的,我想在更新后更改颜色 new ListTile( trailing: InkWell( child: Icon(Icons.share), ), leading: InkWell( onTap: () { snapshot.data[index].isFavorite == 0

要在点击时更改图标的颜色。默认情况下,如果项目已经是收藏夹,则图标为红色,而其他图标为默认颜色

如果用户点击图标使其成为最受欢迎的或不受欢迎的,我想在更新后更改颜色

new ListTile(
    trailing: InkWell(
      child: Icon(Icons.share),
    ),
    leading: InkWell(
        onTap: () {
          snapshot.data[index].isFavorite == 0
              ? makeFavorite(snapshot.data[index].id)
              : makeUnfavorite(
              snapshot.data[index].id);
        },
        child: snapshot.data[index].isFavorite == 1
            ? Icon(
          Icons.favorite,
          color: Colors.red,
        )
            : Icon(Icons.favorite)),
    title: new Text(snapshot.data[index].body,
        style: new TextStyle(
            fontWeight: FontWeight.bold, fontSize: 14.0)),
),

在onTap函数中使用setState并在其中指定颜色。

创建一个Statefull小部件以更改其状态

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Title'),
      ),
      body: new ListView.builder(itemBuilder: (context, index) {
        return new ListItem();
      }),
    );
  }

class ListItem extends StatefulWidget {
    @override
    State<StatefulWidget> createState() => new _ItemView();
  }
  class _ItemView extends State<ListItem>{
    bool isFavorite = false;
    @override
    Widget build(BuildContext context) {
      return new ListTile(
        trailing: InkWell(
          child: Icon(Icons.share),
        ),
        leading: InkWell(
            onTap: () {
              isFavorite = !isFavorite;
              setState(() {
              });
            },
            child: isFavorite ? Icon(
              Icons.favorite,
              color: Colors.red,
            ): Icon(Icons.favorite)),
        title: new Text('Your Text',
            style: new TextStyle(
                fontWeight: FontWeight.bold, fontSize: 14.0)),
      );
    }
  }
@覆盖
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“标题”),
),
正文:new ListView.builder(itemBuilder:(上下文,索引){
返回新的ListItem();
}),
);
}
类ListItem扩展StatefulWidget{
@凌驾
State createState()=>new_ItemView();
}
类_ItemView扩展了状态{
bool isFavorite=false;
@凌驾
小部件构建(构建上下文){
返回新的ListTile(
拖尾:墨水井(
子:图标(Icons.share),
),
引导:InkWell(
onTap:(){
isFavorite=!isFavorite;
设置状态(){
});
},
孩子:我最喜欢的图标(
我的最爱,
颜色:颜色,红色,
):Icon(Icons.favorite)),
标题:新文本(“您的文本”,
样式:新文本样式(
fontWeight:fontWeight.bold,fontSize:14.0),
);
}
}

这样解决了问题(更新的代码)

列表磁贴代码

new ListTile(
                        trailing: InkWell(
                          child: Icon(Icons.share),
                        ),
                        leading: InkWell(
                            onTap: () {
                              snapshot.data[index].isFavorite == 0
                                  ? makeFavorite(
                                      snapshot.data[index].id, index)
                                  : makeUnfavorite(
                                      snapshot.data[index].id, index);
                            },
                            child: (indexes[index] == 1)
                                ? Icon(
                                    Icons.favorite,
                                    color: Colors.red,
                                  )
                                : Icon(Icons.favorite)),
                        title: new Text(snapshot.data[index].body,
                            style: new TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 14.0)),
                      ),
 makeFavorite(int id, int index) {
    // operations to be performed
    // end of operations to be performed
    setState(() {
      indexes[index] = 1;
    });
  }
用于更改状态的功能

new ListTile(
                        trailing: InkWell(
                          child: Icon(Icons.share),
                        ),
                        leading: InkWell(
                            onTap: () {
                              snapshot.data[index].isFavorite == 0
                                  ? makeFavorite(
                                      snapshot.data[index].id, index)
                                  : makeUnfavorite(
                                      snapshot.data[index].id, index);
                            },
                            child: (indexes[index] == 1)
                                ? Icon(
                                    Icons.favorite,
                                    color: Colors.red,
                                  )
                                : Icon(Icons.favorite)),
                        title: new Text(snapshot.data[index].body,
                            style: new TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 14.0)),
                      ),
 makeFavorite(int id, int index) {
    // operations to be performed
    // end of operations to be performed
    setState(() {
      indexes[index] = 1;
    });
  }

您的项应该是一个StatefulWidget,然后您可以创建一个var来处理StatefulWidget,但是我在snapshot和onTap函数中获取数据,我必须执行一些操作,然后更新图标的颜色。然而,我已经找到了一个解决方案,根据这个数据列表的索引,使用另一个只保存1或0的列表。现在我用onTap只更改列表索引的状态,图标的颜色现在取决于该列表的值。