· 4 years ago · May 12, 2021, 06:44 AM
1import 'package:flutter/material.dart';
2import 'package:flutter/services.dart';
3import 'package:scoreapp/api/api_service.dart';
4import 'package:scoreapp/models/register_student_model.dart';
5
6class StudentRegistration extends StatefulWidget {
7 @override
8 _StudentRegistrationState createState() => _StudentRegistrationState();
9}
10
11class _StudentRegistrationState extends State<StudentRegistration> {
12 // String _firstName, _lastName, _username, _email;
13
14 TextEditingController _pwd = TextEditingController();
15 TextEditingController _confirmPwd = TextEditingController();
16
17 final scaffoldKey = GlobalKey<ScaffoldState>();
18 GlobalKey<FormState> globalFormKey = new GlobalKey<FormState>();
19 bool _hidePassword = true;
20 bool _hideCPassword = true;
21
22 RegisterStudentRequestModel requestModel;
23
24 @override
25 void initState() {
26 super.initState();
27 requestModel = new RegisterStudentRequestModel();
28 }
29
30 @override
31 Widget build(BuildContext context) {
32 return Scaffold(
33 key: scaffoldKey,
34 body: SingleChildScrollView(
35 child: Column(
36 children: <Widget>[
37 Stack(
38 children: <Widget>[
39 Container(
40 width: double.infinity,
41 padding: EdgeInsets.symmetric(vertical: 30, horizontal: 20),
42 margin: EdgeInsets.symmetric(vertical: 30, horizontal: 20),
43 decoration: BoxDecoration(
44 // color: Theme.of(context).primaryColor,
45 borderRadius: BorderRadius.circular(20),
46 ),
47 child: Form(
48 key: globalFormKey,
49 child: Column(
50 children: <Widget>[
51 SizedBox(
52 height: 150,
53 ),
54 Text(
55 "Sign Up",
56 style: Theme.of(context).textTheme.headline2,
57 ),
58 SizedBox(
59 height: 25,
60 ),
61 // full name field
62 // Text("Enter Your Name:"),
63 // first name
64 new TextFormField(
65 keyboardType: TextInputType.text,
66 onSaved: (input) => requestModel.fName = input,
67 // onSaved: (input) => _firstName = input,
68 decoration: new InputDecoration(
69 // border: Border,
70 hintText: "First Name",
71 // prefixIcon: Icon(Icons.email),
72 ),
73 validator: (String input) => input.isEmpty
74 ? "Please, enter your first name"
75 : null,
76 ),
77
78 // last name
79 new TextFormField(
80 keyboardType: TextInputType.text,
81 onSaved: (input) => requestModel.lName = input,
82 // onSaved: (input) => _lastName = input,
83 decoration: new InputDecoration(
84 hintText: "Last Name",
85 // prefixIcon: Icon(Icons.email),
86 ),
87 validator: (String input) => input.isEmpty
88 ? "Please, enter your last name"
89 : null,
90 ),
91
92 // username field
93 new TextFormField(
94 keyboardType: TextInputType.text,
95 onSaved: (input) =>
96 requestModel.username = input,
97 // onSaved: (input) => _username = input,
98 decoration: new InputDecoration(
99 hintText: "Username",
100 // prefixIcon: Icon(Icons.email),
101 ),
102 validator: (input) {
103 if (!RegExp(r"^\w{2,32}").hasMatch(input)) {
104 return "only a-z, 0-9, underscore are acceptable ranging 2 to 32 characters";
105 }
106 return null;
107 }),
108
109 //emailfield
110 new TextFormField(
111 keyboardType: TextInputType.emailAddress,
112 onSaved: (input) => requestModel.email = input,
113 // onSaved: (input) => _email = input,
114 validator: (input) =>
115 !RegExp(r"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}")
116 .hasMatch(input)
117 ? "Invalid Email"
118 : null,
119 decoration: new InputDecoration(
120 hintText: "Email Address",
121 prefixIcon: Icon(Icons.email),
122 ),
123 ),
124
125 // password field
126 new TextFormField(
127 keyboardType: TextInputType.text,
128 controller: _pwd,
129 obscureText: _hidePassword,
130 // onSaved not required when controller is used
131 validator: (input) => input.length < 8
132 ? "Password must be at least 5 characters long"
133 : null,
134
135 decoration: new InputDecoration(
136 hintText: "Password",
137 prefixIcon: Icon(Icons.lock),
138 suffixIcon: IconButton(
139 icon: Icon(_hidePassword
140 ? Icons.visibility_off
141 : Icons.visibility),
142 onPressed: () {
143 setState(() {
144 _hidePassword = !_hidePassword;
145 });
146 }),
147 ),
148 ),
149
150 //confirm password field
151 new TextFormField(
152 keyboardType: TextInputType.text,
153 controller: _confirmPwd,
154 // onSaved not required when controller is used ,
155 obscureText: _hideCPassword,
156 validator: (input) {
157 if (_pwd.text != _confirmPwd.text) {
158 return "Password did not match!";
159 }
160 return null;
161 },
162
163 decoration: new InputDecoration(
164 hintText: "Confirm Password",
165 prefixIcon: Icon(Icons.lock),
166 suffixIcon: IconButton(
167 icon: Icon(_hideCPassword
168 ? Icons.visibility_off
169 : Icons.visibility),
170 onPressed: () {
171 setState(() {
172 _hideCPassword = !_hideCPassword;
173 });
174 }),
175 ),
176 ),
177 SizedBox(
178 height: 25,
179 ),
180
181 // signup button using material
182 Material(
183 elevation: 5.0,
184 borderRadius: BorderRadius.circular(20),
185 color: Colors.lightGreen,
186 child: MaterialButton(
187 minWidth: MediaQuery.of(context)
188 .size
189 .width, //sets minimum width as of size of screen
190 padding: EdgeInsets.all(20),
191 onPressed: () {
192 // // call the ogin api if credential is valid
193 if (validateAndSave()) {
194 print(requestModel.toJson());
195
196 } else {
197 throw Exception(
198 "client side validation error");
199 }
200 },
201 child: Text("Sign up"),
202 ),
203 ),
204 TextButton(
205 style: ButtonStyle(
206 foregroundColor:
207 MaterialStateProperty.all<Color>(
208 Colors.blue),
209 ),
210 onPressed: () {
211 Navigator.pushNamed(context, "/login");
212 },
213 child: Text('Login Here!'),
214 )
215 ],
216 )))
217 ],
218 ),
219 ],
220 ),
221 ),
222 );
223 }
224
225 bool validateAndSave() {
226 final form = globalFormKey.currentState;
227 if (form.validate()) {
228 form.save();
229 return true;
230 } else {
231 return false;
232 }
233 }
234}
235