Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import '../services/authentication.dart';
  3.  
  4. class LoginSignUpPage extends StatefulWidget {
  5. LoginSignUpPage({this.auth, this.onSignedIn});
  6.  
  7. final BaseAuth auth;
  8. final VoidCallback onSignedIn;
  9.  
  10. @override
  11. State<StatefulWidget> createState() => new _LoginSignUpPageState();
  12. }
  13.  
  14. enum FormMode { LOGIN, SIGNUP }
  15.  
  16. class _LoginSignUpPageState extends State<LoginSignUpPage> {
  17. final _formKey = new GlobalKey<FormState>();
  18.  
  19. String _email;
  20. String _password;
  21. String _errorMessage;
  22.  
  23. // Initial form is login form
  24. FormMode _formMode = FormMode.LOGIN;
  25. bool _isIos;
  26. bool _isLoading;
  27.  
  28. // Check if form is valid before perform login or signup
  29. bool _validateAndSave() {
  30. final form = _formKey.currentState;
  31. if (form.validate()) {
  32. form.save();
  33. return true;
  34. }
  35. return false;
  36. }
  37.  
  38. // Perform login or signup
  39. void _validateAndSubmit() async {
  40. setState(() {
  41. _errorMessage = "";
  42. _isLoading = true;
  43. });
  44. if (_validateAndSave()) {
  45. String userId = "";
  46. try {
  47. if (_formMode == FormMode.LOGIN) {
  48. userId = await widget.auth.signIn(_email, _password);
  49. print('Signed in: $userId');
  50. } else {
  51. userId = await widget.auth.signUp(_email, _password);
  52. widget.auth.sendEmailVerification();
  53. _showVerifyEmailSentDialog();
  54. print('Signed up user: $userId');
  55. }
  56. setState(() {
  57. _isLoading = false;
  58. });
  59.  
  60. if (userId.length > 0 && userId != null && _formMode == FormMode.LOGIN) {
  61. widget.onSignedIn();
  62. }
  63.  
  64. } catch (e) {
  65. print('Error: $e');
  66. setState(() {
  67. _isLoading = false;
  68. if (_isIos) {
  69. _errorMessage = e.details;
  70. } else
  71. _errorMessage = e.message;
  72. });
  73. }
  74. }
  75. }
  76.  
  77.  
  78. @override
  79. void initState() {
  80. _errorMessage = "";
  81. _isLoading = false;
  82. super.initState();
  83. }
  84.  
  85. void _changeFormToSignUp() {
  86. _formKey.currentState.reset();
  87. _errorMessage = "";
  88. setState(() {
  89. _formMode = FormMode.SIGNUP;
  90. });
  91. }
  92.  
  93. void _changeFormToLogin() {
  94. _formKey.currentState.reset();
  95. _errorMessage = "";
  96. setState(() {
  97. _formMode = FormMode.LOGIN;
  98. });
  99. }
  100.  
  101. @override
  102. Widget build(BuildContext context) {
  103. _isIos = Theme.of(context).platform == TargetPlatform.iOS;
  104. return new Scaffold(
  105. appBar: new AppBar(
  106. title: new Text('Flutter Firebase Authentication'),
  107. ),
  108. body: Stack(
  109. children: <Widget>[
  110. _showBody(),
  111. _showCircularProgress(),
  112. ],
  113. ));
  114. }
  115.  
  116. Widget _showCircularProgress(){
  117. if (_isLoading) {
  118. return Center(child: CircularProgressIndicator());
  119. } return Container(height: 0.0, width: 0.0,);
  120.  
  121. }
  122.  
  123. void _showVerifyEmailSentDialog() {
  124. showDialog(
  125. context: context,
  126. builder: (BuildContext context) {
  127. // return object of type Dialog
  128. return AlertDialog(
  129. title: new Text("Verify your account"),
  130. content: new Text("Link to verify account has been sent to your email"),
  131. actions: <Widget>[
  132. new FlatButton(
  133. child: new Text("Dismiss"),
  134. onPressed: () {
  135. _changeFormToLogin();
  136. Navigator.of(context).pop();
  137. },
  138. ),
  139. ],
  140. );
  141. },
  142. );
  143. }
  144.  
  145. Widget _showBody(){
  146. return new Container(
  147. padding: EdgeInsets.all(16.0),
  148. child: new Form(
  149. key: _formKey,
  150. child: new ListView(
  151. shrinkWrap: true,
  152. children: <Widget>[
  153. _showLogo(),
  154. _showEmailInput(),
  155. _showPasswordInput(),
  156. _showPrimaryButton(),
  157. _showSecondaryButton(),
  158. _showErrorMessage(),
  159. ],
  160. ),
  161. ));
  162. }
  163.  
  164. Widget _showErrorMessage() {
  165. if (_errorMessage.length > 0 && _errorMessage != null) {
  166. return new Text(
  167. _errorMessage,
  168. style: TextStyle(
  169. fontSize: 13.0,
  170. color: Colors.red,
  171. height: 1.0,
  172. fontWeight: FontWeight.w300),
  173. );
  174. } else {
  175. return new Container(
  176. height: 0.0,
  177. );
  178. }
  179. }
  180.  
  181. Widget _showLogo() {
  182. return new Hero(
  183. tag: 'hero',
  184. child: Padding(
  185. padding: EdgeInsets.fromLTRB(0.0, 30.0, 0.0, 0.0),
  186. child: CircleAvatar(
  187. backgroundColor: Colors.transparent,
  188. radius: 48.0,
  189. child: Image.asset('assets/images/logo.png'),
  190. ),
  191. ),
  192. );
  193. }
  194.  
  195. Widget _showEmailInput() {
  196. return Padding(
  197. padding: const EdgeInsets.fromLTRB(0.0, 100.0, 0.0, 0.0),
  198. child: new TextFormField(
  199. maxLines: 1,
  200. keyboardType: TextInputType.emailAddress,
  201. autofocus: false,
  202. decoration: new InputDecoration(
  203. hintText: 'Email',
  204. icon: new Icon(
  205. Icons.mail,
  206. color: Colors.grey,
  207. )),
  208. validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
  209. onSaved: (value) => _email = value.trim(),
  210. ),
  211. );
  212. }
  213.  
  214. Widget _showPasswordInput() {
  215. return Padding(
  216. padding: const EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
  217. child: new TextFormField(
  218. maxLines: 1,
  219. obscureText: true,
  220. autofocus: false,
  221. decoration: new InputDecoration(
  222. hintText: 'Password',
  223. icon: new Icon(
  224. Icons.lock,
  225. color: Colors.grey,
  226. )),
  227. validator: (value) => value.isEmpty ? 'Password can\'t be empty' : null,
  228. onSaved: (value) => _password = value.trim(),
  229. ),
  230. );
  231. }
  232.  
  233.  
  234.  
  235. Widget _showSecondaryButton() {
  236. return new FlatButton(
  237. child: _formMode == FormMode.LOGIN
  238. ? new Text('Create an account',
  239. style: new TextStyle(fontSize: 18.0, fontWeight: FontWeight.w300))
  240. : new Text('Have an account? Sign in',
  241. style:
  242. new TextStyle(fontSize: 18.0, fontWeight: FontWeight.w300)),
  243. onPressed: _formMode == FormMode.LOGIN
  244. ? _changeFormToSignUp
  245. : _changeFormToLogin,
  246. );
  247. }
  248.  
  249. Widget _showPrimaryButton() {
  250. return new Padding(
  251. padding: EdgeInsets.fromLTRB(0.0, 45.0, 0.0, 0.0),
  252. child: SizedBox(
  253. height: 40.0,
  254. child: new RaisedButton(
  255. elevation: 5.0,
  256. shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
  257. color: Colors.blue,
  258. child: _formMode == FormMode.LOGIN
  259. ? new Text('Login',
  260. style: new TextStyle(fontSize: 20.0, color: Colors.white))
  261. : new Text('Create account',
  262. style: new TextStyle(fontSize: 20.0, color: Colors.white)),
  263. onPressed: _validateAndSubmit,
  264. ),
  265. ));
  266. }
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement