flutter - Flutter:如何按时间和日期对卡片列表进行排序

标签 flutter dart

我在排序卡片 list 时遇到麻烦。每次保存数据时,数据都会以卡的形式输出到我的应用中。它包含名称,时间,日期,图片等。问题是它没有按时间和日期排序。它只是添加和添加。它甚至不堆叠,仅添加到列表中的任何位置。顺便说一句,我通过StreamBuilder获得此数据。谁能帮我解决这个问题?谢谢。

我的StreamBuilder代码:

StreamBuilder<QuerySnapshot>(
        stream: db.collection('HELP REQUEST').where('Type_OfDisaster', isEqualTo: '[Drought]').snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Column(
                children: snapshot.data.documents
                    .map((doc) => buildItem(doc))
                    .toList());
          }
          else {
            return Container(
                child: Center(
                    child: CircularProgressIndicator()
                )
            );
          }
        });

我在卡中输出数据的位置:
Container buildItem(DocumentSnapshot doc) {
  return Container(
   child: Padding(
    padding: const EdgeInsets.only(left: 20.0, right: 20.0, bottom: 15.0),
    child: Card(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(15),
      ),
      color: Color(0xFFFFFFFF),
      elevation: 5,
      child: Padding(
        padding: const EdgeInsets.only(top: 20, left: 20, right: 20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Row(
                  children: <Widget> [
                    picture(),
                    SizedBox(
                      width: 10,
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Text(
                              '${doc.data['Name_ofUser']}',
                              style: TextStyle(
                                  fontSize: 11.5,
                                  fontWeight: FontWeight.w800),
                            ),
                          ],
                        ),
                           Row(
                          children: <Widget>[
                            Text(doc.data['Help_DatePosted'],
                              style: TextStyle(
                                  fontSize: 9,
                                  fontWeight: FontWeight.w800
                              ),
                            ),
                          ],
                        )
                      ],
                    ),
                    Visibility(
                      visible: orgstat,
                      child: Padding(
                        padding: const EdgeInsets.only(left: 8.0, bottom: 5.0),
                        child: Icon(
                          FontAwesomeIcons.checkCircle,
                          color: Colors.green,
                          size: 12,
                        ),
                      ),
                    )
                  ],
                ),
                   Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    CircleAvatar(
                        radius: 20,
                        backgroundImage: AssetImage('assets/Drought.jpg')
                    ),
                  ],
                ),
              ],
            ),
            Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  SizedBox(
                    height: 20,
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: Text('Description:', style: TextStyle(
                        fontSize: 11.5,
                        color:  Color(0xFF000000)
                    ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: GestureDetector(
                      onTap: (){
                        setState(() {

                        });
                      },
                      child: Text(
                        '${doc.data['Help_Description']}',
                        maxLines: 3,
                        overflow: TextOverflow.ellipsis,
                        textAlign: TextAlign.justify,
                        style: TextStyle(
                            fontSize: 13.5, fontWeight: FontWeight.w800,
                            color:  Color(0xFF000000)),
                      ),
                    ),
                  ),
                  Row(
                    children: <Widget>[
                      Text('Families affected:', style: TextStyle(
                          fontSize: 11.5,
                          color: Color(0xFF000000)
                      ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          '${doc.data['Help_FamiliesAffected']}',
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: 13.5, fontWeight: FontWeight.w800,
                              color:  Color(0xFF000000)),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Text('Area/Barangay:', style: TextStyle(
                          fontSize: 11.5,
                          color:  Color(0xFF000000)
                      ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          '${doc.data['Help_AreaAffected']}',
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: 13.5,
                              fontWeight: FontWeight.w800,
                              color:  Color(0xFF000000)
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Text('Things we need:',
                        style: TextStyle(
                            fontSize: 11.5,
                            color:  Color(0xFF000000)
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          '${doc.data['Help_ThingsNeeded']}'.replaceAll(new RegExp(r'[^\w\s\,]+'),''),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: 13.5,
                              fontWeight: FontWeight.w800,
                              color:  Color(0xFF000000)),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Text('Drop off location:',
                        style: TextStyle(
                            fontSize: 11.5,
                            color:  Color(0xFF000000)
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          '${doc.data['Help_DropoffLocation']}',
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: 13.5, fontWeight: FontWeight.w800),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Text('For inquiries call:',
                        style: TextStyle(
                            fontSize: 11.5,
                            color:  Color(0xFF000000)
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          '${doc.data['Help_Inquiry']}',
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: 13.5,
                              fontWeight: FontWeight.w800,
                              color:  Color(0xFF000000)
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 8.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(bottom: 10, top: 15),
                    child: Container(
                      height: _height * 0.05,
                      width: _width * 0.30,
                      child: Text(
                        '${doc.data['Respondents_Count'].toString()} respondents.',
                        style: TextStyle(
                          color:  Color(0xFF000000),
                          fontSize: 12,
                          fontWeight: FontWeight.w800,
                        ),
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(bottom: 10),
                    child: Container(
                      height: _height * 0.05,
                      width: _width * 0.30,
                      child: RaisedButton(
                        highlightColor: Color(0xFF02663B1),
                        shape: RoundedRectangleBorder(
                            borderRadius:
                            BorderRadius.all(Radius.circular(20.0))),
                        color: Color(0xFF3F6492),
                        onPressed: () async {
                          final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
                          final FirebaseUser user = await _firebaseAuth.currentUser();
                          if (user.isEmailVerified){
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (BuildContext context) => DroughtFeedBenefactorAnswerHelp(
                                        doc.data['Help_Description'],
                                        doc.data['Name_ofUser'],
                                        doc.data['User_ID'],
                                        doc.data['Help_DropoffLocation'],
                                        doc.data['Help_ThingsNeeded'],
                                        doc.data['Type_OfDisaster'],
                                        doc.data['Help_AreaAffected'],
                                        doc.data['Unique_ID'],
                                        widget.totalInterestedCount,
                                        widget.totalConfirmedhelpCount,
                                        doc.data['Respondents_Count']
                                    )
                                )
                            );
                          }else{
                            _showVerifyEmail(doc.data);
                          }
                        },
                        child: Text(
                          'I want to help',
                          style: TextStyle(
                              color: Color(0xFFFFFFFF),
                              fontSize: 12,
                              fontWeight: FontWeight.w800),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    ),
  ),
);
}

卡的用户界面:

enter image description here

最佳答案

您可以使用firestore提供的方法

https://firebase.google.com/docs/firestore/query-data/order-limit-data

db.collection('HELP REQUEST')
   .where('Type_OfDisaster', isEqualTo: '[Drought]')
   .orderBy('time', descending: true)
   .snapshots()

希望它可以帮助您;)

关于flutter - Flutter:如何按时间和日期对卡片列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59751542/

相关文章:

dart - 在抽象类中没有成员的意义是什么?

sqlite - 如何在 Flutter 应用程序中将 SQLite 数据导出到 CSV 文件

dart - Flutter - 未定义边界参数

ios - Flutter iOS版本因Pod文件: Podfile is out of date错误而失败

flutter - 是否可以在小部件中使用上下文?

flutter - 如何在flutter中清除应用程序的全部数据

json - 从 Firebase Datasnapshot 获取 JSON 键值

flutter - "WidgetsBinding.instance!.addPostFrameCallback((_) => yourFunction(context));"不执行

string - 如何在 Flutter 中将列表转换为字符串?

firebase - 数组的元素是固定长度的,而数组本身不是