Flutter 如何在flatter中将php mysql streambuilder中的变量数据设置为全局变量?
如何从数据库中获取通用值,以便在页面中的任何位置调用和使用它 这是目前我的代码,但它根本没有给我想要的lolFlutter 如何在flatter中将php mysql streambuilder中的变量数据设置为全局变量?,flutter,dart,Flutter,Dart,如何从数据库中获取通用值,以便在页面中的任何位置调用和使用它 这是目前我的代码,但它根本没有给我想要的lol class _CartPageState extends State<CartPage> { StreamController<List> _streamController = StreamController<List>(); Timer _timer; Future getData() async { final respo
class _CartPageState extends State<CartPage> {
StreamController<List> _streamController = StreamController<List>();
Timer _timer;
Future getData() async {
final response = await http
.post(Uri.parse("http://192.168.43.107/flutterdb/cart.php"), body: {
"customerid": widget.customerid,
});
List data = json.decode(response.body);
//Add your data to stream
_streamController.add(data);
/*/starts a new upload
final quantityupdate = await http.post(
Uri.parse("http://192.168.43.107/flutterdb/cartquantity.php"),
body: {
"customerid": widget.customerid,
"productid": productName,
});
List quantitydata = json.decode(quantityupdate.body);
//Add your data to stream
_streamController.add(quantitydata);
*/ ///ends the new upload
}
@override
void initState() {
getData();
//Check the server every 5 seconds
_timer = Timer.periodic(Duration(seconds: 2), (timer) => getData());
super.initState();
}
@override
void dispose() {
//cancel the timer
if (_timer.isActive) _timer.cancel();
super.dispose();
}
然后,我想能够显示总价格加上运输,即个别价格乘以数量,然后添加到下一个产品
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
padding: EdgeInsets.only(left: 10, right: 10),
margin: EdgeInsets.only(left: 33),
height: 80,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(color: Colors.grey[300]),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
alignment: Alignment.topLeft,
child: Text('SubTotal'),
),
Container(
alignment: Alignment.centerLeft,
child: Text('Shipping'),
),
Container(
alignment: Alignment.bottomLeft,
child: Text(
'Total',
style: TextStyle(fontWeight: FontWeight.bold),
),
)
],
),
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
child: Row(
children: [
FaIcon(
FontAwesomeIcons.dollarSign,
size: 14,
),
Container(
child: Text(
"I want price value here",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
],
),
),
Container(
child: Row(
children: [
FaIcon(
FontAwesomeIcons.dollarSign,
size: 14,
),
Container(
child: Text(
shipping.toString(),
style: TextStyle(fontWeight: FontWeight.bold),
),
),
],
),
),
Container(
child: Row(
children: [
FaIcon(
FontAwesomeIcons.dollarSign,
size: 14,
),
Container(
child: Text(
"I want total value price here i.e. products + shipping",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
],
),
),
],
),
],
)),
Container(
padding: EdgeInsets.only(left: 30),
alignment: Alignment.bottomCenter,
child: Container(
width: MediaQuery.of(context).size.width,
child: ElevatedButton.icon(
label: Text('Checkout'),
icon: Icon(Icons.shopping_cart),
onPressed: () {
//do something
},
style: ElevatedButton.styleFrom(primary: Colors.deepOrange),
),
),
),
],
));
}
这是我用来构建每个数据文件的容器函数,我在上面调用了它
Container cartlist(imagelink, productName, price, quantityval, productID) {
return Container(
padding: EdgeInsets.all(2),
margin: EdgeInsets.only(right: 17, left: 17, top: 5),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(0, 1),
blurRadius: 5,
),
],
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
child: Image.network(
imagelink,
fit: BoxFit.contain,
width: 120,
height: 100,
),
),
Container(
child: Column(
children: [
Container(
child: Text(
productName,
style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
),
),
Container(
padding: EdgeInsets.all(5),
child: Text("Quantity"),
),
Container(
child: Row(
children: [
FaIcon(
FontAwesomeIcons.dollarSign,
size: 14,
),
Container(
child: Text(
price,
style: TextStyle(fontWeight: FontWeight.bold),
),
),
],
),
)
],
),
),
Container(
padding: EdgeInsets.all(20),
child: Column(
children: [
InkWell(
onTap: () {
Future getdata2() async {
final quantityupdate = await http.post(
Uri.parse(
"http://192.168.43.107/flutterdb/cartquantity.php"),
body: {
"customerid": widget.customerid,
"productID": productID,
});
List quantitydata = json.decode(quantityupdate.body);
//Add your data to stream
_streamController.add(quantitydata);
print(quantityval);
}
getdata2();
},
child: Icon(Icons.add),
),
Container(
padding: EdgeInsets.all(5),
child: Text('"' + quantityval.toString() + '"'),
),
InkWell(
onTap: () {
if (quantityval == 1) {
} else {
Future getdata() async {
setState(
() async {
final quantityupdate = await http.post(
Uri.parse(
"http://192.168.43.107/flutterdb/cartquantity2.php"),
body: {
"customerid": widget.customerid,
"productID": productID,
});
List quantitydata =
json.decode(quantityupdate.body);
//Add your data to stream
_streamController.add(quantitydata);
print(quantityval);
},
);
}
getdata();
}
},
child: Icon(Icons.remove),
),
],
),
),
],
),
);
}
}
这是应用程序的图片
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
padding: EdgeInsets.only(left: 10, right: 10),
margin: EdgeInsets.only(left: 33),
height: 80,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(color: Colors.grey[300]),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
alignment: Alignment.topLeft,
child: Text('SubTotal'),
),
Container(
alignment: Alignment.centerLeft,
child: Text('Shipping'),
),
Container(
alignment: Alignment.bottomLeft,
child: Text(
'Total',
style: TextStyle(fontWeight: FontWeight.bold),
),
)
],
),
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
child: Row(
children: [
FaIcon(
FontAwesomeIcons.dollarSign,
size: 14,
),
Container(
child: Text(
"I want price value here",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
],
),
),
Container(
child: Row(
children: [
FaIcon(
FontAwesomeIcons.dollarSign,
size: 14,
),
Container(
child: Text(
shipping.toString(),
style: TextStyle(fontWeight: FontWeight.bold),
),
),
],
),
),
Container(
child: Row(
children: [
FaIcon(
FontAwesomeIcons.dollarSign,
size: 14,
),
Container(
child: Text(
"I want total value price here i.e. products + shipping",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
],
),
),
],
),
],
)),
Container(
padding: EdgeInsets.only(left: 30),
alignment: Alignment.bottomCenter,
child: Container(
width: MediaQuery.of(context).size.width,
child: ElevatedButton.icon(
label: Text('Checkout'),
icon: Icon(Icons.shopping_cart),
onPressed: () {
//do something
},
style: ElevatedButton.styleFrom(primary: Colors.deepOrange),
),
),
),
],
));
}
Container cartlist(imagelink, productName, price, quantityval, productID) {
return Container(
padding: EdgeInsets.all(2),
margin: EdgeInsets.only(right: 17, left: 17, top: 5),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(0, 1),
blurRadius: 5,
),
],
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
child: Image.network(
imagelink,
fit: BoxFit.contain,
width: 120,
height: 100,
),
),
Container(
child: Column(
children: [
Container(
child: Text(
productName,
style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
),
),
Container(
padding: EdgeInsets.all(5),
child: Text("Quantity"),
),
Container(
child: Row(
children: [
FaIcon(
FontAwesomeIcons.dollarSign,
size: 14,
),
Container(
child: Text(
price,
style: TextStyle(fontWeight: FontWeight.bold),
),
),
],
),
)
],
),
),
Container(
padding: EdgeInsets.all(20),
child: Column(
children: [
InkWell(
onTap: () {
Future getdata2() async {
final quantityupdate = await http.post(
Uri.parse(
"http://192.168.43.107/flutterdb/cartquantity.php"),
body: {
"customerid": widget.customerid,
"productID": productID,
});
List quantitydata = json.decode(quantityupdate.body);
//Add your data to stream
_streamController.add(quantitydata);
print(quantityval);
}
getdata2();
},
child: Icon(Icons.add),
),
Container(
padding: EdgeInsets.all(5),
child: Text('"' + quantityval.toString() + '"'),
),
InkWell(
onTap: () {
if (quantityval == 1) {
} else {
Future getdata() async {
setState(
() async {
final quantityupdate = await http.post(
Uri.parse(
"http://192.168.43.107/flutterdb/cartquantity2.php"),
body: {
"customerid": widget.customerid,
"productID": productID,
});
List quantitydata =
json.decode(quantityupdate.body);
//Add your data to stream
_streamController.add(quantitydata);
print(quantityval);
},
);
}
getdata();
}
},
child: Icon(Icons.remove),
),
],
),
),
],
),
);
}
}