Advertisement
wildanfuady

Untitled

Nov 14th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:sekolahku/project/ListSiswa.dart';
  3. import 'dart:async';
  4. import 'dart:convert';
  5. import 'package:http/http.dart' as http;
  6. import 'package:shared_preferences/shared_preferences.dart';
  7.  
  8. class Response {
  9.  
  10. final String status;
  11. final String message;
  12.  
  13. Response({this.status, this.message});
  14.  
  15. factory Response.fromJson(Map<String, dynamic> json) {
  16. return Response(
  17. status: json['status'],
  18. message: json['message'],
  19. );
  20. }
  21.  
  22. }
  23.  
  24. class Login extends StatefulWidget {
  25.  
  26. @override
  27. LoginState createState() => new LoginState();
  28. }
  29.  
  30. class LoginState extends State<Login> {
  31.  
  32. bool _secureText = true;
  33. showHide() {
  34. setState(() {
  35. _secureText = !_secureText;
  36. });
  37. }
  38.  
  39.  
  40. // variabel member class
  41. final _email = TextEditingController();
  42. final _password = TextEditingController();
  43.  
  44. // variabel response
  45. String _response = "";
  46. bool _apiCall = false;
  47.  
  48. // status login menggunakan shared preference
  49. bool alreadyLogin = false;
  50.  
  51. // function untuk kirim http post
  52. Future<Response> post(String url, var body) async {
  53.  
  54. return await http
  55. .post(Uri.encodeFull(url), body: body, headers: {"Accept" : "application/json"})
  56. .then((http.Response response) {
  57.  
  58. final int statusCode = response.statusCode;
  59.  
  60. if(statusCode < 200 || statusCode > 400 || json == null){
  61. throw new Exception("Error while fetchingdata");
  62. }
  63. return Response.fromJson(json.decode(response.body));
  64. });
  65. }
  66.  
  67. // function untuk kirim email dan password, lalu dicek loginnya
  68. void _toLogin() {
  69. post(
  70. 'http://10.0.2.2/flutter-sekolahku-server/login.php',
  71. {
  72. 'email' : _email.text,
  73. 'password' : _password.text,
  74. }).then((response) async {
  75.  
  76. setState(() {
  77. _apiCall = false;
  78. _response = response.message;
  79. });
  80.  
  81. // cek jika login berhasil
  82. if(response.status == "success") {
  83.  
  84. // beri tanda sudah login
  85. final prefs = await SharedPreferences.getInstance();
  86.  
  87. setState(() {
  88. alreadyLogin = true;
  89. prefs.setBool('login', alreadyLogin);
  90. });
  91.  
  92. // langsung menuju list siswa jika sudah pernah login
  93. Navigator.pushReplacement(context,
  94. MaterialPageRoute(builder: (context) => ListSiswa())
  95. );
  96. }
  97. },
  98.  
  99. // jika response error
  100. onError: (error){
  101. _apiCall = false;
  102. _response = error.toString();
  103. }
  104.  
  105. );
  106. }
  107.  
  108. // Menampilkan progress login
  109.  
  110. Widget progressWidget() {
  111.  
  112. if(_apiCall)
  113.  
  114. // jika masih proses kirim API
  115. return AlertDialog(
  116. content: new Column(
  117. children: <Widget>[
  118. CircularProgressIndicator(),
  119. Text("Please Wait")
  120. ],
  121. ),
  122. );
  123.  
  124. else
  125.  
  126. // jika sudah selesai kirim API
  127. return Center(
  128. child: Text(
  129. _response,
  130. style: TextStyle(fontSize: 15.0),
  131. ),
  132. );
  133. }
  134.  
  135. // ambil status login
  136. Future<bool> getLoginStatus() async{
  137. final prefs = await SharedPreferences.getInstance();
  138. bool loginStatus = prefs.getBool('login') ?? false;
  139. return loginStatus;
  140. }
  141.  
  142. @override
  143. Widget build(BuildContext context) {
  144. return FutureBuilder<bool>(
  145. future: getLoginStatus(),
  146. builder: (context, snapshot) {
  147. return (snapshot.data) ?
  148. // jika sudah login tampilkan list siswa
  149. new ListSiswa() :
  150. // jika belum login tampilkan form login
  151. loginForm();
  152. },
  153. );
  154. }
  155.  
  156. @override
  157. Widget loginForm() {
  158. return Scaffold(
  159. body: Center(
  160. child: ListView(
  161. shrinkWrap: true,
  162. padding: EdgeInsets.only(left: 24.0, right: 24.0),
  163. children: <Widget>[
  164. Padding(
  165. padding: EdgeInsets.only(top: 25.0),
  166. ),
  167. Hero(
  168. tag: 'hero',
  169. child: CircleAvatar(
  170. backgroundColor: Colors.transparent,
  171. radius: 48.0,
  172. child: Image.asset('images/logo2.png'),
  173. ),
  174. ),
  175. SizedBox(height: 24.0),
  176. Center(
  177. child: Column(
  178. children: <Widget>[
  179. Text(
  180. "SEKOLAHKU",
  181. style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.grey),
  182. ),
  183. ],
  184. ),
  185. ),
  186. SizedBox(height: 24.0),
  187. TextFormField(
  188. controller: _email,
  189. keyboardType: TextInputType.text,
  190. autofocus: false,
  191. decoration: InputDecoration(
  192. hintText: 'email',
  193. filled: true,
  194. contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  195. border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
  196. suffixIcon: Icon(Icons.person),
  197. ),
  198. ),
  199. // spasi
  200. SizedBox(height: 8.0),
  201. TextFormField(
  202. autofocus: false,
  203. obscureText: _secureText,
  204. controller: _password,
  205. keyboardType: TextInputType.text,
  206. decoration: InputDecoration(
  207. hintText: 'Password',
  208. filled: true,
  209. contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  210. border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
  211. suffixIcon: IconButton(
  212. onPressed: showHide,
  213. icon: Icon(_secureText
  214. ? Icons.visibility_off
  215. : Icons.visibility),
  216. ),
  217. ),
  218. ),
  219. // spasi
  220. SizedBox(height: 24.0),
  221. // tombol
  222. Padding(
  223. padding: EdgeInsets.symmetric(vertical: 16.0),
  224. child: Material(
  225. borderRadius: BorderRadius.circular(30.0),
  226. shadowColor: Colors.lightBlueAccent.shade100,
  227. elevation: 5.0,
  228. child: MaterialButton(
  229. minWidth: 200.0,
  230. height: 42.0,
  231. onPressed: () {
  232. setState(() {
  233. _apiCall = true;
  234. });
  235. _toLogin();
  236. },
  237. color: Colors.lightBlueAccent,
  238. child: Text('Log In', style: TextStyle(color: Colors.white, fontSize: 20.0)),
  239. ),
  240. ),
  241. ),
  242. progressWidget()
  243. ],
  244. )
  245. )
  246. );
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement