Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.65 KB | None | 0 0
  1. Login
  2.  
  3. import 'dart:convert';
  4.  
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_login_bloc/src/custom/clipper.dart';
  7. import 'package:http/http.dart' as http;
  8. import 'package:http/http.dart';
  9. import 'package:shared_preferences/shared_preferences.dart';
  10. import '../resources/api/api_provider.dart';
  11. import '../resources/utils/shared_preferences.dart';
  12. import 'home_page.dart';
  13.  
  14. class Login extends StatefulWidget {
  15.  
  16. @override
  17. _LoginState createState() => _LoginState();
  18. }
  19.  
  20. enum LoginStatus{
  21. notSignIn,
  22. signIn
  23. }
  24.  
  25. class _LoginState extends State<Login> {
  26.  
  27. LoginStatus _loginStatus = LoginStatus.notSignIn;
  28. String username, password;
  29. final _key = GlobalKey<FormState>();
  30. bool _secureText = true;
  31.  
  32. Client client = Client();
  33. static final String baseUrl = 'http://192.3.168.178/restapi/';
  34.  
  35. showHide() {
  36. setState(() {
  37. _secureText = !_secureText;
  38. });
  39. }
  40.  
  41. check() {
  42. final form = _key.currentState;
  43. if(form.validate()) {
  44. form.save();
  45. ApiProvider().login(username,password).then((value){
  46. setState(() {
  47. _loginStatus = value;
  48. });
  49. });
  50. }
  51. }
  52.  
  53. // login() async {
  54. // final response = await http.post(baseUrl + "login.php", body: {
  55. // "username" : username,
  56. // "password" : password
  57. // });
  58. //
  59. // final data = json.decode(response.body);
  60. // String status = data['status'];
  61. // String message = data['message'];
  62. // if (status == "success") {
  63. // setState(() {
  64. // SessionManager().savePref(status).then((value){
  65. // setState(() {
  66. // _loginStatus = value;
  67. // });
  68. // });
  69. // });
  70. // print(message);
  71. // } else {
  72. // print(message);
  73. // }
  74. // }
  75.  
  76. // savePref(String status) async {
  77. // SharedPreferences preferences = await SharedPreferences.getInstance();
  78. // setState(() {
  79. // preferences.setString("status", status);
  80. // });
  81. // }
  82.  
  83. // var status;
  84. // getPref() async {
  85. // SharedPreferences preferences = await SharedPreferences.getInstance();
  86. // setState(() {
  87. // status = preferences.getString("status");
  88. // _loginStatus = status == "success" ? LoginStatus.signIn : LoginStatus.notSignIn;
  89. // });
  90. // }
  91.  
  92. // signOut() async {
  93. // SharedPreferences preferences = await SharedPreferences.getInstance();
  94. // setState(() {
  95. // preferences.setString("status", null);
  96. //// preferences.commit();
  97. // _loginStatus = LoginStatus.notSignIn;
  98. // });
  99. // }
  100.  
  101. @override
  102. void initState() {
  103. super.initState();
  104. SessionManager().getPref().then((value){
  105. setState(() {
  106. _loginStatus = value;
  107. });
  108. });
  109. }
  110.  
  111. @override
  112. Widget build(BuildContext context) {
  113. switch (_loginStatus) {
  114. case LoginStatus.notSignIn:
  115. return Scaffold(
  116. backgroundColor: Colors.white,
  117. body: Form(
  118. key: _key,
  119. child: SafeArea(
  120. child: ListView(
  121. physics: ClampingScrollPhysics(),
  122. children: <Widget>[
  123. ClipPath(
  124. clipper: MyClipper(),
  125. child: Container(
  126. decoration: BoxDecoration(
  127. image: DecorationImage(
  128. image: NetworkImage('https://raw.githubusercontent.com/samarthagarwal/FlutterScreens/master/assets/images/full-bloom.png'),
  129. fit: BoxFit.cover,
  130. ),
  131. ),
  132. alignment: Alignment.center,
  133. padding: EdgeInsets.only(top: 150.0, bottom: 100.0),
  134. ),
  135. ),
  136. formCardLogin(),
  137. loginButton(),
  138. ],
  139. ),
  140. ),
  141. ),
  142. );
  143. break;
  144. case LoginStatus.signIn:
  145. return Home();
  146. break;
  147. }
  148. }
  149.  
  150. Widget formCardLogin() {
  151. return Container(
  152. child: Column(
  153. children: <Widget>[
  154. Container(
  155. margin: EdgeInsets.only(top: 20.0, right: 20.0, left: 20.0, bottom: 10.0),
  156. alignment: Alignment.centerLeft,
  157. child: Text(
  158. "Username",
  159. style: TextStyle(color: Colors.grey, fontSize: 16.0),
  160. ),
  161. ),
  162. Container(
  163. margin: EdgeInsets.only(right: 20.0, left: 20.0),
  164. decoration: BoxDecoration(
  165. border: Border.all(
  166. color: Colors.grey.withOpacity(0.5),
  167. width: 1.0,
  168. ),
  169. borderRadius: BorderRadius.circular(20.0),
  170. ),
  171. child: Row(
  172. mainAxisSize: MainAxisSize.max,
  173. children: <Widget>[
  174. Padding(
  175. padding:
  176. EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
  177. child: Icon(
  178. Icons.person_outline,
  179. color: Colors.grey,
  180. ),
  181. ),
  182. Container(
  183. height: 30.0,
  184. width: 1.0,
  185. color: Colors.grey.withOpacity(0.5),
  186. margin: EdgeInsets.only(left: 00.0, right: 10.0),
  187. ),
  188. Flexible(
  189. child: TextFormField(
  190. validator: (e) {
  191. if(e.isEmpty) {
  192. return "Please Insert Username";
  193. }
  194. },
  195. onSaved: (e) => username = e,
  196. decoration: InputDecoration(
  197. border: InputBorder.none,
  198. hintText: 'Enter your email',
  199. hintStyle: TextStyle(color: Colors.grey),
  200. ),
  201. ),
  202. )
  203. ],
  204. ),
  205. ),
  206. Container(
  207. margin: EdgeInsets.only(top: 10.0, right: 20.0, left: 20.0, bottom: 10.0),
  208. alignment: Alignment.centerLeft,
  209. child: Text(
  210. "Password",
  211. style: TextStyle(color: Colors.grey, fontSize: 16.0),
  212. ),
  213. ),
  214. Container(
  215. margin: EdgeInsets.only(right: 20.0, left: 20.0),
  216. decoration: BoxDecoration(
  217. border: Border.all(
  218. color: Colors.grey.withOpacity(0.5),
  219. width: 1.0,
  220. ),
  221. borderRadius: BorderRadius.circular(20.0),
  222. ),
  223. child: Row(
  224. mainAxisSize: MainAxisSize.max,
  225. children: <Widget>[
  226. Padding(
  227. padding:
  228. EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
  229. child: Icon(
  230. Icons.lock_open,
  231. color: Colors.grey,
  232. ),
  233. ),
  234. Container(
  235. height: 30.0,
  236. width: 1.0,
  237. color: Colors.grey.withOpacity(0.5),
  238. margin: EdgeInsets.only(left: 00.0, right: 10.0),
  239. ),
  240. Flexible(
  241. child: TextFormField(
  242. obscureText: _secureText,
  243. onSaved: (e) => password = e,
  244. decoration: InputDecoration(
  245. border: InputBorder.none,
  246. suffixIcon: IconButton(
  247. icon: Icon(_secureText ? Icons.visibility_off : Icons.visibility),
  248. onPressed: showHide
  249. ),
  250. hintText: 'Enter your password',
  251. hintStyle: TextStyle(color: Colors.grey),
  252. ),
  253. ),
  254. )
  255. ],
  256. ),
  257. ),
  258. ],
  259. ),
  260. );
  261. }
  262.  
  263. Widget loginButton() {
  264. return Column(
  265. children: <Widget>[
  266. GestureDetector(
  267. child: Container(
  268. margin: EdgeInsets.only(top: 40.0),
  269. padding: EdgeInsets.only(left: 20.0, right: 20.0),
  270. child: DecoratedBox(
  271. decoration: BoxDecoration(
  272. borderRadius: BorderRadius.all(Radius.circular(30.0)),
  273. color: Colors.blue
  274. ),
  275. child: Row(
  276. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  277. children: <Widget>[
  278. Padding(
  279. padding: EdgeInsets.only(left: 20.0),
  280. child: Text(
  281. "LOGIN",
  282. style: TextStyle(color: Colors.white),
  283. ),
  284. ),
  285. Transform.translate(
  286. offset: Offset(15.0, 0.0),
  287. child: Container(
  288. margin: EdgeInsets.only(right: 20.0),
  289. padding: EdgeInsets.all(10.0),
  290. child: DecoratedBox(
  291. decoration: BoxDecoration(
  292. borderRadius: BorderRadius.all(Radius.circular(28.0)),
  293. color: Colors.white
  294. ),
  295. child: Icon(
  296. Icons.arrow_forward,
  297. color: Colors.blue,
  298. ),
  299. ),
  300. ),
  301. )
  302. ],
  303. ),
  304. ),
  305. ),
  306. onTap: () {
  307. check();
  308. },
  309. ),
  310. ],
  311. );
  312. }
  313.  
  314. }
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322. api_porvider
  323. import 'dart:async';
  324. import 'package:flutter_login_bloc/src/models/test.dart';
  325. import 'package:http/http.dart' show Client;
  326. import 'dart:convert';
  327.  
  328. import '../../ui/login_page.dart';
  329. import '../utils/shared_preferences.dart';
  330.  
  331. class ApiProvider {
  332.  
  333. Client client = Client();
  334. static final String baseUrl = 'http://192.3.168.178/restapi/';
  335.  
  336. Future<ResultTest> loginProvider(var body) async {
  337. final response = await client.post(baseUrl + 'login.php', body: body, headers: {"Accept":"application/json"});
  338. if (response.statusCode == 200) {
  339. return ResultTest.fromJson(json.decode(response.body));
  340. } else {
  341. throw Exception('Failed');
  342. }
  343. }
  344. LoginStatus _loginStatus ;
  345.  
  346. Future<LoginStatus> login(String username, String password) async {
  347. final response = await client.post(baseUrl + "login.php", body: {
  348. "username" : username,
  349. "password" : password
  350. });
  351.  
  352. final data = json.decode(response.body);
  353. String status = data['status'];
  354. String message = data['message'];
  355. if (status == "success") {
  356. SessionManager().savePref(status);
  357. print(message);
  358. _loginStatus = LoginStatus.signIn;
  359. } else {
  360. print(message);
  361. _loginStatus = LoginStatus.notSignIn;
  362. }
  363. return _loginStatus;
  364. }
  365.  
  366. // static final String baseUrl = '';
  367. // static final String body = "{\"kode\": 1,\"pesan\": \"Login Berhasil\",\"result\": [{\"npm\": \"1154089\",\"kd_prodi\": \"14\",\"nm_dosen\": \"SYAFRIAL FACHRI PANE\",\"kd_matkul\": \"T4I811B4\",\"nm_matkul\": \"TUGAS AKHIR\",\"semester\": \"8\",\"status\": \"1\"},{\"npm\": \"1154089\",\"kd_prodi\": \"14\",\"nm_dosen\": \"MOHAMAD HARRY KHOMAS SAPUTRA\",\"kd_matkul\": \"T4I811C4\",\"nm_matkul\": \"INTERNSHIP II\",\"semester\": \"8\",\"status\": \"1\"}]}";
  368. //
  369. // Future<Users> loginProvider(String username, String password) async {
  370. //// final response = await client.get(baseUrl + 'login.php' + username + password);
  371. //// final response = await client.get(baseUrl + 'login.php?npm=$username&password=$password');
  372. // if (1 == 1) {
  373. // return Users.fromJson(json.decode(body));
  374. // } else {
  375. // throw Exception('Failed');
  376. // }
  377. // }
  378.  
  379. }
  380.  
  381.  
  382.  
  383. shared_prefre
  384.  
  385. import 'package:shared_preferences/shared_preferences.dart';
  386.  
  387. import '../../ui/login_page.dart';
  388.  
  389. class SessionManager {
  390. // Future<LoginStatus> savePref(String status) async {
  391. // SharedPreferences preferences = await SharedPreferences.getInstance();
  392. // preferences.setString("status", status);
  393. // return LoginStatus.signIn;
  394. // }
  395. savePref(String status) async {
  396. SharedPreferences preferences = await SharedPreferences.getInstance();
  397. preferences.setString("status", status);
  398. }
  399.  
  400. LoginStatus _loginStatus = LoginStatus.notSignIn;
  401. Future<LoginStatus> getPref() async {
  402. SharedPreferences preferences = await SharedPreferences.getInstance();
  403. if(preferences.getString("status") == "success"){
  404. _loginStatus = LoginStatus.signIn;
  405. }
  406. print(_loginStatus);
  407. return _loginStatus;
  408. }
  409.  
  410. signOut() async {
  411. SharedPreferences preferences = await SharedPreferences.getInstance();
  412. preferences.setString("status", null);
  413. }
  414. }
  415.  
  416.  
  417.  
  418.  
  419. home
  420.  
  421. import 'package:flutter/material.dart';
  422. import 'package:flutter_login_bloc/src/resources/utils/shared_preferences.dart';
  423.  
  424. import 'login_page.dart';
  425.  
  426. class Home extends StatefulWidget {
  427.  
  428. @override
  429. _HomeState createState() => _HomeState();
  430. }
  431.  
  432. class _HomeState extends State<Home> {
  433.  
  434.  
  435. @override
  436. Widget build(BuildContext context) {
  437. return Scaffold(
  438. appBar: AppBar(
  439. actions: <Widget>[
  440. IconButton(
  441. icon: Icon(Icons.lock_open),
  442. onPressed: () {
  443. setState(() {
  444. SessionManager().signOut();
  445. Navigator.pushReplacement(context, MaterialPageRoute(builder:(context) => Login() ));
  446. });
  447. },
  448. ),
  449. ],
  450. ),
  451. body: SafeArea(
  452. child: Center(
  453. child: Text("Home"),
  454. ),
  455. ),
  456. );
  457. }
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement