· 4 months ago · May 15, 2025, 02:55 PM
1import 'package:einventorycomputer/modules/authentication/sign_in.dart';
2import 'package:einventorycomputer/shared/constants.dart';
3import 'package:einventorycomputer/shared/loading.dart';
4import 'package:flutter/material.dart';
5import 'package:einventorycomputer/services/auth.dart';
6
7class SignUp extends StatefulWidget {
8
9 final Function toggleView;
10 SignUp({required this.toggleView});
11
12 @override
13 _SignUpState createState() => _SignUpState();
14
15}
16
17class _SignUpState extends State<SignUp> {
18
19 final AuthService _auth = AuthService();
20 final _formKey = GlobalKey<FormState>();
21 bool loading = false;
22
23 // text field state
24 String email = '';
25 String password = '';
26 String error = '';
27
28 @override
29 Widget build(BuildContext context) {
30 return loading ? Loading() : Scaffold(
31 backgroundColor: Colors.brown[100],
32 appBar: AppBar(
33 backgroundColor: Colors.brown[400],
34 elevation: 0.0,
35 title: Text('Sign Up to e-Inventory'),
36 ),
37 body: Container(
38 padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
39 child: Form(
40 key: _formKey,
41 child: Column(
42 children: <Widget>[
43 SizedBox(height: 20.0),
44 TextFormField(
45 decoration: InputDecoration(
46 labelText: 'Username',
47 border: OutlineInputBorder(
48 borderRadius: BorderRadius.circular(40.0), // Rounded corners
49 borderSide: const BorderSide(color: Colors.grey),
50 ),
51 focusedBorder: OutlineInputBorder(
52 borderRadius: BorderRadius.circular(40.0),
53 borderSide: const BorderSide(color: Colors.blue),
54 ),
55 prefixIcon: const Icon(Icons.person),
56 ),
57 validator: (val) => val!.isEmpty ? 'Enter an email' : null,
58 onChanged: (val){
59 setState(() => email = val);
60 }
61 ),
62 SizedBox(height: 20.0),
63 TextFormField(
64 decoration: textDecoration.copyWith(hintText: 'Password'),
65 obscureText: true,
66 validator: (val) => val!.length < 6 ? 'Enter a password 6+ chars long' : null,
67 onChanged: (val){
68 setState(() => password = val);
69 }
70 ),
71 ElevatedButton(
72 child: Text(
73 'Register',
74 style: TextStyle(color: Colors.white),
75 ),
76 onPressed: () async {
77 if (_formKey.currentState!.validate()){
78 setState(() => loading = true);
79 dynamic result = await _auth.registerWithEmailAndPassword(email, password);
80 if(result == null){
81 setState(() {
82 error = 'Please supply a valid email';
83 loading = false;
84 });
85 }
86 }
87 }
88 ),
89 SizedBox(height: 12.0),
90 Text(
91 error,
92 style: TextStyle(color: Colors.red, fontSize: 14.0),
93 ),
94 SizedBox(height: 20.0),
95 ElevatedButton(
96 child: Text(
97 'Sign In',
98 style: TextStyle(color: Colors.white),
99 ),
100 onPressed: () {
101 widget.toggleView();
102 }
103 ),
104 ],
105 ),
106 ),
107 ),
108 );
109 }
110}