[英]flutter setState() is working but not updating the UI
I am trying to make a ecommerce app.我正在尝试制作电子商务应用程序。 But in the Product details screen when I am trying to cart Icon and favorite Icon they are not changing their colors and Icons.
但是在产品详细信息屏幕中,当我尝试购物图标和最喜欢的图标时,它们并没有改变它们的颜色和图标。 I have added a print statement in the SetState function and the value to change after clicking the icon.
我在 SetState 函数中添加了一个打印语句,并在单击该图标后更改了该值。 I am getting the output and value changes but they are not updating in the UI.
我得到了输出和值的变化,但它们没有在 UI 中更新。 Please help me.
请帮我。
class ProductDetails extends StatefulWidget {
final Product _product;
ProductDetails(this._product);
@override
_ProductDetailsState createState() => _ProductDetailsState(_product);
}
class _ProductDetailsState extends State<ProductDetails> {
final Product _product;
_ProductDetailsState(this._product);
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return ScaffoldWidget(
child: ListView(
children: <Widget>[
MainPartProduct(_product),
Container(
child: Row(
children: <Widget>[
GestureDetector(
onTap: () {
Navigator.of(context).push(
(MaterialPageRoute(
builder: (context) => CartScreen(),
)),
);
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 0.0),
child: Container(
margin: EdgeInsets.symmetric(vertical: 10.0),
alignment: Alignment.center,
width: MediaQuery.of(context).size.width * 0.75,
color: Colors.red,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
child: Text(
"BUY",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
IconButton(
color:_product.cart ? Colors.black : Colors.red,
onPressed: () {
_product.cart ? productCart.remove(_product): productCart.add(_product);
setState(() {
_product.cart = !_product.cart;
print(_product.cart);
});
},
icon:_product.cart ? Icon(Icons.remove_shopping_cart) : Icon(Icons.add_shopping_cart),
),
IconButton(
color: Colors.red,
onPressed: () {
this.setState(() {
_product.fav ? productFavorite.remove(_product) : productFavorite.add(_product);
_product.fav = !_product.fav;
});
},
icon: (_product.fav) ? Icon(Icons.favorite) : Icon(Icons.favorite_border),
),
],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Container(
height: 500,
child: Text(
_product.details + _product.details,
style: TextStyle(
fontSize: 16.5,
),
),
),
),
],
));
}
}
You shouldn't pass parameters through the constructor of the StatefulWidget
.您不应该通过
StatefulWidget
的构造函数传递参数。 You have to access it via widget
field aka widget._product
您必须通过
widget
字段 aka widget._product
访问它
class ProductDetails extends StatefulWidget {
final Product _product;
const ProductDetails({ Key key, this._product }): super(key: key);
@override
_ProductDetailsState createState() => _ProductDetailsState();
}
class _ProductDetailsState extends State<ProductDetails> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return ScaffoldWidget(
child: ListView(
children: <Widget>[
MainPartProduct(widget._product),
Container(
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.