本文介绍了Flutter-VoidCallback内容未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下自定义小部件:

I've created the following custome widget:

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,
            ),)
        ],
      ),
    ),
  );
}

}

这是我创建MainButtonWidget类型的对象的方式:

Here is how I created an object of type MainButtonWidget:

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

按下按钮时似乎VoidCallback的内容未执行.

When pressing on the button seems that the content of the VoidCallback is not executing.

在日志中显示以下消息:I/flutter(21436):_onPressed之前的mainButtonWidgetI/flutter(21436):_onPressed之后的mainButtonWidget

In the log the following messages are shown:I/flutter (21436): mainButtonWidget before _onPressedI/flutter (21436): mainButtonWidget after _onPressed

我希望在上述消息之间看到"mapMainBtn-> onPressed被调用".

I expected to see 'mapMainBtn -> onPressed called' between the above messages.

推荐答案

调用它()是必需的

_onPressed();

这篇关于Flutter-VoidCallback内容未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 19:14