Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NETWORK.DART
- import 'dart:async';
- import 'dart:convert';
- import 'package:http/http.dart' as http;
- class NetworkUtil {
- static NetworkUtil _instance = new NetworkUtil.internal();
- NetworkUtil.internal();
- factory NetworkUtil() => _instance;
- final JsonDecoder _decoder = new JsonDecoder();
- Future<dynamic> get(String url) {
- return http.get(url).then((http.Response response) {
- final String res = response.body;
- final int statusCode = response.statusCode;
- print(res);
- if (statusCode < 200 || statusCode > 400 || json == null || res == null) {
- throw new Exception("Error");
- }
- return _decoder.convert(res);
- });
- }
- }
- DATA/LOGIN.DART
- import 'dart:async';
- import 'package:megaflix/utils/network.dart';
- import 'package:megaflix/models/user.dart';
- class Login{
- NetworkUtil _netUtil = new NetworkUtil();
- static final String BASE_URL = "http://megaflix.live:25461";
- static final LOGIN_URL = BASE_URL + "/player_api.php";
- Future<User> login(String username, String password) {
- return _netUtil.get(LOGIN_URL + "?username=" + username + "&password=" + password).then((dynamic res) {
- return new User.map(res["user_info"]);
- });
- }
- }
- SCREENS/LOGIN.DART
- import 'dart:ui';
- import 'package:flutter/material.dart';
- import 'package:megaflix/auth.dart';
- import 'package:megaflix/data/database.dart';
- import 'package:megaflix/models/user.dart';
- import 'package:megaflix/screens/loginPresenter.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, _username;
- 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 = Container(width: 300,child: Row(
- crossAxisAlignment: CrossAxisAlignment.end,
- mainAxisAlignment: MainAxisAlignment.end,
- children: <Widget>[
- Container(
- margin: EdgeInsets.only(right: 10, top: 25, bottom: 10),
- child: OutlineButton(
- onPressed: (){},
- color: Colors.white,
- textColor: Colors.pink,
- child: Text("CANCEL"),),
- ),
- Container(
- margin: EdgeInsets.only(top:25, bottom: 10),
- child: MaterialButton(
- onPressed: _submit,
- color: Colors.pink,
- textColor: Colors.white,
- elevation: 8,
- child: Text("LOGIN"),),
- )
- ],
- ));
- var loginForm = new Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- new Form(
- key: formKey,
- child: Center(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Container(
- width: 250,
- height: 250,
- margin: EdgeInsets.only(bottom: 0, top: 0),
- child: new Image.asset("images/logo.png"),
- ),
- Container(
- width: 300,
- padding: EdgeInsets.all(4),
- child: new TextFormField(
- decoration: InputDecoration(
- border: OutlineInputBorder(),
- labelText: 'Enter your username'
- ),
- onSaved: (value) => _username = value,
- )
- ),
- Container(
- width: 300,
- padding: EdgeInsets.all(4),
- child: new TextFormField(
- decoration: InputDecoration(
- border: OutlineInputBorder(),
- labelText: 'Enter your password'
- ),
- onSaved: (value) => _password = value
- )
- ),
- /*Container(
- width: 300,
- padding: EdgeInsets.all(4),
- child: new TextFormField(
- decoration: InputDecoration(
- border: OutlineInputBorder(),
- labelText: 'Enter your host'
- ),
- onSaved: (value) => _host = value
- )
- ),*/
- /*Container(
- width: 300,
- padding: EdgeInsets.all(4),
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.end,
- mainAxisAlignment: MainAxisAlignment.end,
- children: <Widget>[
- Container(
- margin: EdgeInsets.only(right: 10, top: 25),
- child: OutlineButton(
- onPressed: (){},
- color: Colors.white,
- textColor: Colors.pink,
- child: Text("CANCEL"),),
- ),
- Container(
- child: MaterialButton(
- onPressed: _submit,
- color: Colors.pink,
- textColor: Colors.white,
- child: Text("LOGIN"),),
- )
- ],))*/
- ],
- ),
- ),
- ),
- _isLoading ? new Container(margin: EdgeInsets.only(top: 25, bottom: 10), width: 30, height: 30,child: new CircularProgressIndicator()) : loginBtn
- ],
- crossAxisAlignment: CrossAxisAlignment.center,
- );
- return new Scaffold(
- appBar: null,
- key: scaffoldKey,
- resizeToAvoidBottomInset: true,
- body: new SingleChildScrollView(child: Container(child: loginForm, margin: EdgeInsets.only(top: 90)))
- );
- }
- @override
- void onLoginError(String errorTxt) {
- _showSnackBar(errorTxt);
- setState(() => _isLoading = false);
- }
- @override
- void onLoginSuccess(User user) async {
- setState(() => _isLoading = false);
- var db = new DatabaseHelper();
- await db.saveUser(user);
- var authStateProvider = new AuthStateProvider();
- authStateProvider.notify(AuthState.LOGGED_IN);
- }
- }
- LOGINPRESENTER
- import 'package:megaflix/data/login.dart';
- import 'package:megaflix/models/user.dart';
- abstract class LoginScreenContract {
- void onLoginSuccess(User user);
- void onLoginError(String errorTxt);
- }
- class LoginScreenPresenter {
- LoginScreenContract _view;
- Login api = new Login();
- LoginScreenPresenter(this._view);
- doLogin(String username, String password) {
- api.login(username, password).then((User user) {
- _view.onLoginSuccess(user);
- }).catchError((Exception error) => _view.onLoginError(error.toString()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement