Home > Net >  I am new to programming can someone help me what's the problem here, I was doing a login page a
I am new to programming can someone help me what's the problem here, I was doing a login page a

Time:08-26

If password and username is same, I want to print 'username and password is same' statement in the terminal .But it is only printing the second statement 'username and password does not match even if username and password is same or not . I don't understand why this happened somebody help, I am new here' ( NB : problem is inside the function named checkLogin)

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

  @override
  State<ScreenLogin> createState() => _ScreenLoginState();
}

class _ScreenLoginState extends State<ScreenLogin> {
  final _usernameController = TextEditingController();

  final _passwordController = TextEditingController();

  bool _isDataMatched = false;

  final _formkey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
      child: Padding(
        padding: const EdgeInsets.all(15.0),
        child: Form(
          key: _formkey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              TextFormField(
                controller: _usernameController,
                decoration: const InputDecoration(
                    border: OutlineInputBorder(), hintText: 'Username'),
                validator: (value) {
                 
                  if (value == null || value.isEmpty) {
                    return 'value is empty';
                  } else {
                    return null;
                  }
                },
              ),
              const SizedBox(
                height: 20,
              ),
              TextFormField(
                  controller: _passwordController,
                  obscureText: true,
                  decoration: const InputDecoration(
                      border: OutlineInputBorder(), hintText: 'Password'),
                  validator: (value) {
                   
                    if (value == null || value.isEmpty) {
                      return 'value is empty';
                    } else {
                      return null;
                    }
                  }),
              SizedBox(
                height: 20,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Visibility(
                    visible: _isDataMatched,
                    child: Text(
                      'Username and password does not match',
                      style: TextStyle(color: Colors.red),
                    ),
                  ),
                  ElevatedButton.icon(
                      onPressed: () {
                        if (_formkey.currentState!.validate()) {
                          checkLogin(context);
                        } else {
                          print('Data Empty');
                        }
                      },
                      icon: const Icon(Icons.check),
                      label: const Text('Login ')),
                ],
              )
            ],
          ),
        ),
      ),
    ));
  }

  void checkLogin(BuildContext context) {
    final _username = _usernameController;
    final _password = _passwordController;
    if (_password == _username) {
      print('Username and password is matching');
    } else {
      print('Username and password does not match');
    }
  }
}

CodePudding user response:

To get text, you need to use .text on controller.

_usernameController.text;
void checkLogin(BuildContext context) {
  final _username = _usernameController.text;
  final _password = _passwordController.text;
  if (_password == _username) {
    print('Username and password is matching');
  } else {
    print('Username and password does not match');
  }
}

CodePudding user response:

In login check you should do this:

void checkLogin(BuildContext context) {
    final _username = _usernameController.text;
    final _password = _passwordController.text;
    if (_password == _username) {
      print('Username and password is matching');
    } else {
      print('Username and password does not match');
    }
  }
  • Related