user14037074
user14037074

Reputation:

One TextField works properly the other doesn't

I have two nearly identical TextField widgets. They are:

class EmailField extends StatelessWidget {
  const EmailField(this.width, this.height, this.controller, {Key key})
      : super(key: key);

  final double width;
  final double height;
  final TextEditingController controller;

  @override
  Widget build(BuildContext context) {
    return Positioned(
      top: 60 * height / 100,
      left: width / 15,
      width: width,
      child: Container(
        margin: EdgeInsets.fromLTRB(width / 5, 0, width / 5, 0),
        child: TextField(
          controller: controller,
          style: TextStyle(color: Colors.white, fontSize: 25),
          keyboardType: TextInputType.emailAddress,
          decoration: InputDecoration(
            hintText: 'email',
            hintStyle: TextStyle(color: Colors.white70, fontSize: 25),
            border: InputBorder.none,
            isDense: true,
          ),
        ),
      ),
    );
  }
}

and

class PasswordField extends StatelessWidget {
  const PasswordField(this.width, this.height, this.controller, {Key key})
      : super(key: key);

  final double width;
  final double height;
  final TextEditingController controller;

  @override
  Widget build(BuildContext context) {
    return Positioned(
      top: 60 * height / 100 + 100,
      left: width / 15,
      width: width,
      child: Container(
        margin: EdgeInsets.fromLTRB(width / 5, 0, width / 5, 0),
        child: TextField(
          controller: controller,
          style: TextStyle(color: Colors.white, fontSize: 25),
          decoration: InputDecoration(
            hintText: 'şifre',
            hintStyle: TextStyle(color: Colors.white70, fontSize: 25),
            border: InputBorder.none,
            isDense: true,
          ),
          obscureText: true,
        ),
      ),
    );
  }
}

This is how I handle the controllers and then I pass them as parameters:

class _AuthState extends State<Auth> {
  final emailController = TextEditingController();
  final passwordController = TextEditingController();
  bool isSignIn = true;
  String _email;
  String _password;

  @override
  void initState() {
    super.initState();
    emailController.addListener(
      () => _email = emailController.text,
    );
    emailController.addListener(
      () => _password = passwordController.text,
    );
  }

  @override
  void dispose() {
    super.dispose();
    emailController.dispose();
    passwordController.dispose();
  }

The problem is the Email works properly, the password is always null.

But If I edit the email while there is some text typed to the password, it updates and works fine.

How is that possible, they are almost the same thing?

That's how I call them

@override
  Widget build(BuildContext context) {
    final double width = MediaQuery.of(context).size.width;
    final double height = MediaQuery.of(context).size.height;
    return Scaffold(
      body: SingleChildScrollView(
        child: Builder(
          builder: (context) => Container(
            height: height,
            width: width,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topRight,
                end: Alignment.bottomLeft,
                colors: [
                  Color.fromRGBO(28, 18, 18, 1),
                  Color.fromRGBO(138, 135, 135, 1),
                ],
              ),
            ),
            child: Stack(
              children: [
                EmailField(width, height, emailController),
                PasswordField(width, height, passwordController),

Upvotes: 0

Views: 27

Answers (1)

Nicol&#242; Bozzato
Nicol&#242; Bozzato

Reputation: 86

Check your addListener in _AuthState, they are both related to emailController

So change this

  @override
  void initState() {
    super.initState();
    emailController.addListener(
      () => _email = emailController.text,
    );
    emailController.addListener(
      () => _password = passwordController.text,
    );
  }

to this

  @override
  void initState() {
    super.initState();
    emailController.addListener(
      () => _email = emailController.text,
    );
    passwordController.addListener(
      () => _password = passwordController.text,
    );
  }

Upvotes: 0

Related Questions