import 'package:comunic/helpers/account_helper.dart'; import 'package:comunic/models/authentication_details.dart'; import 'package:comunic/ui/routes/create_account_route.dart'; import 'package:comunic/ui/routes/home_route.dart'; import 'package:comunic/utils/input_utils.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; /// Login route /// /// @author Pierre HUBERT class LoginRoute extends StatefulWidget { @override State createState() => _LoginRouteState(); } class _LoginRouteState extends State { String _currEmail; String _currPassword; AuthResult _authResult; bool _loading = false; @override void initState() { super.initState(); _currEmail = ""; _currPassword = ""; } void _emailChanged(String newEmail) { setState(() { _currEmail = newEmail; }); } void _passwordChanged(String newValue) { setState(() { _currPassword = newValue; }); } /// Call this whenever the user request to perform login Future _submitForm(BuildContext context) async { setState(() { _loading = true; }); final loginResult = await AccountHelper().signIn( AuthenticationDetails(email: _currEmail, password: _currPassword)); if (loginResult == AuthResult.SUCCESS) Navigator.pushReplacement( context, MaterialPageRoute(builder: (b) => HomeRoute())); else setState(() { _authResult = loginResult; _loading = false; }); } void _openCreateAccountPage() { Navigator.of(context) .push(MaterialPageRoute(builder: (c) => CreateAccountRoute())); } /// Build error card Widget _buildErrorCard() { if (_authResult == null) return null; //Determine the right message final message = (_authResult == AuthResult.INVALID_CREDENTIALS ? tr("Invalid credentials!") : (_authResult == AuthResult.TOO_MANY_ATTEMPTS ? tr( "Too many unsuccessfull login attempts! Please try again later...") : tr("A network error occured!"))); return buildErrorCard(message); } /// Build login form Widget _buildLoginForm() { // Whether the form can be submitted or not final valid = validateEmail(_currEmail) && _currPassword.length >= 3; return SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(8.0), child: Column( children: [ Text(tr("Please sign into your Comunic account: ")), Container( child: _buildErrorCard(), ), //Email address TextField( keyboardType: TextInputType.emailAddress, decoration: InputDecoration( labelText: tr("Email address"), alignLabelWithHint: true, errorText: _currEmail.length > 0 && !validateEmail(_currEmail) ? tr("Invalid email address!") : null), onChanged: _emailChanged, ), //Password TextField( obscureText: true, decoration: InputDecoration( labelText: tr("Password"), alignLabelWithHint: true, ), onChanged: _passwordChanged, onSubmitted: valid ? (s) => _submitForm(context) : null, ), Container( padding: EdgeInsets.all(8.0), child: _loading ? CircularProgressIndicator() : RaisedButton( child: Text(tr("Sign in")), onPressed: valid ? () => _submitForm(context) : null, ), ), InkWell( child: Text( tr("Create an account"), style: TextStyle(color: Colors.blue), ), onTap: () => _openCreateAccountPage(), ), ], ), ), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Comunic"), ), body: _buildLoginForm()); } }