我需要根据用户选择(例如在过滤器部分中)更新我的 Firestore 查询,但查询不会更改且列表不会更新

问题描述 投票:0回答:1

通过选择一个选项并提交,我更改了排序值,然后我使用所选值调用 getDataDb 函数,但它不起作用

showModalBottomSheet(
                  isScrollControlled: true, // required for min/max child size
                  context: context,
                  builder: (ctx) {
                    return  MultiSelectBottomSheet(
                      title:
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text("Select Category for filtering",textAlign: TextAlign.center,),
                            ],
                          ),
                      // Center(
                      //   child:
                      //   Text("Select Category for filtering",textAlign: TextAlign.center,),
                      // ),
                      listType: MultiSelectListType.CHIP,
                      items: _items,
                      initialValue: [],
                      onConfirm: (values) {
                        setState(() {
                          sort = "yes";
                        });
                        _getDataDb(sort);
                          setState(() {
                          });
                      },
                      maxChildSize: 0.8,
                    );
                  },
                );

这是完整代码

class RecentPost extends StatefulWidget {
  const RecentPost({super.key});

  @override
  State<RecentPost> createState() =>
      _RecentPostState();
}

class Cat {
  final int id;
  final String name;

  Cat({
    required this.id,
    required this.name,
  });
}
class _RecentPostState
    extends State<RecentPost> {
  Query _query =  FirebaseFirestore.instance.collection('Posts');
  String sort = "";
  void initState() {
    super.initState();
    _getDataDb(sort);
  }

  final _textController = TextEditingController();
  static List<Cat> _cats = [
    Cat(id: 1, name: "Vehicles"),
    Cat(id: 2, name: "Mobile"),
    Cat(id: 3, name: "Property"),
    Cat(id: 4, name: "Electrics"),
    Cat(id: 5, name: "Bikes"),
    Cat(id: 6, name: "Services"),
    Cat(id: 7, name: "Fashion"),
    Cat(id: 8, name: "Appliances"),
    Cat(id: 9, name: "Stores"),
    Cat(id: 10, name: "Rickshaw"),
  ];
  final _items = _cats
      .map((cats) => MultiSelectItem<Cat>(cats, cats.name))
      .toList();
  @override
   _getDataDb(sort) async {
    if(sort == "yes"){
      _query = await _query.orderBy('price',descending: false);
    }
    else{
      _query = await _query.orderBy('publish_date',descending: true);
    }
    // return _query;
  }

  Widget build(BuildContext context) {
    return Scaffold(
      drawerEnableOpenDragGesture: false,
      appBar: AppBar(

        actions:[
          Builder(builder: (context) =>
        Padding(padding: EdgeInsets.only(right: 10),
          child:
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Text("Sort",style: TextStyle(fontSize: 12,fontWeight: FontWeight.bold),),
              SizedBox(width: 5,),
              IconButton(onPressed: (){
                showModalBottomSheet(
                    context: context,
                    backgroundColor: Colors.transparent,
                    builder: (BuildContext bc) {
                      return Container(
                        width: 100,
                        color: Colors.transparent,
                        child: Padding(
                          padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
                          child: Container(
                            color: Colors.white,
                            width: 100,
                            child: new Wrap(
                              children: <Widget>[
                                Padding(
                                  padding: EdgeInsets.only(
                                      top: 20,
                                      left: 20,
                                      bottom: 10,
                                      right: 20),
                                  child: Row(children: <Widget>[
                                    Expanded(child: Divider()),
                                    SizedBox(
                                      width: 10,
                                    ),
                                    Text(
                                      "Sort list",
                                      style: TextStyle(
                                          fontWeight: FontWeight.bold),
                                    ),
                                    SizedBox(
                                      width: 10,
                                    ),
                                    Expanded(child: Divider()),
                                  ]),
                                ),
                                new ListTile(
                                  leading:
                                  Wrap(
                                    spacing: 0, // space between two icons
                                    children: <Widget>[
                                      Icon(Icons.arrow_downward_sharp,size: 20,color: Colors.indigoAccent,), // icon-1
                                      Icon(Icons.attach_money_sharp,size: 20,color: Colors.indigoAccent,), // icon-2
                                    ],
                                  ),
                                  // new Icon(Icons.p,
                                  //     color: Colors.indigoAccent),
                                  title: new Text(
                                    'Lowest price',
                                    style: TextStyle(fontSize: 14),
                                  ),
                                  onTap: () => {
                                  },
                                ),
                                new ListTile(
                                  leading:
                                  Wrap(
                                    spacing: 0, // space between two icons
                                    children: <Widget>[
                                      Icon(Icons.arrow_upward_sharp,size: 20,color: Colors.indigoAccent,), // icon-1
                                      Icon(Icons.attach_money_sharp,size: 20,color: Colors.indigoAccent,), // icon-2
                                    ],
                                  ),
                                  title: new Text(
                                    'Highest price',
                                    style: TextStyle(fontSize: 14),
                                  ),
                                  onTap: () => {
                                  },
                                ),
                              ],
                            ),
                          ),
                        ),
                      );
                    });
              },
                  icon: Icon(Icons.sort,size: 20,),),
              SizedBox(width: 10,),
              Text("|",style: TextStyle(fontSize: 12,fontWeight: FontWeight.bold),),
              SizedBox(width: 15,),
              Text("Filter",style: TextStyle(fontSize: 12,fontWeight: FontWeight.bold),),
              SizedBox(width: 5,),
              IconButton(onPressed: (){
                showModalBottomSheet(
                  isScrollControlled: true, // required for min/max child size
                  context: context,
                  builder: (ctx) {
                    return  MultiSelectBottomSheet(
                      title:
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text("Select Category for filtering",textAlign: TextAlign.center,),
                            ],
                          ),
                      // Center(
                      //   child:
                      //   Text("Select Category for filtering",textAlign: TextAlign.center,),
                      // ),
                      listType: MultiSelectListType.CHIP,
                      items: _items,
                      initialValue: [],
                      onConfirm: (values) {
                        setState(() {
                          sort = "yes";
                        });
                        _getDataDb(sort);
                          setState(() {
                          });
                      },
                      maxChildSize: 0.8,
                    );
                  },
                );
              },
                icon: Icon(Icons.filter_alt_outlined,size: 20,),),
            ],
          )
          ))],

      ),

      body:

      Column(
        children: [
          Expanded(
            child:
            FirestorePagination(
              viewType: ViewType.list,
              bottomLoader: const Center(
                child: CircularProgressIndicator(
                  strokeWidth: 3,
                  color: Colors.blue,
                ),
              ),
              isLive: false,
              scrollDirection: Axis.vertical,
              limit: 10,
              query: _query,
              itemBuilder: (context, documentSnapshot, index) {
                final data = documentSnapshot.data() as Map<String, dynamic>;
                return
                  Card(
                    // clipBehavior: Clip.antiAlias,
                    child:
                    InkWell(
                      onTap: (){

                      },
                      child:
                      Container(
                        width: 100,
                        height: 100,
                        padding: const EdgeInsets.only(left: 10),
                        child: Row(children: [
                          Container(
                              width: 100,
                              height: 80,
                              child:
                              ClipRRect(

                                borderRadius: BorderRadius.only(topRight: Radius.circular(15.0),topLeft: Radius.circular(15.0),bottomRight: Radius.circular(15.0),bottomLeft: Radius.circular(15.0)),
                                child:

                                CachedNetworkImage(
                                  imageUrl: data['photo_0'],
                                  imageBuilder: (context, imageProvioder) {
                                    return Container(
                                      decoration: BoxDecoration(
                                        image: DecorationImage(image: imageProvioder, fit: BoxFit.cover),
                                      ),
                                    );
                                  },
                                  placeholder: (context, url) => Container(
                                    height: 250,
                                    width: 164,
                                    child: Center(
                                      child: CircularProgressIndicator(
                                        color: Colors.green,
                                      ),
                                    ),
                                  ),
                                  errorWidget: (context, url, error) => Icon(Icons.error),
                                ),
                              )
                          ),
                          Spacer(
                            flex: 1,
                          ),
                          Expanded(
                            flex: 30,
                            child: Container(
                              padding: const EdgeInsets.only(top: 5),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: <Widget>[
                                  Text("Rs " +
                                      data['price'].toString(),
                                      style: TextStyle(
                                        fontSize: 14.0,
                                        fontWeight: FontWeight.w700,
                                      )),
                                  SizedBox(height: 5,),
                                  Row(
                                    children: <Widget>[
                                      Flexible(child:
                                      Text(
                                        data['title'],overflow: TextOverflow.ellipsis,
                                        style: TextStyle(fontSize: 12,fontWeight: FontWeight.w600),
                                      )),

                                    ],
                                  ),
                                  SizedBox(height: 5,),
                                  Row(
                                    children: <Widget>[

                                      Text(
                                        data['address'],
                                        style: TextStyle(fontSize: 10),
                                      ),


                                    ],
                                  ),
                                  SizedBox(height: 5,),
                                  Row(
                                    children: [
                                      Icon(Icons.watch_later_outlined, size: 10,),
                                      SizedBox(width: 5,),
                                      Text(ymwdhms(data['publish_date'].toDate()), style: TextStyle(
                                          fontSize: 8,
                                          fontWeight: FontWeight.bold),),
                                    ],
                                  )


                                ],
                              ),
                            ),
                          ),
                        ]),
                      ),

                    ),
                  );
              },
            ),
          ),

        ],
      ),
    );
  }
}
flutter firebase sorting filter
1个回答
0
投票

我相信您的值未从

showModalBottomSheet
更新,请尝试使用
StatefulBuilder
内部的
showModalBottomSheet
更新值并尝试再次运行

© www.soinside.com 2019 - 2024. All rights reserved.