Guest User

Untitled

a guest
Jan 30th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.06 KB | None | 0 0
  1. import 'dart:ui';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:login_app/auth.dart';
  5. import 'package:login_app/data/database_helper.dart';
  6. import 'package:login_app/models/user.dart';
  7. import 'package:login_app/screens/login/login_screen_presenter.dart';
  8.  
  9. class LoginScreen extends StatefulWidget {
  10.   @override
  11.   State<StatefulWidget> createState() {
  12.     // TODO: implement createState
  13.     return new LoginScreenState();
  14.   }
  15. }
  16.  
  17. class LoginScreenState extends State<LoginScreen>
  18.     implements LoginScreenContract, AuthStateListener {
  19.   BuildContext _ctx;
  20.  
  21.   bool _isLoading = false;
  22.   final formKey = new GlobalKey<FormState>();
  23.   final scaffoldKey = new GlobalKey<ScaffoldState>();
  24.   String _password, _password;
  25.  
  26.   LoginScreenPresenter _presenter;
  27.  
  28.   LoginScreenState() {
  29.     _presenter = new LoginScreenPresenter(this);
  30.     var authStateProvider = new AuthStateProvider();
  31.     authStateProvider.subscribe(this);
  32.   }
  33.  
  34.   void _submit() {
  35.     final form = formKey.currentState;
  36.  
  37.     if (form.validate()) {
  38.       setState(() => _isLoading = true);
  39.       form.save();
  40.       _presenter.doLogin(_username, _password);
  41.     }
  42.   }
  43.  
  44.   void _showSnackBar(String text) {
  45.     scaffoldKey.currentState
  46.         .showSnackBar(new SnackBar(content: new Text(text)));
  47.   }
  48.  
  49.   @override
  50.   onAuthStateChanged(AuthState state) {
  51.    
  52.     if(state == AuthState.LOGGED_IN)
  53.       Navigator.of(_ctx).pushReplacementNamed("/home");
  54.   }
  55.  
  56.   @override
  57.   Widget build(BuildContext context) {
  58.     _ctx = context;
  59.     var loginBtn = new RaisedButton(
  60.       onPressed: _submit,
  61.       child: new Text("LOGIN"),
  62.       color: Colors.primaries[0],
  63.     );
  64.     var loginForm = new Column(
  65.       children: <Widget>[
  66.         new Text(
  67.           "Login App",
  68.           textScaleFactor: 2.0,
  69.         ),
  70.         new Form(
  71.           key: formKey,
  72.           child: new Column(
  73.             children: <Widget>[
  74.               new Padding(
  75.                 padding: const EdgeInsets.all(8.0),
  76.                 child: new TextFormField(
  77.                   onSaved: (val) => _username = val,
  78.                   validator: (val) {
  79.                     return val.length < 10
  80.                         ? "Username must have atleast 10 chars"
  81.                         : null;
  82.                   },
  83.                   decoration: new InputDecoration(labelText: "Username"),
  84.                 ),
  85.               ),
  86.               new Padding(
  87.                 padding: const EdgeInsets.all(8.0),
  88.                 child: new TextFormField(
  89.                   onSaved: (val) => _password = val,
  90.                   decoration: new InputDecoration(labelText: "Password"),
  91.                 ),
  92.               ),
  93.             ],
  94.           ),
  95.         ),
  96.         _isLoading ? new CircularProgressIndicator() : loginBtn
  97.       ],
  98.       crossAxisAlignment: CrossAxisAlignment.center,
  99.     );
  100.  
  101.     return new Scaffold(
  102.       appBar: null,
  103.       key: scaffoldKey,
  104.       body: new Container(
  105.         decoration: new BoxDecoration(
  106.           image: new DecorationImage(
  107.               image: new AssetImage("assets/login_background.jpg"),
  108.               fit: BoxFit.cover),
  109.         ),
  110.         child: new Center(
  111.           child: new ClipRect(
  112.             child: new BackdropFilter(
  113.               filter: new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
  114.               child: new Container(
  115.                 child: loginForm,
  116.                 height: 300.0,
  117.                 width: 300.0,
  118.                 decoration: new BoxDecoration(
  119.                     color: Colors.grey.shade200.withOpacity(0.5)),
  120.               ),
  121.             ),
  122.           ),
  123.         ),
  124.       ),
  125.     );
  126.   }
  127.  
  128.   @override
  129.   void onLoginError(String errorTxt) {
  130.     _showSnackBar(errorTxt);
  131.     setState(() => _isLoading = false);
  132.   }
  133.  
  134.   @override
  135.   void onLoginSuccess(User user) async {
  136.     _showSnackBar(user.toString());
  137.     setState(() => _isLoading = false);
  138.     var db = new DatabaseHelper();
  139.     await db.saveUser(user);
  140.     var authStateProvider = new AuthStateProvider();
  141.     authStateProvider.notify(AuthState.LOGGED_IN);
  142.   }
  143. }
Add Comment
Please, Sign In to add comment