class ExtractArgumentsScreen extends StatelessWidget {
static const routeName = '/extractArguments';
double price = 0;
_changeprice(num){
setState(){
price+ = num;
}
}
@override
Widget build(BuildContext context) {
final MealArguments args = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text('MyApp'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
SizedBox(height: 20,),
ViewCard(args.combo,_changeprice()),
]));
这是父类。我把函数 _changePrice()
传递给了 ViewCard
,这样以后就可以改变价格的值了。
但这给了我一个
错误:“需要1个参数,但找到0个”
但我不能向 _changePrice
传递参数,因为那会毁掉使用此回调的整个目的。
我试图实现的与 Flutter中将数据传递至父组件中建议的类似。
子类 ViewCard 将此回调传递给了另一个子类 OneItem
。
class OneItem extends StatefulWidget {
OneItem(this.combo, this.index, this.changePrice);
final Combo combo;
final int index;
final VoidCallback changePrice;
@override
State<StatefulWidget> createState() => OneItemState();
}
class OneItemState extends State<OneItem> {
List<bool> Vals = new List();
@override
void initState() {
super.initState();
int count = widget.combo.items.length;
Vals = List<bool>.generate(count, (_) => false);
}
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Align(
child: Text(widget.combo.items[widget.index].item),
alignment: Alignment(-1, 0),
),
Align(
child: Text(widget.combo.items[widget.index].price),
alignment: Alignment(0.1, 0)),
Align(
child: Checkbox(
value: Vals[widget.index],
onChanged: (bool value) {
setState(() {
Vals[widget.index] = value;
if(value == true){double childPrice = double.parse(widget.combo.items[widget.index].price); }
else{double val = double.parse(widget.combo.items[widget.index].price);
double childPrice = -1*val;}
widget.changePrice(childPrice);
//widget.changePrice();
});
},
),
alignment: Alignment(0.6, 0)),
],
);
}
}