Firebase 颤振:使用数据库中的字符串过滤数据

Firebase 颤振:使用数据库中的字符串过滤数据,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,**我想使用shortInfo查看数据** 当用户单击“添加到购物车”按钮时,我在列表中存储了shortInfo字符串。 shortInfo存储在firestore的用户集合中 这是我的购物车页面,我使用shortinfo显示我的购物车项目 import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:e_shop/Config/config.dart'; import 'package:e_shop/Address

**我想使用shortInfo查看数据**

当用户单击“添加到购物车”按钮时,我在列表中存储了shortInfo字符串。 shortInfo存储在firestore的用户集合中

这是我的购物车页面,我使用shortinfo显示我的购物车项目

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:e_shop/Config/config.dart';
import 'package:e_shop/Address/address.dart';
import 'package:e_shop/Widgets/customAppBar.dart';
import 'package:e_shop/Widgets/loadingWidget.dart';
import 'package:e_shop/Models/item.dart';
import 'package:e_shop/Counters/cartitemcounter.dart';
import 'package:e_shop/Counters/totalMoney.dart';
import 'package:e_shop/Widgets/myDrawer.dart';
import 'package:e_shop/Widgets/searchBox.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:e_shop/Store/storehome.dart';
import 'package:provider/provider.dart';
import '../main.dart';

class CartPage extends StatefulWidget {
//   @override
//   _CartPageState createState() => _CartPageState();
// }
  final ItemModel itemModel;
  CartPage({this.itemModel});
  @override //edit by ut
  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {


  // @override
  // Widget build(BuildContext context) {
  //   return Scaffold(
  //   );
  // }
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(

          // flexibleSpace: Container(
          //   decoration: new BoxDecoration( //gradient color
          //       gradient: new LinearGradient(
          //         colors: [Colors.white, Colors.redAccent],
          //         begin: const FractionalOffset(0.0, 0.0),
          //         end: const FractionalOffset(0.0, 0.0),
          //         stops: [0.0, 1.0],
          //         tileMode: TileMode.clamp,
          //       )
          //   ),
          // ),
          backgroundColor: Colors.red,
          title: Text(
            "Saved Services",
            style: TextStyle(fontSize: 30.0, color: Colors.white, fontFamily:"Signatra"),
          ),
        ),
        drawer: MyDrawer(),
        body: CustomScrollView(
          slivers: [
            SliverPersistentHeader(pinned: true, delegate: SearchBoxDelegate()),   //search box
            StreamBuilder<QuerySnapshot>(     //
              stream: Firestore.instance.collection("items").limit(15).orderBy("publishedDate", descending: false).snapshots(),
              builder: (context, dataSnapshot)
              {
                return !dataSnapshot.hasData
                    ? SliverToBoxAdapter(child: Center(child: circularProgress(),),)
                    : SliverStaggeredGrid.countBuilder(
                  crossAxisCount: 1,
                  staggeredTileBuilder: (c) => StaggeredTile.fit(1),
                  itemBuilder: (context, index)
                  {
                    ItemModel model = ItemModel.fromJson(dataSnapshot.data.documents[index].data);
                    return cartInfo(model, context);
                  },
                  itemCount: dataSnapshot.data.documents.length,
                );

              },
            ),
          ],
        ),
      ),
    );
  }


  //edit ut

}

