Search code examples
flutterpushsetstatenavigator

setState() is not working inside my widget, since my that widget is built on the bottom of the widget tree, then how can I rebuild that widget?


I used Navigator.push to navigator to next screen and after I pop that screen I could not able to rebuild the old screen. And also I want my code to rebuild if user on pressed the login button

//This is my stateful Widget

class LoginpageClass extends StatefulWidget {
   @override
   _LoginpageClassState createState() => _LoginpageClassState();
}

class _LoginpageClassState extends State<LoginpageClass> {
  final _auth = FirebaseAuth.instance;

  @override
  Widget build(BuildContext context) {
  return Scaffold(
      body: SingleChildScrollView(child: buildBody(context)),
  );
  } 
 }

//Calling this widget above

Widget buildBody(BuildContext context) => Container(
  width: double.infinity,
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.center,
    children: [
      buildTopClippers(),
      heightSpacer(20.00),
      buildLogoField(),
      heightSpacer(25.00),
      buildTextField(
        "Username",
        Icons.person,
        false,
        onChangedEmail,
      ),
      heightSpacer(15.00),
      buildTextField("Password", Icons.lock, true, onChangedPassword),
      heightSpacer(25.00),
      buildSignUpText(context),
      heightSpacer(25.00),
      buildLoginContainer(context),
      heightSpacer(20.00),
      buildOtherLogins(context),
      buildBottomClippers()
    ],
  ),
);

//Calling buildLoginContainer above

  Widget buildLoginContainer(BuildContext context) => Container(
    width: MediaQuery.of(context).size.width,
    height: 65,
    child: Stack(
      children: [
        buildLoginBtn(context),
      ],
    ),
  );

 Widget buildLoginBtn(context) => Positioned(
     right: -100,
     child: FlatButton(
        onPressed: () async {
            print(email);
         try {
            final newUser = await _auth.signInWithEmailAndPassword(
                email: Email, password: Password);
            print(newUser);

            if (newUser != null) {
              Navigator.pushNamed(context, '/');
             }
           } catch (e) {
           print(e);
        
         }
        },
    child: Container(
      width: 350,
      height: 65,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(40.00),
        color: Color.fromRGBO(11, 94, 255, 1),
      ),
      child: buildLoginText(),
    ),
  ),
);

Widget buildLoginText() => Row(
  children: [
    widthSpacer(75.00),
    Text(
      "Login",
      style: TextStyle(color: Colors.white, fontSize: 20.00),
    ),
    widthSpacer(5.5),
    Icon(
      Icons.navigate_next,
      color: Colors.white,
    )
  ],
);

I am beginner on developer therefore I dont know how to use setState in this widget, and also it is not available for access through this widget. All widgets are created on seperate dart file.

I viewed some of wrote about inherited widget usage. How can it be used here?? Or any other method??


Solution

  • while using push you are storing the state, thats' why when you come back, you find the same state. lets' use pushReplacement, also make use on pop side.