Flutter - VoidCallback 内容未执行

Sto*_*min 2 flutter

我创建了以下客户小部件:

class MainButtonWidget extends StatelessWidget{
  String _text = "";
  TextTheme _textTheme = new TextTheme();
  IconData _icon = null;
  VoidCallback _onPressed;

  MainButtonWidget(String text, TextTheme textTheme, IconData icon, VoidCallback onPressed){
    _text = text;
    _textTheme = textTheme;
    _icon = icon;
    _onPressed = onPressed;
  }

  void setText(String text){
    _text = text;
  }

@override
Widget build(BuildContext context) {
  return new Container(
    child: new RaisedButton(
      padding: const EdgeInsets.fromLTRB(
          Dimens.edgeMainButtonLRB, Dimens.edgeMainButtonT,
          Dimens.edgeMainButtonLRB, Dimens.edgeMainButtonLRB),
      shape: new CircleBorder(side: new BorderSide(
          color: ColorRes.whiteTranslucent2,
          width: Dimens.widthSmallButtonHome)),
      onPressed: (){
        debugPrint("mainButtonWidget before _onPressed");
        _onPressed;
        debugPrint("mainButtonWidget after _onPressed");
        },
      color: Colors.transparent,
      child: new Column(
        children: <Widget>[
          new Icon(
            _icon,
            color: Colors.white,
          ),
          new Text(_text,
            style: _textTheme.button.copyWith(
              color: Colors.white,
            ),)
        ],
      ),
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)

}

以下是我创建 MainButtonWidget 类型的对象的方法:

Widget mapMainBtn = new MainButtonWidget('Map', textTheme, Icons.location_on, ()=> debugPrint("mapMainBtn -> onPressed called"));
Run Code Online (Sandbox Code Playgroud)

当按下按钮时,VoidCallback 的内容似乎没有执行。

日志中显示以下消息: I/flutter (21436): mainButtonWidget before _onPressed I/flutter (21436): mainButtonWidget after _onPressed

我希望在上述消息之间看到“mapMainBtn -> onPressed Called”。

Gün*_*uer 8

()需要调用它

_onPressed();
Run Code Online (Sandbox Code Playgroud)