计算颤动中的价格折扣
calculate discount of prices in flutter
我想尝试定义下面的公式来计算旧价格和新价格并将其显示在文本小部件上但是当我想在双变量中使用它时我遇到了这个错误:"Only static members can be accessed in initializers"
这就是我想要做的:
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%");
}
}
我想尝试定义下面的公式来计算旧价格和新价格并将其显示在文本小部件上但是当我想在双变量中使用它时我遇到了这个错误:"Only static members can be accessed in initializers" 这就是我想要做的:
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%");
}
}