Widget cartInfo(ItemModel model, BuildContext context,
    {Color background, removeCartFunction}) {
  return InkWell(
    onTap: ()
    {
      Route route = MaterialPageRoute(builder: (_) => CartPage(itemModel: model));
      Navigator.pushReplacement(context, route);
    },
    splashColor: Colors.pink,
    child: Padding(
      padding: EdgeInsets.all(6.0),
      child: Container(
        height: 190.0,
        width: width,
        child: Row(
          children: [
            Image.network(model.thumbnailUrl, width: 140.0, height: 140.0,),
            SizedBox(width: 4.0,),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    child: Row(
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        Expanded(
                          child: Text(model.title, style: TextStyle(color: Colors.black, fontSize: 14.0),),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 5.0,),
                  Container(
                    child: Row(
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        Expanded(
                          child: Text(model.shortInfo, style: TextStyle(color: Colors.black54, fontSize: 12.0),),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 20.0,),
                  Row(
                    children: [
                      Container(
                        decoration: BoxDecoration(
                          shape: BoxShape.rectangle,
                          color: Colors.pink,
                        ),
                        alignment: Alignment.topLeft,
                        width: 40.0,
                        height: 43.0,
                        child: Center(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                "50%", style: TextStyle(fontSize: 15.0, color: Colors.white, fontWeight: FontWeight.normal),
                              ),
                              Text(
                                "Discount", style: TextStyle(fontSize: 10.0, color: Colors.white, fontWeight: FontWeight.normal),
                              ),
                            ],
                          ),
                        ),
                      ),
                      SizedBox(width: 10.0,),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(top: 0.0),
                            child: Row(
                              children: [
                                Text(
                                  r"Price: ₹ ",
                                  style: TextStyle(
                                    fontSize: 14.0,
                                    color: Colors.grey,
                                    decoration: TextDecoration.lineThrough,
                                  ),
                                ),
                                Text(
                                  (model.price + model.price).toString(),
                                  style: TextStyle(
                                    fontSize: 14.0,
                                    color: Colors.grey,
                                    decoration: TextDecoration.lineThrough,
                                  ),
                                ),
                              ],
                            ),

                          ),

                          Padding(
                            padding: EdgeInsets.only(top: 5.0),
                            child: Row(
                              children: [
                                Text(
                                  r"New Price: ",
                                  style: TextStyle(
                                    fontSize: 14.0,
                                    color: Colors.grey,
                                  ),
                                ),
                                Text(
                                  "₹ ",
                                  style: TextStyle(color: Colors.red, fontSize: 16.0),
                                ),
                                Text(
                                    (model.price).toString(),
                                    style: TextStyle(
                                      fontSize: 15.0,
                                      color: Colors.grey,
                                    )
                                ),
                              ],
                            ),
                          )


                        ],
                      )
                    ],
                  ),



                  // Flexible(
                  //   child: Container(),
                  // ),
                  // //to implment the cart item remove feature
                  Align(
                      alignment: Alignment.centerRight,
                      child:
                           IconButton(
                        icon: Icon(Icons.medical_services, color: Colors.black,),
                        onPressed: ()
                        {
                          showAlertDialog(context);
                        },
                      )

                  ),

                  Divider(
                    height: 5.0,
                    color: Colors.pinkAccent,
                  )

                ],
              ),
            ),
          ],
        ),
      ),
    ),
  );
}

showAlertDialog(BuildContext context) {
  // Create button
  Widget okButton = FlatButton(
    child: Text("OK"),
    onPressed: () {
      Navigator.of(context).pop();
    },
  );

  // Create AlertDialog
  AlertDialog alert = AlertDialog(
    title: Text("Request"),
    content: Text("Request Has Been Sent Please wait for Confirmation"),
    actions: [
      okButton,
    ],
  );

  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}
