Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.20 KB | None | 0 0
  1. import 'dart:ui';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:megaflix/auth.dart';
  5. import 'package:megaflix/data/database.dart';
  6. import 'package:megaflix/models/user.dart';
  7. import 'package:megaflix/screens/loginPresenter.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, _username;
  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 = Container(width: 300,child: Row(
  60.       crossAxisAlignment: CrossAxisAlignment.end,
  61.       mainAxisAlignment: MainAxisAlignment.end,
  62.       children: <Widget>[
  63.         Container(
  64.  
  65.           margin: EdgeInsets.only(right: 10, top: 25),
  66.           child: OutlineButton(
  67.             onPressed: (){},
  68.             color: Colors.white,
  69.             textColor: Colors.pink,
  70.             child: Text("CANCEL"),),
  71.         ),
  72.         Container(
  73.           child: MaterialButton(
  74.             onPressed: _submit,
  75.             color: Colors.pink,
  76.             textColor: Colors.white,
  77.             child: Text("LOGIN"),),
  78.         )
  79.       ],
  80.     ));
  81.     var loginForm = new Column(
  82.       mainAxisAlignment: MainAxisAlignment.center,
  83.       children: <Widget>[
  84.         new Form(
  85.           key: formKey,
  86.             child: Center(
  87.               child: SingleChildScrollView(
  88.                 child: Column(
  89.                   crossAxisAlignment: CrossAxisAlignment.center,
  90.                   mainAxisAlignment: MainAxisAlignment.center,
  91.                   children: <Widget>[
  92.                     Container(
  93.                       width: 250,
  94.                       height: 250,
  95.                       margin: EdgeInsets.only(bottom: 0, top: 0),
  96.                       child: new Image.asset("images/logo.png"),
  97.                     ),
  98.  
  99.                     Container(
  100.                         width: 300,
  101.                         padding: EdgeInsets.all(4),
  102.                         child: new TextFormField(
  103.                           decoration: InputDecoration(
  104.                               border: OutlineInputBorder(),
  105.                               labelText: 'Enter your username'
  106.                           ),
  107.                           onSaved: (value) => _username = value,
  108.                         )
  109.                     ),
  110.                     Container(
  111.                         width: 300,
  112.                         padding: EdgeInsets.all(4),
  113.                         child: new TextFormField(
  114.                             decoration: InputDecoration(
  115.                                 border: OutlineInputBorder(),
  116.                                 labelText: 'Enter your password'
  117.                             ),
  118.                             onSaved: (value) => _password = value
  119.                         )
  120.                     ),
  121.                     /*Container(
  122.                         width: 300,
  123.                         padding: EdgeInsets.all(4),
  124.                         child: new TextFormField(
  125.                             decoration: InputDecoration(
  126.                                 border: OutlineInputBorder(),
  127.                                 labelText: 'Enter your host'
  128.                             ),
  129.                             onSaved: (value) => _host = value
  130.                         )
  131.                     ),*/
  132.                     /*Container(
  133.                         width: 300,
  134.                         padding: EdgeInsets.all(4),
  135.                         child: Row(
  136.                           crossAxisAlignment: CrossAxisAlignment.end,
  137.                           mainAxisAlignment: MainAxisAlignment.end,
  138.                           children: <Widget>[
  139.                             Container(
  140.  
  141.                               margin: EdgeInsets.only(right: 10, top: 25),
  142.                               child: OutlineButton(
  143.                                 onPressed: (){},
  144.                                 color: Colors.white,
  145.                                 textColor: Colors.pink,
  146.                                 child: Text("CANCEL"),),
  147.                             ),
  148.                             Container(
  149.                               child: MaterialButton(
  150.                                 onPressed: _submit,
  151.                                 color: Colors.pink,
  152.                                 textColor: Colors.white,
  153.                                 child: Text("LOGIN"),),
  154.                             )
  155.                           ],))*/
  156.                   ],
  157.                 ),
  158.               ),
  159.             )
  160.         ),
  161.         _isLoading ? new CircularProgressIndicator() : loginBtn
  162.       ],
  163.       crossAxisAlignment: CrossAxisAlignment.center,
  164.     );
  165.  
  166.     return new Scaffold(
  167.       appBar: null,
  168.       key: scaffoldKey,
  169.       body: new Center(child: loginForm)
  170.     );
  171.   }
  172.  
  173.   @override
  174.   void onLoginError(String errorTxt) {
  175.     _showSnackBar(errorTxt);
  176.     setState(() => _isLoading = false);
  177.   }
  178.  
  179.   @override
  180.   void onLoginSuccess(User user) async {
  181.     _showSnackBar(user.toString());
  182.     setState(() => _isLoading = false);
  183.     var db = new DatabaseHelper();
  184.     await db.saveUser(user);
  185.     var authStateProvider = new AuthStateProvider();
  186.     authStateProvider.notify(AuthState.LOGGED_IN);
  187.   }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement