颤动:IconButton onPressed没有被调用

Mne*_*oee 2 android click ios flutter

我在ScaffoldappBar中将一个widget列表作为动作,但是当我按下它们时它们没有响应,我floatingButton在场景中也有一个并且它完美地工作.

appBar: new AppBar(
        title: new Text(
            widget.title,
          style: new TextStyle(
            fontFamily: 'vazir'
          ),
        ),
        centerTitle: true,
        actions: <Widget>[
          new IconButton(
            icon: new Icon(Icons.search),
            highlightColor: Colors.pink,
            onPressed: _onSearchButtonPressed(),
          ),
        ],
      ),

void _onSearchButtonPressed() {
    print("search button clicked");
  }
Run Code Online (Sandbox Code Playgroud)

即使我把IconButton放在一个RowColumn小部件中,而不是在其中appBar,它也不再起作用.
答:
感谢siva Kumar,我在调用函数时遇到了错误,我们应该这样调用它:

onPressed: _onSearchButtonPressed, // without parenthesis.
Run Code Online (Sandbox Code Playgroud)

或者这样:

onPressed: (){
    _onSearchButtonPressed();
},
Run Code Online (Sandbox Code Playgroud)

siv*_*mar 9

请尝试我的回答它会工作.

    appBar: new AppBar(
    title: new Text(
        widget.title,
      style: new TextStyle(
        fontFamily: 'vazir'
      ),
    ),
    centerTitle: true,
    actions: <Widget>[
      new IconButton(
        icon: new Icon(Icons.search),
        highlightColor: Colors.pink,
        onPressed: (){_onSearchButtonPressed();},
      ),
    ],
  ),

void _onSearchButtonPressed() {
print("search button clicked");
}
Run Code Online (Sandbox Code Playgroud)

  • 如果可能,请尝试添加解释,说明为什么问题中发布的代码不起作用以及您的解决方案如何缓解它。 (4认同)