import'包:cloud_firestore/cloud_firestore.dart';
导入“package:e_shop/Config/Config.dart”;
导入“包装:e_shop/Address/Address.dart”;
导入“package:e_shop/Widgets/customAppBar.dart”;
导入“package:e_shop/Widgets/loadingWidget.dart”;
导入“包装:e_shop/Models/item.dart”;
导入“package:e_shop/Counters/cartitemcounter.dart”;
导入“包装:电子商店/柜台/totalMoney.dart”;
导入“package:e_shop/Widgets/myDrawer.dart”;
导入“package:e_shop/Widgets/searchBox.dart”;
进口“包装:颤振/材料.省道”;
导入“包:颤振交错网格视图/颤振交错网格视图.省道”;
进口“包装:fluttoast/fluttoast.dart”;
进口“包装:e_shop/Store/storehome.dart”;
导入“包:provider/provider.dart”;
导入“../main.dart”;
类CartPage扩展StatefulWidget{
//@覆盖
//_CartPageState createState()=>_CartPageState();
// }
最终项目模型项目模型;
CartPage({this.itemModel});
@覆盖//按ut编辑
_CartPageState createState()=>\u CartPageState();
}
类_CartPageState扩展状态{
//@覆盖
//小部件构建(构建上下文){
//返回脚手架(
//   );
// }
@凌驾
小部件构建(构建上下文){
返回安全区(
孩子:脚手架(
appBar:appBar(
//flexibleSpace:容器(
//装饰:新框装饰(//渐变色
//渐变:新的线性渐变(
//颜色:[颜色。白色,颜色。红色口音],
//开始:常量分数偏移(0.0,0.0),
//结束:常数分馏偏移(0.0,0.0),
//停止:[0.0,1.0],
//tileMode:tileMode.clamp,
//       )
//   ),
// ),
背景颜色:Colors.red,
标题:正文(
“保存的服务”,
样式:TextStyle(fontSize:30.0,颜色:Colors.white,fontFamily:“Signatra”),
),
),
抽屉:MyDrawer(),
正文:自定义滚动视图(
条子:[
SliverPersistentHeader(pinted:true,delegate:SearchBoxDelegate()),//搜索框
StreamBuilder(//
stream:Firestore.instance.collection(“items”).limit(15).orderBy(“publishedDate”,降序:false).snapshots(),
生成器:(上下文,数据快照)
{
return!dataSnapshot.hasData
?SliverToBoxAdapter(子对象:中心(子对象:循环过程(),),)
:SliverStaggeredGrid.countBuilder(
交叉轴计数:1,
交错文件生成器:(c)=>交错文件.fit(1),
itemBuilder:(上下文,索引)
{
ItemModel model=ItemModel.fromJson(dataSnapshot.data.documents[index].data);
返回cartInfo(模型、上下文);
},
itemCount:dataSnapshot.data.documents.length,
);
},
),
],
),
),
);
}
//编辑ut
}
Widget cartInfo(ItemModel模型、BuildContext上下文、,
{彩色背景,removeCartFunction}){
回墨槽(
onTap:()
{
Route Route=MaterialPage路线(生成器:()=>CartPage(itemModel:model));
pushReplacement(上下文、路线);
},
颜色:颜色。粉红色,
孩子:填充(
填充:所有边缘设置(6.0),
子:容器(
身高:190.0,
宽度:宽度,
孩子:排(
儿童:[
Image.network(model.thumbnailUrl,宽度:140.0,高度:140.0,),
尺寸控制盒(宽度:4.0,),
扩大(
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
孩子:排(
mainAxisSize:mainAxisSize.max,
儿童:[
扩大(
子项:文本(model.title,style:TextStyle(颜色:Colors.black,fontSize:14.0)),
),
],
),
),
尺寸箱(高度:5.0,),
容器(
孩子:排(
mainAxisSize:mainAxisSize.max,
儿童:[
扩大(
子项:文本(model.shortInfo,style:TextStyle(颜色:Colors.black54,fontSize:12.0),
),
],
),
),
尺寸箱(高度:20.0,),
划船(
儿童:[
容器(
装饰:盒子装饰(
形状:BoxShape.rectangle,
颜色:颜色。粉红色,
),
对齐:alignment.topLeft,
宽度:40.0,
身高:43.0,
儿童:中心(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“50%”,样式:TextStyle(fontSize:15.0,颜色:Colors.white,fontWeight:fontWeight.normal),
),
正文(
“折扣”,样式:TextStyle(fontSize:10.0,颜色:Colors.white,fontWeight:fontWeight.normal),
),
],
import 'package:cloud_firestore/cloud_firestore.dart';

class ItemModel {
  String title;
  String shortInfo;
  Timestamp publishedDate;
  String thumbnailUrl;
  String longDescription;
  String status;
  int price;

  ItemModel(
      {this.title,
        this.shortInfo,
        this.publishedDate,
        this.thumbnailUrl,
        this.longDescription,
        this.status,
        });

  ItemModel.fromJson(Map<String, dynamic> json) {
    title = json['title'];
    shortInfo = json['shortInfo'];
    publishedDate = json['publishedDate'];
    thumbnailUrl = json['thumbnailUrl'];
    longDescription = json['longDescription'];
    status = json['status'];
    price = json['price'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;
    data['shortInfo'] = this.shortInfo;
    data['price'] = this.price;
    if (this.publishedDate != null) {
      data['publishedDate'] = this.publishedDate;
    }
    data['thumbnailUrl'] = this.thumbnailUrl;
    data['longDescription'] = this.longDescription;
    data['status'] = this.status;
    return data;
  }
}

class PublishedDate {
  String date;

  PublishedDate({this.date});

  PublishedDate.fromJson(Map<String, dynamic> json) {
    date = json['$date'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['$date'] = this.date;
    return data;
  }
}
addItemToCart(String shortInfoAsID, BuildContext context)
{
  List tempCartList = AmbulanceApp.sharedPreferences.getStringList(AmbulanceApp.userCartList);
  tempCartList.add(shortInfoAsID);

  AmbulanceApp.firestore.collection(AmbulanceApp.collectionUser)
  .document(AmbulanceApp.sharedPreferences.getString(AmbulanceApp.userUID))
  .updateData({
    AmbulanceApp.userCartList: tempCartList,
  }).then((v){
    Fluttertoast.showToast(msg: "Item Added to Quick Access successfully");

    AmbulanceApp.sharedPreferences.setStringList(AmbulanceApp.userCartList, tempCartList);

    Provider.of<CartItemCounter>(context, listen: false).displayResult();

  });
}