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()),
]));
This is the parent class. I passed the function _changePrice()
to ViewCard
so that I can change the value of the price later.
This is giving me an
error "1 required argument(s) expected, but 0 found"
but I can't pass an argument to _changePrice
as that defeats the whole purpose of using this callback.
What I'm trying to implement is similar to what was suggested in Emit the data to parent Widget in Flutter.
The child class ViewCard passed this callback to another child called 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)),
],
);
}
}