Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - VoidCallback content is not executing

Tags:

flutter

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

}

Here is how I created an object of type MainButtonWidget:

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

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

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

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

like image 278
Stoinescu Cosmin Avatar asked Dec 14 '25 02:12

Stoinescu Cosmin


1 Answers

To call it () is required

_onPressed();
like image 63
Günter Zöchbauer Avatar answered Dec 16 '25 23:12

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!