简体   繁体   English

Function 作为小部件 flutter 中的参数

[英]Function as parameter in widget flutter

I am trying to pass function as a parameter in my custom widget:我正在尝试将 function 作为自定义小部件中的参数传递:

_Btn(name: _btnOne, continueBtn: onClick(1)),

I made an onClick function in my main widget:我在我的主要小部件中制作了 onClick function :

onClick(int i) {
    switch(i){
      case 1:{ 
        print('hello');
      }
        break;
  }
}

and my button widget:和我的按钮小部件:

class _Btn extends StatelessWidget {
  final Function btn;

  const _GreenBtn({
    Key key,
    this.btn
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ElevatedButton(
        child: Text('hello'),
        style: ElevatedButton.styleFrom(
          primary: Colors.white,
        ),
        onPressed: () {
          btn();
        },
      ),
    );
  }
}

I am new to flutter, and I am not sure why its not working, if I am doing the next syntax than its ok:我是 flutter 的新手,我不确定为什么它不工作,如果我正在执行下一个语法而不是正常:

_Btn(name: _btnOne, continueBtn: () => print('hello')),

thanks谢谢

Replace your _Btn with the following:将您的_Btn替换为以下内容:

class _Btn extends StatelessWidget {
  final Function btn;
  final String name;
  const _Btn({Key key, this.btn, this.name}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ElevatedButton(
        child: Text('hello'),
        style: ElevatedButton.styleFrom(primary: Colors.white),
        onPressed: btn,
      ),
    );
  }
}

Now, use it like this:现在,像这样使用它:

_Btn(
  name: _btnOne,
  btn: () => onClick(1),
)

Why not send the function parameter as a parameter then call onClick(parameter) inside?为什么不发送 function 参数作为参数然后在里面调用 onClick(parameter) 呢?

Like this:像这样:

class _Btn extends StatelessWidget {
  final int i;

  const _GreenBtn({
    Key key,
    this.btn
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ElevatedButton(
        child: Text('hello'),
        style: ElevatedButton.styleFrom(
          primary: Colors.white,
        ),
        onPressed: () {
          onClick(i) //here
        },
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Flutter 中以函数为参数调用小部件 - Calling a widget with a function as a parameter in Flutter 在 Flutter 小部件内调用 function - Call a function inside Flutter Widget 如何使用 buildContext 在 flutter 中为 map function 创建一个小部件 - How to create a widget for map function in flutter with buildContext Function 参数为 null。 #扑 - Function parameter of contructor was null . #Flutter 如何将函数的结果放入 Flutter 中的 Text 小部件? - How to put the result of a function into a Text widget in Flutter? Flutter 如果将 RawMaterial 与 Textspan 或识别器一起使用,则将 function 传递给无状态小部件不起作用 - Flutter Passing a function to stateless widget not working if using RawMaterial with Textspan or recognizer Flutter中如何将函数中的String类型参数传递给IconData - How to pass String type parameter in function to IconData in Flutter Flutter 开关小部件 - 我只希望它在活动时执行该功能,该怎么办? - Flutter switch widget- I ony want it to execute the function when it's active, what to do? 使用列表填充小部件<Widgets>来自预填充列表的函数 - Flutter - Populate a widget with a List<Widgets> function from a pre-populated List - Flutter Stateless Widget和Widget function的区别 - Stateless widget and Widget function difference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM