首页 文章

如何在onTap(Flutter ListItem)中执行函数后更改Icon的颜色

提问于
浏览
0

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

如果用户点击Icon以使其成为收藏夹或Unfavorite,我想在更新后更改颜色 .

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)),
),

3 回答

  • 0

    在onTap函数中使用setState并在那里指定颜色 .

  • 0

    创建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)),
          );
        }
      }
    
  • 0

    Solved the problem this way (Updated Code)

    Code of List Tile

    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)),
                          ),
    

    Functions for changing state

    makeFavorite(int id, int index) {
        // operations to be performed
        // end of operations to be performed
        setState(() {
          indexes[index] = 1;
        });
      }
    

相关问题