Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:ui';
- import 'package:flutter/material.dart';
- import 'package:login_app/auth.dart';
- import 'package:login_app/data/database_helper.dart';
- import 'package:login_app/models/user.dart';
- import 'package:login_app/screens/login/login_screen_presenter.dart';
- class LoginScreen extends StatefulWidget {
- @override
- State<StatefulWidget> createState() {
- // TODO: implement 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, _password;
- 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(_username, _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 App",
- 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) => _username = val,
- validator: (val) {
- return val.length < 10
- ? "Username must have atleast 10 chars"
- : null;
- },
- decoration: new InputDecoration(labelText: "Username"),
- ),
- ),
- 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);
- }
- }
Add Comment
Please, Sign In to add comment