Advertisement
Zuhairy_Harry

sign_in.dart(old)

May 15th, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import 'package:einventorycomputer/services/auth.dart';
  2. import 'package:einventorycomputer/shared/constants.dart';
  3. import 'package:einventorycomputer/shared/loading.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:einventorycomputer/modules/authentication/sign_up.dart';
  6.  
  7. class SignIn extends StatefulWidget {
  8.  
  9. final Function toggleView;
  10. SignIn({required this.toggleView});
  11.  
  12. @override
  13. _SignInState createState() => _SignInState();
  14. }
  15.  
  16. class _SignInState extends State<SignIn> {
  17.  
  18. final AuthService _auth = AuthService();
  19. final _formKey = GlobalKey<FormState>();
  20. bool loading = false;
  21.  
  22. // text field state
  23. String email = '';
  24. String password = '';
  25. String error = '';
  26.  
  27. @override
  28. Widget build(BuildContext context) {
  29. return loading ? Loading() : Scaffold(
  30. backgroundColor: Colors.brown[100],
  31. appBar: AppBar(
  32. backgroundColor: Colors.brown[400],
  33. elevation: 0.0,
  34. title: Text('Sign In to e-Inventory'),
  35. ),
  36. body: Container(
  37. padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
  38. child: Form(
  39. key: _formKey,
  40. child: Column(
  41. children: <Widget>[
  42. SizedBox(height: 20.0),
  43. TextFormField(
  44. decoration: textDecoration.copyWith(hintText: 'Email'),
  45. validator: (val) => val!.isEmpty ? 'Enter an email' : null,
  46. onChanged: (val){
  47. setState(() => email = val);
  48. }
  49. ),
  50. SizedBox(height: 20.0),
  51. TextFormField(
  52. decoration: textDecoration.copyWith(hintText: 'Password'),
  53. obscureText: true,
  54. validator: (val) => val!.length < 6 ? 'Enter a password 6+ chars long' : null,
  55. onChanged: (val){
  56. setState(() => password = val);
  57. }
  58. ),
  59. ElevatedButton(
  60. child: Text(
  61. 'Sign In',
  62. style: TextStyle(color: Colors.white),
  63. ),
  64. onPressed: () async {
  65. if (_formKey.currentState!.validate()){
  66. setState(() => loading = true);
  67. dynamic result = await _auth.signInWithEmailAndPassword(email, password);
  68. if(result == null){
  69. setState(() {
  70. error = 'could not sign in';
  71. loading = false;
  72. });
  73. }
  74. }
  75. }
  76. ),
  77. SizedBox(height: 12.0),
  78. Text(
  79. error,
  80. style: TextStyle(color: Colors.red, fontSize: 14.0),
  81. ),
  82. SizedBox(height: 20.0),
  83. ElevatedButton(
  84. child: Text(
  85. 'Sign Up',
  86. style: TextStyle(color: Colors.white),
  87. ),
  88. onPressed: () {
  89. widget.toggleView();
  90. }
  91. ),
  92. ],
  93. ),
  94. ),
  95. ),
  96. );
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement