单击按钮时,是否可以更新列表视图的项目?

时间:2019-05-02 09:23:32

标签: dart flutter

enter image description here enter image description here我正在创建一个Listview,它从服务器动态获取数据。每个项目都有一个按钮“ like”。当用户单击按钮时,我想更改按钮“不同于”。我的物品是无状态小部件,所以找不到方法。

1 个答案:

答案 0 :(得分:0)

希望有帮助

class ListViewPosts extends StatefulWidget {
  final MyResponse posts;

  ListViewPosts({Key key, this.posts}) : super(key: key);

  @override
  _ListViewPostsState createState() => _ListViewPostsState();
}

class _ListViewPostsState extends State<ListViewPosts> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
          itemCount: widget.posts.data.length,
          padding: const EdgeInsets.all(15.0),
          itemBuilder: (context, position) {
            return Column(
              children: <Widget>[
                Divider(height: 5.0),
                ListTile(
                  title: Text(
                    '${widget.posts.data[position].post1}',
                    style: TextStyle(
                      fontSize: 22.0,
                      color: Colors.deepOrangeAccent,
                    ),
                  ),
                  subtitle: Text(
                    '${widget.posts.data[position].age}',
                    style: new TextStyle(
                      fontSize: 18.0,
                      fontStyle: FontStyle.italic,
                    ),
                  ),
                  trailing:
                      FlatButton(onPressed: _onPressed, child: Text("here you can enter text according to your like/unlike")),
                  onTap: () => _onTapItem(context, widget.posts, position),
                ),
              ],
            );
          }),
    );
  }