我想尝试定义下面的公式来计算旧价格和新价格之间的关系,并在文本小部件上显示它,但当我想在双变量中使用它时,却出现了这样的错误:“只有静态成员可以在初始化器中访问”,这就是我想要做的:
class ProductDetails extends StatefulWidget {
final prod_fullName;
final prod_pic;
final prod_old_price;
final prod_price;
double percent=(prod_old_price - prod_price)/prod_old_price*100;
ProductDetails({
this.prod_fullName,
this.prod_pic,
this.prod_old_price,
this.prod_price,
});
@override
_ProductDetailsState createState() => _ProductDetailsState();
}
class _ProductDetailsState extends State<ProductDetails> {
Row{
child:new Text("$percent%");
}
class ProductDetails extends StatefulWidget {
final String prod_fullName;
final String prod_pic;
final double prod_old_price;
final double prod_price;
const ProductDetails({Key key, this.prod_fullName, this.prod_pic, this.prod_old_price, this.prod_price}) : super(key: key);
@override
_ProductDetailsState createState() => _ProductDetailsState();
}
class _ProductDetailsState extends State<ProductDetails> {
double percent;
@override
void initState() {
percent=(widget.prod_old_price - widget.prod_price)/widget.prod_old_price*100;
super.initState();
}
@override
Widget build(BuildContext context) {
return Text("$percent%");
}
}