使用Streambuilder颤振排序数据Firestore

时间:2019-09-21 21:35:11

标签: flutter google-cloud-firestore

我的目标:当用户按下“ _mainListItem”中的“列表”按钮时,我希望列表视图按orderBy排序。以及在屏幕上更新

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';


class mainlist extends StatefulWidget {
  @override
  _mainlistpage createState() => _mainlistpage();
}

class _mainlistpage extends State<mainlist> {


  Widget homePage() {
    return StreamBuilder(
      stream: Firestore.instance.collection("Test").snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return Text("Loading");
        return ListView.builder(
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) =>
                _mainListItem(context, snapshot.data.documents[index]));
      },
    );
  }


  Widget _mainListItem(BuildContext context, DocumentSnapshot document) {
    return Card(
      color: Colors.white,
      child: InkWell(
        onTap: () {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => profile(context, document)));
        },
        child: Container(
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Container(
                decoration: BoxDecoration(
                    border: Border(bottom: BorderSide(color: Colors.black12))),
                child: Row(
                  children: [
                    Expanded(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Column(
                            children: <Widget>[
                              Stack(
                                  alignment: Alignment.topRight,
                                  children: <Widget>[
                                    Padding(
                                      padding: const EdgeInsets.only(right: 5),
                                      child: ClipRRect(
                                        borderRadius:
                                            BorderRadius.circular(0.0),
                                        child: FittedBox(
                                            child: Image.asset(
                                          "assets/Profile Picture.png",
                                          fit: BoxFit.fill,
                                        )),
                                      ),
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.only(
                                          top: 7, right: 4),
                                      child: Text(
                                        'Test',
                                        style: TextStyle(fontSize: 12),
                                      ),
                                    ),
                                  ]),
                              Row()
                            ],
                          ),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Text(
                                    document['name'],
                                  ),
                                  // Text("2km"),
                                ],
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              Column(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(top: 5, bottom: 5),
                    child: Row(
                      children: <Widget>[
                        Padding(
                          padding: const EdgeInsets.only(left: 10, right: 7),
                          child: Container(
                            child: Material(
                              borderRadius: BorderRadius.circular(5),
                              shadowColor: Colors.black,
                              elevation: 1,
                              child: SizedBox(
                                  height: 28,
                                  width: 68,
                                  child: IconButton(
                                    padding: EdgeInsets.only(bottom: 10),
                                    **icon: Icon(Icons.list),
                                    disabledColor: Colors.blue,
                                    iconSize: 25,**
                                  )),
                            ),
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              )
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(
        backgroundColor: Colors.grey,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          color: Colors.red,
        ),
        title: Text("Test"),
        centerTitle: true,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.menu),
            iconSize: 30,
            color: Colors.white,
          )
        ],
      ),
      body: homePage(),
    );
  }
}

我尝试过 -将streambuilder函数添加到列表按钮中的ontapped: -阅读并观看了每个视频,仍然找不到解决方法

注意:该应用看上去很奇怪,因为我删除了不必要的信息

3 个答案:

答案 0 :(得分:1)

您可以在快照方法之前对列表项进行排序,例如:

.orderBy('sortField', descending: true).snapshot()

我希望这对您有用。

答案 1 :(得分:0)

尝试将值映射到const qrcode = window.qrcode; const video = document.createElement("video"); const canvasElement = document.getElementById("qr-canvas"); const canvas = canvasElement.getContext("2d"); const qrResult = document.getElementById("qr-result"); const outputData = document.getElementById("outputData"); const btnScanQR = document.getElementById("btn-scan-qr"); let scanning = false; qrcode.callback = res => { if (res) { outputData.innerText = res; scanning = false; video.srcObject.getTracks().forEach(track => { track.stop(); }); qrResult.hidden = false; canvasElement.hidden = true; btnScanQR.hidden = false; } }; btnScanQR.onclick = () => { navigator.mediaDevices .getUserMedia({ video: { facingMode: "environment" } }) .then(function(stream) { scanning = true; qrResult.hidden = true; btnScanQR.hidden = true; canvasElement.hidden = false; video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen video.srcObject = stream; video.play(); tick(); scan(); }); }; function tick() { canvasElement.height = video.videoHeight; canvasElement.width = video.videoWidth; canvas.drawImage(video, 0, 0, canvasElement.width, canvasElement.height); scanning && requestAnimationFrame(tick); } function scan() { try { qrcode.decode(); } catch (e) { setTimeout(scan, 300); } } 并在列表视图中使用对象列表。

答案 2 :(得分:0)

我建议您使用状态来确定列表的排序方式。 这就是我要做的(从相同的代码继续):

...
class _mainlistpage extends State<mainlist> {
  String _orderBy = 'defaultSort'; //? HERE YOU PUT WHAT YOUR SORTING FIELD NAME IS
  bool _isDescending = true; //? THIS IS WHAT WILL SET THE ORDER SORTING

  Widget homePage() {
    return StreamBuilder(
      stream: Firestore.instance
        .collection("Test")
        .orderBy(_orderBy, descending: _isDescending) //? PUT THE ORDERBY QUERY HERE
        .snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return Text("Loading");
        return ListView.builder(
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) =>
                _mainListItem(context, snapshot.data.documents[index]));
      },
    );
  }
...

在类中的某个位置,放置按钮或下拉菜单,然后使用setState(...)设置 新变量的状态。

注意:您可能必须在Firestore中创建“索引”。新的时候会出现错误 索引是必需的。