Flutter:更新ListView中的特定项目

问题描述

我有一个用户对象列表,它们包含用户数据并显示为ListView。每个项目都有一个按钮,当我单击一个项目按钮时,我试图更新ListView中的特定项目。但是我不能,而且我像下面那样重新加载了完整的ListView...。我想要按顺序仅更新列表数据中的特定对象,并更新这些按钮标题,并根据付款ID将紫色更改为绿色。无论如何,是否只需要更新ListView中的项目,而不必刷新整个列表?

ListView的加载方式如下:

FutureBuilder(
  future: getInwardResidentList(param),builder: (BuildContext context,AsyncSnapshot snapshot) {
    switch (snapshot.connectionState) {
      case ConnectionState.waiting:{
          return Container();}
      case ConnectionState.active:{
          break;}
      case ConnectionState.done:{
          if (snapshot.hasData) {
            if (snapshot.data != null) {
              if (snapshot.data.length > 0) {
                return Container(
                  color: Colors.white70,child: Scrollbar(
                    child: ListView.builder(
                        scrollDirection: Axis.vertical,shrinkWrap: true,physics: ScrollPhysics(),padding: EdgeInsets.all(5),itemCount: snapshot.data.length,itemBuilder: (context,index) {
                          return generateColumn(item,index)
                        }),),);
              }
            } 
          }
        }
    }
    return Container();
  },)

这是我的点击按钮代码

Widget generateColumn(InwardResidentModel item,int index) => Column(
  children: <Widget>[
    Row(
      children: <Widget>[
        ...,...,Column(
          children: <Widget>[
            if (item.payment_status_id == "1")
              MaterialButton(
                onPressed: () {},minWidth: 50,height: 25,padding: EdgeInsets.all(8),child: Text("Paid ₹${item.amount}",style: TextStyle(
                        color: Colors.green,fontSize: 13)),if (item.payment_status_id != "1")
              MaterialButton(
                onPressed: () {
                  setState(() {
                    inwardResidentModelList[index] = InwardResidentModel.fromJson({'payment_status_id': "1"});
                    // inwardResidentModelList[inwardResidentModelList.indexWhere((element) => item.inward_user_id == item.inward_user_id)] = InwardResidentModel(payment_status_id: "1");
                  });
                },child: Text("Pay ₹${item.payable_amount}",style: TextStyle(
                        color: Colors.deepPurple,)
          ],)
      ],],);

用户模型类的外观

class InwardUserModel {
  final String inward_user_id;
  final String user_id;
  final String payment_status_id;
  final String first_name;
  final String middle_name;
  final String last_name;
  final String amount;


  InwardUserModel({
    this.inward_user_id,this.user_id,this.payment_status_id,this.first_name,this.middle_name,this.last_name,this.amount,});

  factory InwardUserModel.fromJson(Map<String,dynamic> parsedJson) {
    return InwardUserModel(
        user_id: parsedJson['user_id'] ?? "",payment_status_id: parsedJson['payment_status_id'] ?? "",first_name: parsedJson['first_name'] ?? "",middle_name: parsedJson['middle_name'] ?? "",last_name: parsedJson['last_name'] ?? "",amount: parsedJson["amount"] ?? "",);
  }
}

enter image description here

如何在不重新加载完整ListView的情况下分别更新每个项目以显示新标题并通过单击正确更改颜色?任何帮助表示赞赏。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)