Flutter如何更新从另一个类传递的Firebase值

时间:2019-03-12 16:52:06

标签: firebase flutter google-cloud-firestore

我是新手,但我想在单击按钮时增加Firestore的值。我有一个流生成器,可以获取我在第一堂课上需要的数据。并将其传递给第二类(增量按钮在此处)。有没有一种方法可以更新第二个类上的值,还是我应该在第一个类上构建另一个“ Widget build”以在第二个类上构建显示(列表图块)?

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

class Cart_products extends StatefulWidget {
  Cart_products({this.userId});
  final userId;
  @override
  _Cart_productsState createState() => _Cart_productsState();
}

class _Cart_productsState extends State<Cart_products> {
  @override
  Widget build(BuildContext context) {
    return new StreamBuilder(
      stream: Firestore.instance.collection('cart').where('userId', isEqualTo: "${widget.userId}").snapshots(),
      builder: (context, snapshot) {

        if (!snapshot.hasData) return const Text('Loading....');
        return ListView.builder(
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) => Single_cart_product(
              cart_prod_amount: snapshot.data.documents[index]['price'] * snapshot.data.documents[index]['Quantity'],
              cart_price: snapshot.data.documents[index]['price'],
              cart_prod_picture: snapshot.data.documents[index]['image'],
              cart_prod_quantity: snapshot.data.documents[index]['Quantity'],
              cart_prod_name: snapshot.data.documents[index]['product'],
              userId: widget.userId,
              //cart_prod_sum_quantity: snapshot.data.documents[index]['Quantity'] += snapshot.data.documents[index]['Quantity'],
            ));
      },
    );
  }
}


class Single_cart_product extends StatefulWidget {
  final cart_prod_name;
  final cart_prod_picture;
  final cart_price;
  final cart_prod_quantity;
  final cart_prod_amount;
  final cart_prod_id;
  final cart_prod_sum_quantity;
  final userId;



  Single_cart_product({
    this.cart_prod_name,
    this.cart_prod_picture,
    this.cart_price,
    this.cart_prod_quantity,
    this.cart_prod_amount,
    this.cart_prod_id,
    this.cart_prod_sum_quantity,
    this.userId,
  });
  @override
  _Single_cart_productState createState() => _Single_cart_productState();
}

class _Single_cart_productState extends State<Single_cart_product> {


  @override
  Widget build(BuildContext context) {
    return Card(
      child: ListTile(
        //============LEAFING SECTION =============
        leading: new Image.network(widget.cart_prod_picture, width: 80.0, height: 80.0,),
        //============TITLE SECTION ===============
        title: new Text(widget.cart_prod_name),
        //============SUBTITLE SECTION ===========
        subtitle: new Column(
          children: <Widget>[
            //ROW INSIDE COLUMN
            new Row(
              children: <Widget>[
                //this is for the SIZE OF THE PRODUCT
                Padding(
                  padding: const EdgeInsets.fromLTRB(0.0, 8.0, 8.0, 8.0),
                  child: new Text("Quantity:"),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: new Text(
                    "${widget.cart_prod_quantity}",
                    style: TextStyle(color: Colors.red),
                  ),
                ),
// ================ this section for the amount ================//
                new Padding(
                  padding: const EdgeInsets.fromLTRB(20.0, 8.0, 8.0, 8.0),
                  child: new Text("Price :"),
                ),
                Padding(
                  padding: const EdgeInsets.all(4.0),
                  child: new Text(
                    "${widget.cart_price}",
                    style: TextStyle(color: Colors.red),
                  ),
                )
              ],
            ),
            new Container(
              alignment: Alignment.topLeft,
              child: new Text("Total Amount: P ${widget.cart_prod_amount}",
                style: TextStyle(fontSize: 17.0,
                    fontWeight: FontWeight.bold,
                    color: Colors.red),),
            ),

          ],
        ),
        trailing: new Column(
          children: <Widget>[
            new IconButton(icon: Icon(Icons.arrow_drop_up), 
  

onPressed:(){                     int _Quantity = widget.cart_prod_quantity + 1;                    Firestore.instance.document(widget.cart_prod_quantity).updateData({'Quantity:'   :_Quantity});                   })

            new Text("${widget.cart_prod_quantity}"),
            new IconButton(icon: Icon(Icons.arrow_drop_down), onPressed: () {})
          ],
        ),
      ),
    );
  }
}

0 个答案:

没有答案