r/flutterhelp • • Feb 06 '22

RESOLVED Waiting for Button Press in a While Loop

I am building a game where I need my while loop to frequently stop and wait for which button is pressed to determine what happens next, my buttons are in another file and I am not sure how to make this happen.

So I need the while loop to run until I need the user to give input.

0 Upvotes

5 comments sorted by

2

u/StarTrekVeteran Feb 06 '22

are the buttons on the screen the whole time or in a dialog box?

1

u/[deleted] Feb 06 '22

They are on the screen until it is clicked, then the state changes until the loop restarts and needs more button input.

2

u/StarTrekVeteran Feb 06 '22

ok, thats easy.

I would just set 2 variables, buttonPressed and oldButtonPressed

In your build have:

if (buttonPressed != oldButtonPressed) {
oldButtonPressed = buttonPressed;
<Do something with the new button state>
}

In the button pressed event have:

buttonPressed++;
setState(() {

});

This way a tap on the button will cause a refresh of the page with the new value

There may be a better way of doing it, but this would be my first try.

good luck

4

u/David_Owens Feb 06 '22

You may need to post/link your code. Can you upload it to a public Github repo?

It sounds like you're trying to do things the wrong way.

1

u/Techne03 Feb 06 '22

The easiest way would be probably to have the button press set a variable. Then have the while loop check the value of that variable.