简体   繁体   English

如何在自定义按钮颤动中给 onPressed 一个方法

[英]How to give onPressed a method in custom button flutter

i have custom button and i want to add method in my onpressed from my parameter,unfortunately i'm still stuck what type should i use in my custom button parameter, curently i hard code the onpressed method and pass a string route to navigate the problem is not all my button is made to navigate there is one button to logout, i already used void in my parameter but it's still doesn't work.我有自定义按钮,我想在我的 onpressed 从我的参数中添加方法,不幸的是我仍然坚持我应该在我的自定义按钮参数中使用什么类型,目前我硬编码 onpressed 方法并传递一个字符串路由来导航问题不是我的所有按钮都是用来导航的,只有一个按钮可以注销,我已经在我的参数中使用了 void 但它仍然不起作用。 here is my custom button这是我的自定义按钮

FlatButton buttonMenu(String title, IconData icon, String route) {
    return new FlatButton(
      shape: new RoundedRectangleBorder(
          borderRadius: new BorderRadius.circular(25.0)),
      color: Colors.blueGrey,
      child: new Container(
        height: 50.0,
        width: 120.0,
        child: new Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            new Padding(
              padding: EdgeInsets.only(right: 10.0),
              child: new Icon(
                icon,
                color: Colors.white70,
                size: 30.0,
              ),
            ),
            new Text(
              title,
              style: new TextStyle(fontSize: 20.0, fontWeight: FontWeight.w300),
            ),
          ],
        ),
      ),
      onPressed: () {
      Navigator.of(context).pushNamed('/$route');
      },
    );
  }
}

and here is how i call the custom button这就是我如何调用自定义按钮

 buttonMenu('MVP Arc', Icons.star, 'EmployeeScreen')

and what i want is i can send method in my parameter to my onpressed so it will be like this我想要的是我可以将我的参数中的方法发送到我的 onpressed 所以它会是这样的

buttonMenu('MVP Arc', Icons.star, myMethod())

my method for navigate我的导航方法

void _navigate(String route){
    Navigator.of(context).pushNamed('/$route');
  }

the onPressed type is VoidCallback so make your parameter like this this type. onPressed类型是VoidCallback所以让你的参数像这种类型。

FlatButton buttonMenu(String title, IconData icon, VoidCallback onCustomButtonPressed ) 

and when you call it pass a reference to it当你调用它时,传递对它的引用

buttonMenu('MVP Arc', Icons.star, myMethod)

and don't forget to do the same in your function body并且不要忘记在你的函数体中做同样的事情

  onPressed: onCustomButtonPressed ,

UPDATE: if you want to pass parameters you have to declare your own Signature :更新:如果你想传递参数,你必须声明你自己的 Signature :

Example in your case :你的例子:

typedef VoidNavigate = void  Function(String route);

and then use this new type然后使用这种新类型

FlatButton buttonMenu(String title, IconData icon, VoidNavigate onCustomButtonPressed) 

call it like this像这样称呼它

onPressed: (){onCustomButtonPressed},

and you pass your function like this然后你像这样传递你的函数

buttonMenu('MVP Arc', Icons.star, _navigate('EmployeeScreen')) 

or you can just do it like this as @Kirill Shashov said或者你可以像@Kirill Shashov 所说的那样做

buttonMenu('MVP Arc', Icons.star, () {_navigate('EmployeeScreen');})

The accepted answer for passing parameters no longer seems to work on newer versions of Flutter.传递参数的公认答案似乎不再适用于较新版本的 Flutter。

In case someone searches for a similar issue, the modifications below worked for me.如果有人搜索类似的问题,下面的修改对我有用。

Modifying Raouf Rahiche's answer, the working method as of 2019. October 28 is as follows.修改 Raouf Rahiche 的回答,截至 2019 年 10 月 28 日的工作方法如下。

Update: added mising flutter version number below更新:在下面添加了缺失的颤振版本号

Flutter version: 1.9.1+hotfix.5 Flutter 版本:1.9.1+hotfix.5

Dart version: 2.5.0飞镖版本:2.5.0

Using the existing typedef:使用现有的 typedef:

typedef VoidNavigate = void Function(String route);

Using the type changed in a small way, you have to pass the parameter to the button separately.使用以小方式更改的类型,您必须将参数单独传递给按钮。

FlatButton buttonMenu(
   String title,
   IconData icon, 
   VoidNavigate onCustomButtonPressed, 
   String route //New addition
) {
   ...
}

Calling it now:现在调用它:

onPressed: () { onCustomButtonPressed(route); },

Passing the function:传递函数:

buttonMenu('MVP Arc', Icons.star, _navigate, 'EmployeeScreen') 

Here is what worked for me:这对我有用:

Flutter Version : 2.6.0 Dart Version : 2.15.0颤振版本:2.6.0 飞镖版本:2.15.0

in my customButton function:在我的 customButton 函数中:

Widget customButton(VoidCallback onPressed) 

{
....
onPressed : onPressed.call(),
}

and no other changes needed.并且不需要其他更改。

the only we need to do is add .call() to the end of the function to be void.我们唯一需要做的就是将 .call() 添加到函数的末尾以使其无效。 onPressed: ()=>onPressed to onPressed: ()=>onPressed.call() onPressed: ()=>onPressed 到 onPressed: ()=>onPressed.call()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM