diff options
Diffstat (limited to 'lib/screens')
-rw-r--r-- | lib/screens/home/home_screen.dart | 14 | ||||
-rw-r--r-- | lib/screens/login/login_screen.dart | 141 | ||||
-rw-r--r-- | lib/screens/login/login_screen_presenter.dart | 19 |
3 files changed, 174 insertions, 0 deletions
diff --git a/lib/screens/home/home_screen.dart b/lib/screens/home/home_screen.dart new file mode 100644 index 0000000..46acd22 --- /dev/null +++ b/lib/screens/home/home_screen.dart @@ -0,0 +1,14 @@ +import 'package:flutter/material.dart'; + +class HomeScreen extends StatelessWidget { + @override + Widget build(BuildContext context) { + return new Scaffold( + appBar: new AppBar(title: new Text("Home"),), + body: new Center( + child: new Text("Welcome home!"), + ), + ); + } + +}
\ No newline at end of file diff --git a/lib/screens/login/login_screen.dart b/lib/screens/login/login_screen.dart new file mode 100644 index 0000000..7c8845e --- /dev/null +++ b/lib/screens/login/login_screen.dart @@ -0,0 +1,141 @@ +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<StatefulWidget> createState() { + return new LoginScreenState(); + } +} + +class LoginScreenState extends State<LoginScreen> + implements LoginScreenContract, AuthStateListener { + BuildContext _ctx; + + bool _isLoading = false; + final formKey = new GlobalKey<FormState>(); + final scaffoldKey = new GlobalKey<ScaffoldState>(); + 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: <Widget>[ + new Text( + "Login", + textScaleFactor: 2.0, + ), + new Form( + key: formKey, + child: new Column( + children: <Widget>[ + 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); + } +} diff --git a/lib/screens/login/login_screen_presenter.dart b/lib/screens/login/login_screen_presenter.dart new file mode 100644 index 0000000..fc5da4f --- /dev/null +++ b/lib/screens/login/login_screen_presenter.dart @@ -0,0 +1,19 @@ +import 'package:beam_messenger/data/rest_ds.dart'; +import 'package:beam_messenger/models/user.dart'; + +abstract class LoginScreenContract { + void onLoginSuccess(User user); + void onLoginError(String errorTxt); +} + +class LoginScreenPresenter { + LoginScreenContract _view; + RestDatasource api = new RestDatasource(); + LoginScreenPresenter(this._view); + + doLogin(String email, String password) { + api.login(email, password).then((User user) { + _view.onLoginSuccess(user); + }).catchError((Exception error) => _view.onLoginError(error.toString())); + } +} |