How to access the changed value of a variable of another class?

Asked

Viewed 384 times

-1

I have two classes, my app’s home screen class (Homescreen) and my button class (Button).

I have this function in my button class:

bool variável = false;

void alterarValor() {
  setState((){
    variável = !variável;
  })
}

After changing the value of this variable in my button class, as I pass its value in real time to my screen home class?

Because even running the function, the value does not change in the class of the start screen, it is in the initial value of the variable.

What do I do? kkkkkkkkkkkk

I’m really stuck on this :/

Thank you in advance!

1 answer

0


As pointed out in the comments, this topic is called state management (in English).

About this, there are many applicable solutions and each has its pros and cons. Which one is best for your application depends a lot on how it is structured and how your state is manipulated. For a large application, this is a decision much important. Undoubtedly it is something that yields many hours of discussion and debate.

But as an example, I leave one of the simplest ways to do what you want.

Your button class should receive as a parameter a Function which will serve as callback for when it is clicked:

class MeuBotao extends StatelessWidget {
MeuBotao({this.callback});
final Function callback;
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
            onPressed: callback,
            child: Text('Texto do botão'),
          );
  }
}

Your main class HomeScreen which should manage the variable status now. Create a function that will be called when the button is pressed:

class _HomeScreenState extends State<HomeScreen> {
    bool variavel = false;
    
    void alterarValor() {
            setState((){
                variável = !variável;
            })
    }
[...]
}

And when you build the button, it takes this callback as a parameter:

child: MeuBotao(callback: alterarValor),

Therefore, some caveats:

  • Using only this method hardly your program will be scalable for a larger application. If there is a more complex widgets tree, it will take several callbacks that call each other. It is impractical and very easy to get confused and generate errors.
  • In the example I put your button as StatelessWidget. If he needs the value of variável for drawing, you can receive as a parameter setState() higher will rebuild it. It may own state as well if necessary. There may be more actions within your onPressed and in the end call the callback. Customization is due to the use.
  • Google calls this solution "Raise the state" (of the English: Lifting state up). This is because the state variable was present in a lower widget in the widgets tree and was moved to a higher widget. This in turn manages to pass it on to all Widgets who need it.
  • 1

    Thank you very much! It really helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.