steve
steve

Reputation: 917

Flutter custom button not working after Flutter Upgrade

I have a custom progress button I created to of course show a progress indicator whilst an action is taking place. After flutter upgrade the buttons function does not get called, if I use a TextButton() widget and pass any function directly into the onPressed property then it does.

It also works in my custom button if I pass the VoidCallBack() myFunction direction into the function (like this onPressed: myFunction) If I add any logic aside passion the callback directly it does not work (code is below).

Any help is much appreciated!

class ProgressButton extends StatefulWidget {
  final String text;
  final VoidCallback? onPressed;

  ProgressButton({
    required this.text,
    this.onPressed,
  });

  @override
  _ProgressButtonState createState() => _ProgressButtonState();
}

class _ProgressButtonState extends State<ProgressButton> {
  bool _isLoading = false;

  @override
  Widget build(BuildContext context) {
    return TextButton(
      onPressed: () async {
        if(this.mounted)
          setState(() {
            _isLoading = true;
          });

        try {
          await widget.onPressed;
        } finally {
          if(this.mounted)
            setState(() {
              _isLoading = false;
            });
        }
      },
      child: Container(
        padding: EdgeInsets.all(12),
        child: _isLoading
            ? SizedBox(
                width: 15,
                height: 15,
                child: NizzProgressIndicator(
                  color: Colors.white,
                ),
              )
            : Text(widget.text),
      ),
    );
  }
}

Upvotes: 0

Views: 58

Answers (0)

Related Questions