1
    import 'package:flutter/material.dart';
    import 'package:flutter_application_1/pages/home_page.dart';
    import 'package:flutter_application_1/utils/routes.dart';
    
    class LoginPage extends StatefulWidget {
      const LoginPage({Key? key}) : super(key: key);
    
      @override
      State<LoginPage> createState() => _LoginPageState();
    }
    
    class _LoginPageState extends State<LoginPage> {
      @override
      Widget build(BuildContext context) {
        String x = '';
        return Material(
            // wrap column with singlechildscrollview widget
            child: SingleChildScrollView(
          child: Column(
            children: [
              SizedBox(
                height: 28,
              ),
              Image.asset(
                "assets/images/login_image.png",
                fit: BoxFit.contain,
              ),
              SizedBox(
                height: 20.0,
              ),
              Text("Welcome $x",
                  style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
              SizedBox(
                height: 20,
              ),
              // wrapping a column with Padding
              Padding(
                padding:
                    const EdgeInsets.symmetric(vertical: 16.0, horizontal: 32.0),
                child: Column(
                  children: [
                    TextFormField(
                      decoration: InputDecoration(
                          hintText: "Enter Username", labelText: "Username"),
                      onChanged: (value) {
                        x = value;
                        setState(() {});
                      },
                    ),
                    TextFormField(
                      obscureText: true,
                      decoration: InputDecoration(
                          hintText: "Enter Password", labelText: "Password"),
                    ),
                    SizedBox(
                      height: 30,
                    ),
                    ElevatedButton(
                      child: Text("Login"),
                      style: TextButton.styleFrom(minimumSize: Size(150, 40)),
                      onPressed: () {
                        print("hello");
                      },
                    )
                  ],
                ),
              ),
            ],
          ),
        ));
      }
    }
    

I'm new to flutter and as I was learning about stateful widget. I'm facing this problem.setstate() is not working in flutter, the compiler is showing no error. Basically I want that when someone enter string in username textfield then the value should be appended along with "welcome".

3 Answers 3

2

You should put String x = ''; outside the Widget, see below:

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  String x = '';
  @override
  Widget build(BuildContext context) {
    ...
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you assign your String x = ''; inside the build function and it always assigns the empty string when the widget rebuilds.

Just assign the variable outside the build function.

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  String x = '';
  @override
  Widget build(BuildContext context) {
    return Material(
        // wrap column with singlechildscrollview widget
        child: SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(
            height: 28,
          ),
          Image.asset(
            "assets/images/login_image.png",
            fit: BoxFit.contain,
          ),
          SizedBox(
            height: 20.0,
          ),
          Text("Welcome $x",
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
          SizedBox(
            height: 20,
          ),
          // wrapping a column with Padding
          Padding(
            padding:
                const EdgeInsets.symmetric(vertical: 16.0, horizontal: 32.0),
            child: Column(
              children: [
                TextFormField(
                  decoration: InputDecoration(
                      hintText: "Enter Username", labelText: "Username"),
                  onChanged: (value) {
                    x = value;
                    print(value);
                    setState(() {});
                  },
                ),
                TextFormField(
                  obscureText: true,
                  decoration: InputDecoration(
                      hintText: "Enter Password", labelText: "Password"),
                ),
                SizedBox(
                  height: 30,
                ),
                ElevatedButton(
                  child: Text("Login"),
                  style: TextButton.styleFrom(minimumSize: Size(150, 40)),
                  onPressed: () {
                    print("hello");
                  },
                )
              ],
            ),
          ),
        ],
      ),
    ));
  }
}

Comments

1

Try to declare your state variable outside the build method.

class _LoginPageState extends State<LoginPage> {
      String x = '';

      @override
      Widget build(BuildContext context) {
       .....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.