Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. NETWORK.DART
  2. import 'dart:async';
  3. import 'dart:convert';
  4. import 'package:http/http.dart' as http;
  5.  
  6. class NetworkUtil {
  7. static NetworkUtil _instance = new NetworkUtil.internal();
  8. NetworkUtil.internal();
  9. factory NetworkUtil() => _instance;
  10.  
  11. final JsonDecoder _decoder = new JsonDecoder();
  12.  
  13. Future<dynamic> get(String url) {
  14. return http.get(url).then((http.Response response) {
  15. final String res = response.body;
  16. final int statusCode = response.statusCode;
  17. print(res);
  18. if (statusCode < 200 || statusCode > 400 || json == null || res == null) {
  19. throw new Exception("Error");
  20. }
  21. return _decoder.convert(res);
  22. });
  23. }
  24. }
  25.  
  26. DATA/LOGIN.DART
  27.  
  28. import 'dart:async';
  29.  
  30. import 'package:megaflix/utils/network.dart';
  31. import 'package:megaflix/models/user.dart';
  32.  
  33. class Login{
  34. NetworkUtil _netUtil = new NetworkUtil();
  35. static final String BASE_URL = "http://megaflix.live:25461";
  36. static final LOGIN_URL = BASE_URL + "/player_api.php";
  37.  
  38. Future<User> login(String username, String password) {
  39. return _netUtil.get(LOGIN_URL + "?username=" + username + "&password=" + password).then((dynamic res) {
  40. return new User.map(res["user_info"]);
  41. });
  42. }
  43. }
  44.  
  45. SCREENS/LOGIN.DART
  46.  
  47. import 'dart:ui';
  48.  
  49. import 'package:flutter/material.dart';
  50. import 'package:megaflix/auth.dart';
  51. import 'package:megaflix/data/database.dart';
  52. import 'package:megaflix/models/user.dart';
  53. import 'package:megaflix/screens/loginPresenter.dart';
  54.  
  55. class LoginScreen extends StatefulWidget {
  56. @override
  57. State<StatefulWidget> createState() {
  58. // TODO: implement createState
  59. return new LoginScreenState();
  60. }
  61. }
  62.  
  63. class LoginScreenState extends State<LoginScreen>
  64. implements LoginScreenContract, AuthStateListener {
  65. BuildContext _ctx;
  66.  
  67. bool _isLoading = false;
  68. final formKey = new GlobalKey<FormState>();
  69. final scaffoldKey = new GlobalKey<ScaffoldState>();
  70. String _password, _username;
  71.  
  72. LoginScreenPresenter _presenter;
  73.  
  74. LoginScreenState() {
  75. _presenter = new LoginScreenPresenter(this);
  76. var authStateProvider = new AuthStateProvider();
  77. authStateProvider.subscribe(this);
  78. }
  79.  
  80. void _submit() {
  81. final form = formKey.currentState;
  82.  
  83. if (form.validate()) {
  84. setState(() => _isLoading = true);
  85. form.save();
  86. _presenter.doLogin(_username, _password);
  87. }
  88. }
  89.  
  90. void _showSnackBar(String text) {
  91. scaffoldKey.currentState
  92. .showSnackBar(new SnackBar(content: new Text(text)));
  93. }
  94.  
  95. @override
  96. onAuthStateChanged(AuthState state) {
  97. if(state == AuthState.LOGGED_IN)
  98. Navigator.of(_ctx).pushReplacementNamed("/home");
  99. }
  100.  
  101. @override
  102. Widget build(BuildContext context) {
  103. _ctx = context;
  104. var loginBtn = Container(width: 300,child: Row(
  105. crossAxisAlignment: CrossAxisAlignment.end,
  106. mainAxisAlignment: MainAxisAlignment.end,
  107. children: <Widget>[
  108. Container(
  109.  
  110. margin: EdgeInsets.only(right: 10, top: 25, bottom: 10),
  111. child: OutlineButton(
  112. onPressed: (){},
  113. color: Colors.white,
  114. textColor: Colors.pink,
  115. child: Text("CANCEL"),),
  116. ),
  117. Container(
  118. margin: EdgeInsets.only(top:25, bottom: 10),
  119. child: MaterialButton(
  120. onPressed: _submit,
  121. color: Colors.pink,
  122. textColor: Colors.white,
  123. elevation: 8,
  124. child: Text("LOGIN"),),
  125. )
  126. ],
  127. ));
  128.  
  129. var loginForm = new Column(
  130. mainAxisAlignment: MainAxisAlignment.center,
  131. children: <Widget>[
  132. new Form(
  133. key: formKey,
  134. child: Center(
  135. child: Column(
  136. crossAxisAlignment: CrossAxisAlignment.center,
  137. mainAxisAlignment: MainAxisAlignment.center,
  138. children: <Widget>[
  139. Container(
  140. width: 250,
  141. height: 250,
  142. margin: EdgeInsets.only(bottom: 0, top: 0),
  143. child: new Image.asset("images/logo.png"),
  144. ),
  145.  
  146. Container(
  147. width: 300,
  148. padding: EdgeInsets.all(4),
  149. child: new TextFormField(
  150. decoration: InputDecoration(
  151. border: OutlineInputBorder(),
  152. labelText: 'Enter your username'
  153. ),
  154. onSaved: (value) => _username = value,
  155. )
  156. ),
  157. Container(
  158. width: 300,
  159. padding: EdgeInsets.all(4),
  160. child: new TextFormField(
  161. decoration: InputDecoration(
  162. border: OutlineInputBorder(),
  163. labelText: 'Enter your password'
  164. ),
  165. onSaved: (value) => _password = value
  166. )
  167. ),
  168. /*Container(
  169. width: 300,
  170. padding: EdgeInsets.all(4),
  171. child: new TextFormField(
  172. decoration: InputDecoration(
  173. border: OutlineInputBorder(),
  174. labelText: 'Enter your host'
  175. ),
  176. onSaved: (value) => _host = value
  177. )
  178. ),*/
  179. /*Container(
  180. width: 300,
  181. padding: EdgeInsets.all(4),
  182. child: Row(
  183. crossAxisAlignment: CrossAxisAlignment.end,
  184. mainAxisAlignment: MainAxisAlignment.end,
  185. children: <Widget>[
  186. Container(
  187.  
  188. margin: EdgeInsets.only(right: 10, top: 25),
  189. child: OutlineButton(
  190. onPressed: (){},
  191. color: Colors.white,
  192. textColor: Colors.pink,
  193. child: Text("CANCEL"),),
  194. ),
  195. Container(
  196. child: MaterialButton(
  197. onPressed: _submit,
  198. color: Colors.pink,
  199. textColor: Colors.white,
  200. child: Text("LOGIN"),),
  201. )
  202. ],))*/
  203. ],
  204. ),
  205. ),
  206. ),
  207. _isLoading ? new Container(margin: EdgeInsets.only(top: 25, bottom: 10), width: 30, height: 30,child: new CircularProgressIndicator()) : loginBtn
  208. ],
  209. crossAxisAlignment: CrossAxisAlignment.center,
  210. );
  211.  
  212. return new Scaffold(
  213. appBar: null,
  214. key: scaffoldKey,
  215. resizeToAvoidBottomInset: true,
  216. body: new SingleChildScrollView(child: Container(child: loginForm, margin: EdgeInsets.only(top: 90)))
  217. );
  218. }
  219.  
  220. @override
  221. void onLoginError(String errorTxt) {
  222. _showSnackBar(errorTxt);
  223. setState(() => _isLoading = false);
  224. }
  225.  
  226. @override
  227. void onLoginSuccess(User user) async {
  228. setState(() => _isLoading = false);
  229. var db = new DatabaseHelper();
  230. await db.saveUser(user);
  231. var authStateProvider = new AuthStateProvider();
  232. authStateProvider.notify(AuthState.LOGGED_IN);
  233. }
  234. }
  235.  
  236. LOGINPRESENTER
  237. import 'package:megaflix/data/login.dart';
  238. import 'package:megaflix/models/user.dart';
  239.  
  240. abstract class LoginScreenContract {
  241. void onLoginSuccess(User user);
  242. void onLoginError(String errorTxt);
  243. }
  244.  
  245. class LoginScreenPresenter {
  246. LoginScreenContract _view;
  247. Login api = new Login();
  248. LoginScreenPresenter(this._view);
  249. doLogin(String username, String password) {
  250. api.login(username, password).then((User user) {
  251. _view.onLoginSuccess(user);
  252. }).catchError((Exception error) => _view.onLoginError(error.toString()));
  253. }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement