0

i have a login page where the password field has an "obscure" ability which is found in the buildInputForm defined below, to hide and show the password string if clicked, it use to work fine but now, the action is not reflected on the screen, i reverted back to older version of the code yet it did not change. im using VScode

this is the login page

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/services.dart';

import 'package:imporved_main_screens/theme.dart';
import 'package:imporved_main_screens/buttons/green_button.dart';
import 'package:imporved_main_screens/formatters/whitespace_formatter.dart';

final _firebase = FirebaseAuth.instance;

class LogInForm extends StatefulWidget {
  const LogInForm({super.key});
  @override
  _LogInFormState createState() => _LogInFormState();
}

class _LogInFormState extends State<LogInForm> {
  bool _isObscure = true;

  var formKey = GlobalKey<FormState>();
  var emailController = TextEditingController(); //controls user entry
  var passwordController = TextEditingController();

/*
  final _enteredEmail = '';
  final _enteredPassword = '';
*/

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

  @override
  Widget build(BuildContext context) {
    return Form(
      key: formKey,
      child: Column(
        children: [
          // check validity if email or pass is invalide ask user for re-entry
          buildInputForm(
            'Email',
            false,
            emailController,
            (value) {
              if (value == null || value.isEmpty || !value.contains('@')) {
                return 'Please enter your email';
              } else if (!RegExp(r'^[\w-]+(\.[\w-]+)*@iau\.edu\.sa$')
                  .hasMatch(value)) {
                return 'Please use your university email';
              }
              return null;
            },
            (value) {
              emailController.text = value!;
              return null;
            },
            TextInputType.text,
            [
              WhitespaceFilterFormatter(),
              FilteringTextInputFormatter.singleLineFormatter,
              LengthLimitingTextInputFormatter(500),
            ],
          ),
          buildInputForm(
            'Password',
            true,
            passwordController,
            (value) {
              if (value == null || value.isEmpty) {
                return 'Please enter your password';
              }
              return null;
            },
            (value) {
              passwordController.text = value!;
              return null;
            },
            TextInputType.text,
            [
              WhitespaceFilterFormatter(),
              FilteringTextInputFormatter.singleLineFormatter,
              LengthLimitingTextInputFormatter(500),
            ],
          ),
          const SizedBox(height: 30),
          GestureDetector(
            onTap: () {
              if (formKey.currentState!.validate()) {
                formKey.currentState!.save();
                _validUser();
              }
            },
            child: const GreenButton(buttonText: 'Log In'),
          ),
        ],
      ),
    );
  }

//this function sets all needed attributes to be used to create input fields, they can also be created manually
  Padding buildInputForm(
    String hint,
    bool isPassword,
    TextEditingController controller,
    String? Function(String?)? validator,
    void Function(String?)? onSaved,
    TextInputType keyboardType,
    List<TextInputFormatter>? inputFormatters,
  ) {
    if (inputFormatters == null) {
      inputFormatters = [];
    }

    inputFormatters.insert(0, WhitespaceFilterFormatter());

    inputFormatters.add(WhitespaceFilterFormatter());

    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 5),
      child: TextFormField(
        obscureText: isPassword,
        controller: controller,
        decoration: InputDecoration(
          hintText: hint,
          hintStyle: const TextStyle(color: kTextFieldColor),
          focusedBorder: const UnderlineInputBorder(
            borderSide: BorderSide(color: kPrimaryColor),
          ),
          suffixIcon: isPassword
              ? IconButton(
                  onPressed: () {
                    setState(() {
                      _isObscure = !_isObscure;
                    });
                  },
                  icon: _isObscure
                      ? const Icon(
                          Icons.visibility_off,
                          color: kTextFieldColor,
                        )
                      : const Icon(
                          Icons.visibility,
                          color: kPrimaryColor,
                        ),
                )
              : null,
        ),
        validator: validator,
        onSaved: onSaved,
        keyboardType: keyboardType,
        inputFormatters: inputFormatters,
      ),
    );
  }

//validating user
  void _validUser() async {
    try {
      final userCredintials = await _firebase.signInWithEmailAndPassword(
          email: emailController.text, password: passwordController.text);
    } on FirebaseAuthException catch (error) {
      if (error.code == 'email-already-in-use') {}
      ScaffoldMessenger.of(context).clearSnackBars();
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(error.message ?? 'Authentication failed.'),
        ),
      );
    }
  }
}
kawthar
  • 15
  • 5

1 Answers1

1

Your code has logical bug.

Use this on buildInputForm

 obscureText: isPassword ? _isObscure : false,