【问题标题】:Flutter : Sort widget according to a sorted list of valuesFlutter:根据排序的值列表对小部件进行排序
【发布时间】:2019-02-24 14:07:20
【问题描述】:

我目前有一个应用程序可以显示 1.5 公里半径内的附近医院,它看起来像这样:

我遇到的麻烦是,我无法根据计算出的从最低到最高的距离对卡片进行排序。

我创建了一个List<double> doubleList = []; 来存储计算出的距离列表,并使用doubleList.sort(); 对其进行排序。

这里是sn-p的代码:

 return new ListView.builder(
                shrinkWrap: true,
                itemCount: jsonresponse.length,
                itemBuilder: (context, index){

                  String name = jsonresponse[index]["Name"];
                  String lat = jsonresponse[index]["Latitude"];
                  String lng = jsonresponse[index]["Longitude"];

                  var distance = new GreatCircleDistance.fromDegrees(latitude1: double.parse(widget.lat), longitude1: double.parse(widget.lng), latitude2: double.parse(lat), longitude2: double.parse(lng));

                  var totaldistance = distance.haversineDistance().toStringAsFixed(2);

                  double distanceDouble = double.parse(totaldistance);

                  if(distanceDouble < 1500.00){

                    doubleList.add(distanceDouble);

                    doubleList.sort((a, b){
                      return a.compareTo(b);
                    });

                    print(doubleList);

                    return new Column(
                      children: <Widget>[
                        new Card(
                          child: new ListTile(
                            title: new Text(name),
                            subtitle: new Text("$distanceDouble m away"),
                            trailing: new IconButton(
                                icon: new Icon(Icons.info),
                                onPressed: (){
                                  makeDialog(lat, lng);
                                }),
                          ),
                        )
                      ],
                    );
                  }else{
                    return new Container();
                  }
                });

这是打印时排序距离值的输出:

I/flutter ( 4286): [987.8]
I/flutter ( 4286): [987.8, 1014.87]
I/flutter ( 4286): [987.8, 1014.87, 1352.22]
I/flutter ( 4286): [128.26, 987.8, 1014.87, 1352.22]
I/flutter ( 4286): [128.26, 987.8, 1014.87, 1260.73, 1352.22]
I/flutter ( 4286): [122.91, 128.26, 987.8, 1014.87, 1260.73, 1352.22]
I/flutter ( 4286): [122.91, 128.26, 987.8, 1014.87, 1260.73, 1303.36, 1352.22]
I/flutter ( 4286): [122.91, 128.26, 987.8, 1014.87, 1232.47, 1260.73, 1303.36, 1352.22]
I/flutter ( 4286): [122.91, 128.26, 987.8, 1014.87, 1232.47, 1232.47, 1260.73, 1303.36, 1352.22]

我的问题是:

我如何确保小部件将遵循排序距离值的顺序?

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    你可以试试这样的

    jsonresponse.sort((a, b) {
      var distance1 = new GreatCircleDistance.fromDegrees(latitude1: double.parse(widget.lat), longitude1: double.parse(widget.lng), latitude2: double.parse(a.lat), longitude2: double.parse(a.lng));
      var totaldistance1 = distance1.haversineDistance().toStringAsFixed(2);
      double distanceDouble1 = double.parse(totaldistance1);
      var distance2 = new GreatCircleDistance.fromDegrees(latitude1: double.parse(widget.lat), longitude1: double.parse(widget.lng), latitude2: double.parse(b.lat), longitude2: double.parse(b.lng));
      var totaldistance2 = distance2.haversineDistance().toStringAsFixed(2);
      double distanceDouble2 = double.parse(totaldistance2);
      return (distanceDouble1 - distanceDouble2).toInt();
    });
    

    在返回新列之前(我设置为int,因为double不适合比较器)

    【讨论】:

    • 应该检查答案(y)
    • 您好,先生,我之前用过这种结算方式。你知道如何设置排序距离的最大半径吗? my question
    • 你可以查看答案
    【解决方案2】:

    ListView.builder 正在使用负责构建一个项目的 itemBuilder。请注意,每次构建项目时都要进行排序。

    您需要在调用List.builderjsonresponse[index] 之前对您的jsonresponse 进行排序,否则将不正确。

    【讨论】:

    • 感谢您对此事的投入,Hadrien。我会在调用 Listview.Builder 之前尝试对其进行排序。
    猜你喜欢
    • 1970-01-01
    • 2018-01-30
    • 2015-01-05
    • 2020-12-27
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2020-06-02
    相关资源
    最近更新 更多