【问题标题】:How to pass parameters to callbacks in flutter?如何将参数传递给颤振中的回调?
【发布时间】:2020-02-13 09:28:04
【问题描述】:
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,因为这违背了使用此回调的全部目的。 我正在尝试实现的内容类似于Emit the data to parent Widget in 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)),
      ],
    );
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以声明一个Function 回调,如下所示:

    final void Function(double price) changePrice;
    

    这里是另一个更好理解的例子:

    final bool Function(String id, double price) updateItem;
    
    bool isPriceUpdated = widget.updateItem("item1", 7.5);
    

    【讨论】:

      【解决方案2】:

      你从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
      

      我会这样做

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-24
        • 2020-05-07
        • 1970-01-01
        • 2010-11-14
        • 1970-01-01
        相关资源
        最近更新 更多