如何将参数传递给颤振中的回调?

Hem*_*abh 10 dart flutter

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()),
        ]));

Run Code Online (Sandbox Code Playgroud)

这是父类。我将函数传递_changePrice()ViewCard以便我可以稍后更改价格的值。这给了我一个

错误“需要 1 个必需参数,但找到 0 个”

但我不能传递一个参数,_changePrice因为这违背了使用这个回调的全部目的。我正在尝试实现的内容类似于将数据发送到 Flutter 中的父 Widget 中所建议的内容。

子类 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)),
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Pab*_*era 12

You could declare a Function callback, like this:

final void Function(double price) changePrice;
Run Code Online (Sandbox Code Playgroud)

Here another example to understand it better:

final bool Function(String id, double price) updateItem;

bool isPriceUpdated = widget.updateItem("item1", 7.5);
Run Code Online (Sandbox Code Playgroud)


And*_*sky 9

widget.changePrice(childPrice);从打电话OneItemState,所以changePrice不能VoidCallback

typedef IntCallback = Function(int num);

/* ... */

class OneItem extends StatefulWidget {
  OneItem(this.combo, this.index, this.changePrice);
  final Combo combo;
  final int index;
  final IntCallback changePrice;

  @override
  State<StatefulWidget> createState() => OneItemState();
}

/* ... */

ViewCard(args.combo, _changeprice) // function name without braces
Run Code Online (Sandbox Code Playgroud)

我会这样做