如何在Flutter中向回调函数传递参数?

18
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)),
      ],
    );
  }
}
2个回答

20

您可以声明一个Function回调,像这样:

final void Function(double price) changePrice;

这里有另一个例子,以更好地理解它:

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

bool isPriceUpdated = widget.updateItem("item1", 7.5);

13

OneItemState中调用widget.changePrice(childPrice);,因此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

我会这样做


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接