import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:beam_messenger/auth.dart'; import 'package:beam_messenger/data/database_helper.dart'; import 'package:beam_messenger/models/user.dart'; import 'package:beam_messenger/screens/login/login_screen_presenter.dart'; class LoginScreen extends StatefulWidget { @override State createState() { return new LoginScreenState(); } } class LoginScreenState extends State implements LoginScreenContract, AuthStateListener { BuildContext _ctx; bool _isLoading = false; final formKey = new GlobalKey(); final scaffoldKey = new GlobalKey(); String _password, _email; LoginScreenPresenter _presenter; LoginScreenState() { _presenter = new LoginScreenPresenter(this); var authStateProvider = new AuthStateProvider(); authStateProvider.subscribe(this); } void _submit() { final form = formKey.currentState; if (form.validate()) { setState(() => _isLoading = true); form.save(); _presenter.doLogin(_email, _password); } } void _showSnackBar(String text) { scaffoldKey.currentState .showSnackBar(new SnackBar(content: new Text(text))); } @override onAuthStateChanged(AuthState state) { if (state == AuthState.LOGGED_IN) Navigator.of(_ctx).pushReplacementNamed("/home"); } @override Widget build(BuildContext context) { _ctx = context; var loginBtn = new RaisedButton( onPressed: _submit, child: new Text("LOGIN"), color: Colors.primaries[0], ); var loginForm = new Column( children: [ new Text( "Login", textScaleFactor: 2.0, ), new Form( key: formKey, child: new Column( children: [ new Padding( padding: const EdgeInsets.all(8.0), child: new TextFormField( onSaved: (val) => _email = val, validator: (val) { return val.length < 10 ? "email must have atleast 10 chars" : null; }, decoration: new InputDecoration(labelText: "email"), ), ), new Padding( padding: const EdgeInsets.all(8.0), child: new TextFormField( onSaved: (val) => _password = val, decoration: new InputDecoration(labelText: "Password"), ), ), ], ), ), _isLoading ? new CircularProgressIndicator() : loginBtn ], crossAxisAlignment: CrossAxisAlignment.center, ); return new Scaffold( appBar: null, key: scaffoldKey, body: new Container( decoration: new BoxDecoration( image: new DecorationImage( image: new AssetImage("assets/login_background.jpg"), fit: BoxFit.cover), ), child: new Center( child: new ClipRect( child: new BackdropFilter( filter: new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0), child: new Container( child: loginForm, height: 300.0, width: 300.0, decoration: new BoxDecoration( color: Colors.grey.shade200.withOpacity(0.5)), ), ), ), ), ), ); } @override void onLoginError(String errorTxt) { _showSnackBar(errorTxt); setState(() => _isLoading = false); } @override void onLoginSuccess(User user) async { _showSnackBar(user.toString()); setState(() => _isLoading = false); var db = new DatabaseHelper(); await db.saveUser(user); var authStateProvider = new AuthStateProvider(); authStateProvider.notify(AuthState.LOGGED_IN); } }