【问题标题】:Display the below data in List widget flutter and search it在 List widget flutter 中显示以下数据并搜索它
【发布时间】:2022-11-19 20:29:42
【问题描述】:

我有以下 API 数据在列表视图中显示它并尝试在月份名称(日期)中搜索过滤器

[
   {
  "transGroup": 1,
  "transType": 0,
  "serviceTypeId":85,
   },
   {
  "transGroup": 2,
  "transType": 9,
  "serviceTypeId": 78,
 }
]

搜索过滤器

void searchFilter(String enteredKeyword) {
    List results = [];
    if (enteredKeyword.isEmpty) {
      results = myData;
    } else {
      results = myData
          .where(
            (user) => user["date"].toString().contains(
                  enteredKeyword.toLowerCase(),
                ),
          )
          .toList();
    }
  }

文本域小工具

  TextFormField(
          onChanged: (value) => searchFilter(value),
          decoration: const InputDecoration(
            border: OutlineInputBorder(),
          ),
        ),

初始状态:

 List _foundUsers = [];
  @override
  initState() {
    _foundUsers = myData;
    super.initState();
  }

我的结果->

【问题讨论】:

  • 我没有明白你的问题,你能解释更多吗?

标签: flutter flutter-layout


【解决方案1】:

对于你的第二个问题,让我们假设这是你的列表:

var myData = [
    {
      "transGroup": 1,
      "transType": 0,
      "serviceTypeId": 0,
      "serviceDescription": "Opening Balance",
      "financialYear": "2022/2023",
      "july": 54818.34,
      "august": 54818.34,
      "september": 0,
      "october": 0,
      "november": 0,
      "december": 0,
      "january": 0,
      "febuary": 0,
      "march": 0,
      "april": 0,
      "may": 0,
      "june": 0
    },
    {
      "transGroup": 990,
      "transType": 0,
      "serviceTypeId": 0,
      "serviceDescription": "Closing Balance",
      "financialYear": "2022/2023",
      "july": 54818.34,
      "august": 54818.34,
      "september": 0,
      "october": 0,
      "november": 0,
      "december": 0,
      "january": 0,
      "febuary": 0,
      "march": 0,
      "april": 0,
      "may": 0,
      "june": 0
    }
  ];

您需要定义此类模型:

class BalanceModel {
  final String date;
  final double opening;
  final double closing;
  final double receipts;

  BalanceModel({
    required this.date,
    required this.opening,
    required this.closing,
    required this.receipts,
  });

  static const List months = [
    "january",
    "febuary",
    "march",
    "april",
    "may",
    "june",
    "july",
    "august",
    "september",
    "october",
    "november",
    "december"
  ];

  static List<BalanceModel> getData(List data) {
    List<BalanceModel> result = [];
    for (var month in months) {
      double openValue = 0;
      double closeValue = 0;

      var year = '';

      for (var e in data) {
        if (e['serviceDescription'] == "Opening Balance") {
          var rawYear = e['financialYear'] as String;
          year = rawYear.substring(0, rawYear.lastIndexOf('/'));
          openValue =
              e[month] is int ? double.parse(e[month].toString()) : e[month];
        } else if (e['serviceDescription'] == "Closing Balance") {
          closeValue =
              e[month] is int ? double.parse(e[month].toString()) : e[month];
        }
      }
      result.add(BalanceModel(
          date: "$month $year",
          opening: openValue,
          closing: closeValue,
          receipts: closeValue - openValue));
    }

    return result;
  }
}

您可以像这样将列表传递给它:

var result = BalanceModel.getData(myData);

然后使用 listview 中的 result 列表并基于它创建您的卡片小部件。

【讨论】:

  • 你好@eamirho3ein 我已经尝试在列表中添加搜索栏但没有找到合适的解决方案你能帮助我吗我已经更新了我的问题或者你也可以尝试你自己的搜索功能
  • 我不明白你的新问题是什么?
猜你喜欢
  • 2020-02-11
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
相关资源
最近更新 更多