flutter - 如何从颤动的地图中返回类内的变量
问题描述
如果条形码在地图内,我想返回变量名称和数量。
主页
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:scan/scan.dart';
import 'listviewz.dart';
import 'modelg.dart';
class Scan1a extends StatefulWidget {
const Scan1a({Key? key, this.qrcode1}) : super(key: key);
final qrcode1;
@override
_Scan1aState createState() => _Scan1aState(qrcode: qrcode1);
}
class _Scan1aState extends State<Scan1a> {
/// create a bottom drawer controller to control the drawer.
ScanController controller = ScanController();
String qrcode = '';
String barcode = '';
String name = 'Start Shopping';
String price = '';
int quantity = 0;
String weight = '';
_Scan1aState({required this.qrcode});
bool _chckfin = false;
List<productinfo> productaddinfo = [];
get index => index;
void chckpro(List<productinfo> productaddinfo, String barcode) {
final prochck = productaddinfo
.singleWhere((element) => element.barcode == barcode, orElse: null);
print('Using singleWhere: $prochck');
}
@override
Widget build(BuildContext context) {
final cart = Provider.of<Cart>(context);
final CollectionReference<void> products =
FirebaseFirestore.instance.collection(qrcode);
queryproduct() async {
return await products
.doc(barcode)
.get()
.then((DocumentSnapshot documentSnapshot) {
var name = documentSnapshot.get('name').toString();
print(name);
var price = documentSnapshot.get('price');
print(price);
var wieght = documentSnapshot.get('weight').toString();
print(wieght);
var quantity = documentSnapshot.get('quantity');
setState(() {
this.name = name;
this.price = price;
this.weight = wieght;
this.quantity = quantity;
});
});
}
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Hello'),
actions: [
MaterialButton(
child: Text('resume'),
onPressed: () {
controller.resume();
}),
IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) =>
Listviewz(productinfoadd1: productaddinfo)),
);
},
icon: Icon(Icons.shopping_cart))
],
),
body: Column(
children: [
Container(
width: 600,
height: 600,
child: ScanView(
controller: controller,
scanAreaScale: .7,
scanLineColor: Colors.green.shade400,
onCapture: (data) {
print(barcode);
setState(() {
barcode = data;
});
print(barcode);
queryproduct();
},
),
),
Builder(builder: (context){if (cart.items.containsKey(barcode)) {
return ProductCardExist(context,barcode);
} else {
return ProductCardnew(context);
}})
],
));
}
Widget ProductCardnew(context) {
final cart = Provider.of<Cart>(context);
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
color: const Color(0xffffffff),
boxShadow: [
BoxShadow(
color: const Color(0x29000000),
offset: Offset(0, 3),
blurRadius: 6,
),
],
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
children: [
SizedBox(
width: 40,
child: Text(
name,
textAlign: TextAlign.center,
),
)
],
),
Column(
children: [
SizedBox(
width: 30,
child: Text(
quantity.toString(),
textAlign: TextAlign.center,
),
)
],
),
Column(
children: [
SizedBox(
width: 15,
child: Text(
price,
textAlign: TextAlign.center,
),
)
],
),
Column(
children: [
Container(
color: Colors.green[700],
width: 30,
height: 30,
child: MaterialButton(
onPressed: () {
productaddinfo
.add(productinfo(barcode, name, price, quantity));
cart.addItem(barcode, name, price);
},
child: Icon(Icons.add),
))
],
),
],
),
);
}
Widget ProductCardExist(context,barcode) {
final cart = Provider.of<Cart>(context);
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
color: const Color(0xffffffff),
boxShadow: [
BoxShadow(
color: const Color(0x29000000),
offset: Offset(0, 3),
blurRadius: 6,
),
],
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
children: [
SizedBox(
width: 40,
child: Text(
name,
textAlign: TextAlign.center,
),
)
],
),
Column(
children: [
SizedBox(
width: 30,
child: Text(
quantity.toString(),
textAlign: TextAlign.center,
),
)
],
),
Column(
children: [
SizedBox(
width: 15,
child: Text(
price,
textAlign: TextAlign.center,
),
)
],
),
Column(
children: [
Container(
color: Colors.green[700],
width: 30,
height: 30,
child: MaterialButton(
onPressed: () {
productaddinfo
.add(productinfo(barcode, name, price, quantity));
},
child: Icon(Icons.add),
))
],
),
],
),
);
}
}
模型类
import 'dart:ffi';
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
class productinfo {
String barcode;
String name1;
String price1;
int quantity1;
productinfo(this.barcode, this.name1, this.price1, this.quantity1);
}
class Cart with ChangeNotifier {
Map<String, productinfo> _items = {};
Map<String, productinfo> get items {
return {..._items};
}
Map<String,productinfo> ioe = {};
int get itemCount {
return _items.length;
}
void addItem(String barcode, String name, String price) {
if (_items.containsKey(barcode)) {
_items.update(
barcode,
(existitm) => productinfo(existitm.barcode, existitm.name1,
existitm.price1, existitm.quantity1 + 1));
} else {
_items.putIfAbsent(barcode, () => productinfo(barcode, name, price, 1));
}
notifyListeners();
}
void removeitem(
String barcode,
) {
_items.remove(barcode);
}
void removesingitem(String barcode, int quantity) {
if (!_items.containsKey(barcode)) {
return;
}
if (_items[barcode]!.quantity1 > 1) {
_items.update(
barcode,
(existitm) => productinfo(barcode, existitm.name1, existitm.price1,
existitm.quantity1 - quantity));
}
notifyListeners();
}
void cler() {
_items = {};
notifyListeners();
}
}
class buildcartitem extends StatelessWidget {
String barcode;
String name;
String price;
int quantity;
buildcartitem(this.barcode, this.name, this.price, this.quantity);
@override
Widget build(BuildContext context) {
return Container(
height: 66,
width: 366,
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
color: const Color(0xffe1e1e1),
boxShadow: [
BoxShadow(
color: const Color(0x29000000),
offset: Offset(0, 3),
blurRadius: 6,
),
],
),
child: Card(
color: Colors.transparent,
child: Row(children: [
Column(
children: [
Container(
color: Colors.transparent,
width: 60,
height: 22,
child: Text(name),
)
],
),
Column(
children: [Text('$quantity')],
),
Column(
children: [Text(' price:$price')],
),
])),
);
}
}
它应该查询,然后如果它在购物车内,那么它应该向用户显示产品的数量和名称。在主页上并基于此动态更改数量。
如果有人提前回答谢谢,那将非常有帮助