· 5 years ago · Jul 29, 2020, 01:44 PM
1import 'package:flutter/material.dart';
2import 'package:intl/intl.dart';
3
4import '../widgets/dialogs.dart';
5import '../models/patient.dart';
6import '../networking/api.dart' as api;
7
8class RegisterPage extends StatefulWidget {
9 static const routeName = '/register';
10
11 @override
12 _RegisterPageState createState() => _RegisterPageState();
13}
14
15class _RegisterPageState extends State<RegisterPage> {
16 String _sex = 'male';
17 DateTime _selectedDate;
18 final _formKey = GlobalKey<FormState>();
19 final _dob = TextEditingController();
20 final _pass = TextEditingController();
21 final _name = TextEditingController();
22 final _mobileNo = TextEditingController();
23
24 void registerAction(BuildContext context) async {
25 Patient patient = Patient(
26 name: _name.text,
27 mobileNo: _mobileNo.text,
28 dob: _selectedDate,
29 sex: _sex,
30 password: _pass.text,
31 );
32 bool success = await showDialog<bool>(
33 context: context,
34 barrierDismissible: false,
35 builder: (context) {
36 api.patientRegister(patient).then((data) {
37 Navigator.pop<bool>(context, true);
38 }, onError: (e) {
39 print(e.toString());
40 Navigator.pop<bool>(context, false);
41 });
42 return LoadingDialog(message: 'Please Wait');
43 },
44 );
45 if (success) {
46 print('Successfully Registered');
47 await successDialog(context, 'Successfully Registered');
48 Navigator.pop(context);
49 } else {
50 await failureDialog(context, 'Registration Failed');
51 }
52 }
53
54 @override
55 void dispose() {
56 _dob.dispose();
57 _pass.dispose();
58 super.dispose();
59 }
60
61 @override
62 Widget build(BuildContext context) {
63 return Scaffold(
64 appBar: AppBar(
65 centerTitle: true,
66 title: Text('ShasthoSheba'),
67 ),
68 body: Center(
69 child: Form(
70 key: _formKey,
71 child: ListView(
72 shrinkWrap: true,
73 children: <Widget>[
74 Card(
75 child: Container(
76 padding: EdgeInsets.all(35),
77 child: Column(
78 mainAxisSize: MainAxisSize.min,
79 children: <Widget>[
80 Align(
81 alignment: Alignment.center,
82 child: Text(
83 'Register',
84 style: Theme.of(context).textTheme.headline5,
85 ),
86 ),
87 SizedBox(
88 height: 25,
89 ),
90 TextFormField(
91 controller: _name,
92 decoration: InputDecoration(
93 labelText: 'Name',
94 ),
95 validator: (value) {
96 if (value.isEmpty) {
97 return 'Please enter your name';
98 }
99 return null;
100 },
101 ),
102 SizedBox(
103 height: 20,
104 ),
105 Row(
106 children: <Widget>[
107 Text('Gender:'),
108 Radio(
109 value: 'male',
110 groupValue: _sex,
111 onChanged: (String value) {
112 setState(() {
113 _sex = value;
114 });
115 },
116 ),
117 Text('Male'),
118 Radio(
119 value: 'female',
120 groupValue: _sex,
121 onChanged: (String value) {
122 setState(() {
123 _sex = value;
124 });
125 },
126 ),
127 Text('Female'),
128 ],
129 ),
130 SizedBox(
131 height: 20,
132 ),
133 TextFormField(
134 controller: _mobileNo,
135 decoration: InputDecoration(
136 labelText: 'Mobile No.',
137 ),
138 validator: (value) {
139 if (value.isEmpty) {
140 return "Please enter your Mobile Number";
141 }
142 return null;
143 },
144 ),
145 SizedBox(
146 height: 20,
147 ),
148 TextFormField(
149 readOnly: true,
150 controller: _dob,
151 decoration: InputDecoration(
152 labelText: 'Date of Birth',
153 suffixIcon: Icon(Icons.date_range),
154 ),
155 validator: (value) {
156 if (value.isEmpty) {
157 return "Please enter your Date of Birth";
158 }
159 return null;
160 },
161 onTap: () async {
162 DateTime selectedDate = await showDatePicker(
163 context: context,
164 firstDate: DateTime.now().subtract(
165 Duration(
166 days: 36500,
167 ),
168 ),
169 initialDate: _selectedDate == null
170 ? DateTime.now()
171 : _selectedDate,
172 initialDatePickerMode: DatePickerMode.year,
173 lastDate: DateTime.now());
174 if (selectedDate != null) {
175 _selectedDate = selectedDate;
176 setState(() => _dob.text =
177 DateFormat.yMMMMd('en_US')
178 .format(_selectedDate));
179 }
180 },
181 ),
182 SizedBox(
183 height: 20,
184 ),
185 TextFormField(
186 controller: _pass,
187 obscureText: true,
188 decoration: InputDecoration(
189 labelText: 'Password',
190 ),
191 validator: (value) {
192 if (value.isEmpty) {
193 return "Please provide a password";
194 }
195 return null;
196 },
197 ),
198 SizedBox(
199 height: 20,
200 ),
201 TextFormField(
202 obscureText: true,
203 decoration: InputDecoration(
204 labelText: 'Confirm Password',
205 ),
206 validator: (value) {
207 if (value != _pass.text) {
208 return 'Password does not match';
209 }
210 return null;
211 },
212 ),
213 SizedBox(
214 height: 15,
215 ),
216 Align(
217 alignment: Alignment.centerRight,
218 child: OutlineButton(
219 borderSide:
220 BorderSide(color: Theme.of(context).primaryColor),
221 onPressed: () {
222 if (_formKey.currentState.validate()) {
223 registerAction(context);
224 }
225 },
226 child: Text(
227 'Register',
228 style: Theme.of(context).textTheme.button,
229 ),
230 ),
231 ),
232 ],
233 ),
234 ),
235 margin: EdgeInsets.only(left: 25, right: 25),
236 ),
237 ],
238 ),
239 ),
240 ),
241 );
242 }
243}
244