logIn.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import 'package:flutter/material.dart';
  2. import 'Services/AuthService.dart';
  3. class LogIn extends StatefulWidget {
  4. const LogIn({Key? key, required this.title}) : super(key: key);
  5. final String title;
  6. @override
  7. //_RegisterScreen createState() => _RegisterScreen();
  8. State<LogIn> createState() => _LogIn();
  9. }
  10. class _LogIn extends State<LogIn> {
  11. TextEditingController _email = TextEditingController();
  12. TextEditingController _passwd = TextEditingController();
  13. final AuthenticationServices _auth = AuthenticationServices();
  14. @override
  15. Widget build(BuildContext context) {
  16. return Scaffold(
  17. appBar: AppBar(
  18. title: const Text('Login Screen'),
  19. ),
  20. body: Center(
  21. child: SizedBox(
  22. width: 300,
  23. child: Column(children: <Widget>[
  24. const SizedBox(height: 30),
  25. Text(
  26. 'Welcome back!',
  27. style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
  28. ),
  29. const SizedBox(height: 30),
  30. Image.asset(
  31. 'assets/hello.png',
  32. width: 150,
  33. ),
  34. const SizedBox(height: 30),
  35. TextFormField(
  36. controller: _email,
  37. decoration: InputDecoration(
  38. hintText: 'Mail',
  39. ),
  40. ),
  41. const SizedBox(height: 10),
  42. TextFormField(
  43. controller: _passwd,
  44. obscureText: true,
  45. enableSuggestions: false,
  46. autocorrect: false,
  47. decoration: InputDecoration(
  48. hintText: 'Password',
  49. ),
  50. ),
  51. const SizedBox(height: 10),
  52. ElevatedButton(
  53. onPressed: () {
  54. signInUser();
  55. },
  56. child: const Text('Log In'),
  57. ),
  58. ]),
  59. )),
  60. );
  61. }
  62. void signInUser() async {
  63. dynamic authResult = await AuthenticationServices.loginUser(_email.text, _passwd.text);
  64. if (authResult == null) {
  65. showDialog<String>(
  66. context: context,
  67. builder: (BuildContext context) => AlertDialog(
  68. title: const Text('Log In - Error'),
  69. content: const Text('User or password are not correct'),
  70. actions: <Widget>[
  71. TextButton(
  72. onPressed: () => Navigator.pop(context, 'OK'),
  73. child: const Text('OK'),
  74. ),
  75. ],
  76. ));
  77. } else {
  78. _email.clear();
  79. _passwd.clear();
  80. print("Succesful login ");
  81. Navigator.pushNamed(context, '/mainPage',
  82. arguments: {'uid': authResult.uid},
  83. );
  84. }
  85. }
  86. }