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