1

There are 4 ElevatedButtons in my widget in a Row. If one of them is pressed an image will be shown based on the button. This image comes from an API so it takes a bit to load. Is it possible to disable the not-selected buttons while the data from the API arrives?

This is the code of the button:

 @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        padding: EdgeInsets.symmetric(horizontal: 5, vertical: 3),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5.0),
        ),
        primary: mainBgColor,
      ),
      onPressed: showImage,
      child: Text(
        title,
      ),
    );
  }

And this is the function:

showImage: =>_controller.buttonTap(getImage(quizImages.wrong));
Ballazx
  • 431
  • 4
  • 12
  • Does this answer your question? [How do I disable a Button in Flutter?](https://stackoverflow.com/questions/49351648/how-do-i-disable-a-button-in-flutter) – Robert Sandberg Aug 18 '22 at 13:29

3 Answers3

1

A button is disabled when the property onPressed is null. That is, you can assign null to your button onPressed or a callback based on a flag that is set while the image is fetched.

1

Sure, you need a variable that tells you if you're in loading state and then you can do:

onPressed: isLoading ? null : showImage
FLjubic
  • 161
  • 1
  • 4
1

Please take one variable which state changes respectively as below :

Start Api Calling :

isFetching = true

Api Success / Failure :

isFetching = false

Put below code at onPressed callback :

onPressed: isFetching ? null : showImage
Jaimil Patel
  • 1,301
  • 6
  • 13