Fabian M
Fabian M

Reputation: 288

Flutter How do I change state of other widget

I have two Widgets for my Login in a ListView. This allows me to navigate like that throuw sign up or sign in: pic

I want to change the Icon direction of both widgets, when I press one Button. I tried this with a Global Variable and a setState, but i guess this is a very bad practise :D Also it didnt worked out like I wish: Only the Icon I clicked on, change the direction, the other one dont update its state :(

I read something from a StreamBuilder, but have no idea how to implement it!

Here is one of my setStates:

 IconButton(
                                  onPressed: () {
                                    if (showLogin) {
                                      controllerV.nextPage(duration: const Duration(milliseconds: 750), curve: Curves.easeIn);
                                      setState((){
                                        showLogin = false;
                                      });


                                    } else {
                                      controllerV.previousPage(duration: const Duration(milliseconds: 700), curve: Curves.easeIn);

                                      setState((){
                                        showLogin = true;
                                      });
icon: showLogin? const Icon(Icons.arrow_downward) : const Icon(Icons.arrow_upward)),

I also provide my ListView, maybe it helps you to understand what my code do:

ScrollConfiguration(
  behavior: MyBehavior(),
  child: ListView(
      controller: controllerV,
      scrollDirection: Axis.vertical,
      physics: isKeyboardVisible? const AlwaysScrollableScrollPhysics(): const NeverScrollableScrollPhysics(),

      children:   const [
        LoginForm(),
        RegisterForm(),


      ]),
),

Upvotes: 1

Views: 730

Answers (1)

Mashood .H
Mashood .H

Reputation: 1782

You should try any state manager it's a pretty bad practice to change any other widget state by one widget. I'll recommend you to learn about bloc but it's not beginner friendly you can start with the provider or Getx

Upvotes: 1

Related Questions