Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.86 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:io';
  3.  
  4. import 'package:flutter/material.dart';
  5. import 'package:learn_fazz/app_config.dart';
  6. import 'package:learn_fazz/models/login_result.dart';
  7. import 'package:learn_fazz/pages/dashboard_page.dart';
  8. import 'package:learn_fazz/repositories/auth_repository.dart';
  9. import 'package:learn_fazz/widgets/image_picker_bottom_sheet.dart';
  10. import '../assets/learn_fazz_icons.dart';
  11.  
  12. class RegisterFormPage extends StatefulWidget {
  13. static String tag = '/register-form-page';
  14.  
  15. static String kNoImageErrorMessage =
  16. 'Please choose image first to be able to proceed.';
  17.  
  18. @override
  19. _RegisterFormPageState createState() => _RegisterFormPageState();
  20. }
  21.  
  22. class _RegisterFormPageState extends State<RegisterFormPage> {
  23. final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  24. final GlobalKey<FormFieldState<String>> _passKey =
  25. GlobalKey<FormFieldState<String>>();
  26. final Color borderColor = Colors.black.withOpacity(0.4);
  27. bool passwordVisible = true;
  28. bool confirmPasswordVisible = true;
  29.  
  30. bool _isSubmitting = false;
  31.  
  32. String _name;
  33. String _email;
  34. String _identificationNumber;
  35. String _password;
  36. String _passwordConfirmation;
  37. String _role;
  38. File _imageFile;
  39.  
  40. @override
  41. Widget build(BuildContext context) {
  42. final Widget logo = _imageFile == null
  43. ? Container(
  44. child: CircleAvatar(
  45. radius: 72.0,
  46. backgroundColor: Colors.blue,
  47. foregroundColor: Colors.white,
  48. child: const Icon(LearnFazz.camera, size: 50.0),
  49. ),
  50. padding: const EdgeInsets.all(2.0), // border width
  51. decoration: const BoxDecoration(
  52. color: Colors.white, // border color
  53. shape: BoxShape.circle,
  54. ),
  55. )
  56. : CircleAvatar(
  57. radius: 72.0,
  58. child: ClipOval(child: Image.file(_imageFile)),
  59. );
  60.  
  61. final Widget addPhotoLabel = InkWell(
  62. child: const Text(
  63. 'Add Photo',
  64. key: Key('addPhotoLabel'),
  65. style: TextStyle(
  66. fontWeight: FontWeight.bold,
  67. fontSize: 18.0,
  68. decoration: TextDecoration.underline,
  69. color: Colors.white),
  70. textAlign: TextAlign.center,
  71. ),
  72. onTap: () async {
  73. final File _choosenImage = await showImagePickerBottomSheet(context);
  74. setState(() => _imageFile = _choosenImage);
  75. },
  76. );
  77.  
  78. final TextFormField name = TextFormField(
  79. autofocus: true,
  80. validator: (String value) {
  81. if (value.isEmpty) {
  82. return 'Name cannot be empty';
  83. }
  84. },
  85. onSaved: (String val) => _name = val,
  86. style: const TextStyle(color: Colors.black, fontSize: 16.0),
  87. decoration: InputDecoration(
  88. fillColor: Colors.white,
  89. filled: true,
  90. hintText: 'Name',
  91. contentPadding: const EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  92. border: OutlineInputBorder(
  93. borderRadius: BorderRadius.circular(32.0),
  94. ),
  95. enabledBorder: OutlineInputBorder(
  96. borderRadius: BorderRadius.circular(32.0),
  97. borderSide: BorderSide(width: 1.5, color: borderColor),
  98. ),
  99. prefixIcon: const Icon(LearnFazz.person_outline),
  100. ),
  101. );
  102.  
  103. final TextFormField email = TextFormField(
  104. keyboardType: TextInputType.emailAddress,
  105. validator: (String value) {
  106. if (value.isEmpty) {
  107. return 'Email cannot be empty';
  108. } else if (value.isNotEmpty && !value.contains('@gmail.')) {
  109. return 'Invalid email address';
  110. }
  111. },
  112. onSaved: (String val) => _email = val,
  113. style: const TextStyle(color: Colors.black, fontSize: 16.0),
  114. decoration: InputDecoration(
  115. fillColor: Colors.white,
  116. filled: true,
  117. hintText: 'Email',
  118. contentPadding: const EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  119. border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
  120. enabledBorder: OutlineInputBorder(
  121. borderRadius: BorderRadius.circular(32.0),
  122. borderSide: BorderSide(width: 1.5, color: borderColor),
  123. ),
  124. prefixIcon: const Icon(LearnFazz.mail),
  125. ),
  126. );
  127.  
  128. final TextFormField idNumber = TextFormField(
  129. keyboardType: TextInputType.number,
  130. validator: (String value) {
  131. if (value.isEmpty) {
  132. return 'Identification number cannot be empty';
  133. }
  134. },
  135. onSaved: (String identificationNumber) =>
  136. _identificationNumber = identificationNumber,
  137. style: const TextStyle(color: Colors.black, fontSize: 16.0),
  138. decoration: InputDecoration(
  139. fillColor: Colors.white,
  140. filled: true,
  141. hintText: 'Identification Number',
  142. contentPadding: const EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  143. border: OutlineInputBorder(
  144. borderRadius: BorderRadius.circular(32.0),
  145. ),
  146. enabledBorder: OutlineInputBorder(
  147. borderRadius: BorderRadius.circular(32.0),
  148. borderSide: BorderSide(width: 1.5, color: borderColor),
  149. ),
  150. prefixIcon: const Icon(LearnFazz.id_card),
  151. ),
  152. );
  153.  
  154. final TextFormField password = TextFormField(
  155. validator: (String value) {
  156. if (value.isEmpty) {
  157. return 'Password cannot be empty';
  158. } else if (value.length < 8) {
  159. return 'Password should\'ve at least 8 characters';
  160. }
  161. },
  162. onSaved: (String password) => _password = password,
  163. style: const TextStyle(color: Colors.black, fontSize: 16.0),
  164. obscureText: passwordVisible,
  165. key: _passKey,
  166. decoration: InputDecoration(
  167. fillColor: Colors.white,
  168. filled: true,
  169. hintText: 'Password',
  170. contentPadding: const EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  171. border: OutlineInputBorder(
  172. borderRadius: BorderRadius.circular(32.0),
  173. ),
  174. enabledBorder: OutlineInputBorder(
  175. borderRadius: BorderRadius.circular(32.0),
  176. borderSide: BorderSide(width: 1.5, color: borderColor),
  177. ),
  178. prefixIcon: const Icon(LearnFazz.lock),
  179. suffixIcon: IconButton(
  180. icon: Icon(passwordVisible ? LearnFazz.eye : Icons.visibility_off),
  181. onPressed: () {
  182. setState(() {
  183. passwordVisible
  184. ? passwordVisible = false
  185. : passwordVisible = true;
  186. });
  187. }),
  188. ),
  189. );
  190.  
  191. final TextFormField confirmPassword = TextFormField(
  192. validator: (String value) {
  193. final String password = _passKey.currentState.value;
  194. return (value == password) ? null : 'Password doesn\'t match';
  195. },
  196. onSaved: (String passwordConfirmation) =>
  197. _passwordConfirmation = passwordConfirmation,
  198. style: const TextStyle(color: Colors.black, fontSize: 16.0),
  199. obscureText: confirmPasswordVisible,
  200. decoration: InputDecoration(
  201. fillColor: Colors.white,
  202. filled: true,
  203. hintText: 'Confirm Password',
  204. contentPadding: const EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  205. border: OutlineInputBorder(
  206. borderRadius: BorderRadius.circular(32.0),
  207. ),
  208. enabledBorder: OutlineInputBorder(
  209. borderRadius: BorderRadius.circular(32.0),
  210. borderSide: BorderSide(width: 1.5, color: borderColor),
  211. ),
  212. prefixIcon: const Icon(LearnFazz.lock),
  213. suffixIcon: IconButton(
  214. icon: Icon(
  215. confirmPasswordVisible ? LearnFazz.eye : Icons.visibility_off),
  216. onPressed: () {
  217. setState(() {
  218. confirmPasswordVisible
  219. ? confirmPasswordVisible = false
  220. : confirmPasswordVisible = true;
  221. });
  222. },
  223. ),
  224. ),
  225. );
  226.  
  227. final Widget bottomNavigationBar =
  228. _isSubmitting ? LinearProgressIndicator() : null;
  229.  
  230. return Scaffold(
  231. body: Builder(
  232. builder: (BuildContext context) => Container(
  233. decoration: const BoxDecoration(
  234. image: DecorationImage(
  235. image: AssetImage(
  236. 'lib/assets/images/blue-white-pattern-background.png'),
  237. fit: BoxFit.cover,
  238. )),
  239. child: Form(
  240. key: _formKey,
  241. child: ListView(
  242. padding: const EdgeInsets.only(
  243. top: 100.0, left: 45.0, right: 45.0, bottom: 40.0),
  244. children: <Widget>[
  245. logo,
  246. const SizedBox(height: 10.0),
  247. addPhotoLabel,
  248. const SizedBox(height: 30.0),
  249. name,
  250. const SizedBox(height: 8.0),
  251. email,
  252. const SizedBox(height: 8.0),
  253. idNumber,
  254. const SizedBox(height: 8.0),
  255. password,
  256. const SizedBox(height: 8.0),
  257. confirmPassword,
  258. const SizedBox(height: 30.0),
  259. InkWell(
  260. key: const Key('registerButton'),
  261. onTap: () => _validateThenPostToBackend(context),
  262. child: Container(
  263. height: 50.0,
  264. decoration: BoxDecoration(
  265. color: _isSubmitting
  266. ? Colors.grey
  267. : const Color(0xFF3A8FF3),
  268. borderRadius: BorderRadius.circular(30.0),
  269. ),
  270. child: const Center(
  271. child: Text(
  272. 'Register',
  273. style: TextStyle(fontSize: 18.0, color: Colors.white),
  274. ),
  275. ),
  276. ),
  277. ),
  278. ],
  279. ),
  280. )),
  281. ),
  282. bottomNavigationBar: bottomNavigationBar,
  283. );
  284. }
  285.  
  286. Future<void> _validateThenPostToBackend(BuildContext context) async {
  287. if (_isSubmitting) {
  288. return;
  289. }
  290. if (_formKey.currentState.validate()) {
  291. setState(() => _isSubmitting = true);
  292. _formKey.currentState.save();
  293. try {
  294. if (_imageFile == null) {
  295. throw RegisterFormPage.kNoImageErrorMessage;
  296. }
  297. final AuthRepository authRepository =
  298. AppConfig.of(context).authRepository;
  299. final LoginResult response = await authRepository.register(
  300. name: _name,
  301. email: _email,
  302. identificationNumber: _identificationNumber,
  303. password: _password,
  304. passwordConfirmation: _passwordConfirmation,
  305. role: _role,
  306. photo: _imageFile,
  307. );
  308. Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
  309. builder: (BuildContext context) => DashboardPage(user: response.user),
  310. ));
  311. } catch (e) {
  312. Scaffold.of(context).showSnackBar(SnackBar(
  313. content: Text(e.toString()), duration: const Duration(seconds: 5)));
  314. }
  315. setState(() => _isSubmitting = false);
  316. }
  317. }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement