Advertisement
wildanfuady

Untitled

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