Advertisement
parhorboo

login.dart

Mar 31st, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import 'dart:convert';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:http/http.dart' as http;
  5. import 'package:ospek_unai/homepanitia.dart';
  6. import 'package:shared_preferences/shared_preferences.dart';
  7.  
  8. class LoginPanitia extends StatefulWidget {
  9. @override
  10. _LoginPanitiaPage createState() => _LoginPanitiaPage();
  11. }
  12.  
  13. enum LoginStatus{
  14. notSignIn,
  15. signIn
  16. }
  17.  
  18. class _LoginPanitiaPage extends State<LoginPanitia> {
  19. LoginStatus _loginStatus = LoginStatus.notSignIn;
  20. String nomorindukpanitia, password;
  21. final _key = new GlobalKey<FormState>();
  22.  
  23. bool _secureText = true;
  24.  
  25. showHide() {
  26. setState(() {
  27. _secureText = !_secureText;
  28. });
  29. }
  30.  
  31. check() {
  32. final form = _key.currentState;
  33. if (form.validate()) {
  34. form.save();
  35. //print("$nomorindukpanitia, $password");
  36. login();
  37. }
  38. }
  39.  
  40.  
  41.  
  42. login() async {
  43. final response = await http.post(
  44. "http://10.0.2.2/aplikasi_ospekunai/loginpanitia.php",
  45. body: {"nomorinduk_panitia": nomorindukpanitia, "password": password});
  46. final data = jsonDecode(response.body);
  47. int value = data['value'];
  48. String pesan = data['message'];
  49. if (value==1) {
  50. setState(() {
  51. _loginStatus = LoginStatus.signIn;
  52. savePref(value);
  53. });
  54.  
  55. print(pesan);
  56.  
  57. } else {
  58. print(pesan);
  59. }
  60. //print(data);
  61. }
  62.  
  63. savePref(int value)async{
  64. SharedPreferences preferences = await SharedPreferences.getInstance();
  65. setState(() {
  66. preferences.setInt("value", value);
  67. preferences.commit();
  68. });
  69. }
  70.  
  71.  
  72.  
  73.  
  74. var value;
  75. getPref()async{
  76. SharedPreferences preferences = await SharedPreferences.getInstance();
  77. setState(() {
  78. value = preferences.getInt("value");
  79.  
  80. _loginStatus = value == 1 ? LoginStatus.signIn : LoginStatus.notSignIn;
  81. });
  82. }
  83.  
  84.  
  85. signOut()async{
  86. SharedPreferences preferences = await SharedPreferences.getInstance();
  87. setState(() {
  88. preferences.setInt("value", null);
  89. preferences.commit();
  90. _loginStatus = LoginStatus.notSignIn;
  91. });
  92. }
  93.  
  94.  
  95.  
  96. @override
  97. void initState() {
  98.  
  99. super.initState();
  100. getPref();
  101. }
  102.  
  103.  
  104. @override
  105. Widget build(BuildContext context) {
  106. switch (_loginStatus) {
  107. case LoginStatus.notSignIn:
  108. return Scaffold(
  109. appBar: AppBar(
  110. title: new Text("Login Panitia"),
  111. ),
  112. body: Form(
  113. key: _key,
  114. child: ListView(
  115. padding: EdgeInsets.all(16.0),
  116. children: <Widget>[
  117. TextFormField(
  118. validator: (e) {
  119. if (e.isEmpty) {
  120. return "Please insert";
  121. }
  122. },
  123. onSaved: (e) => nomorindukpanitia = e,
  124. decoration: InputDecoration(labelText: "Nomor Induk"),
  125. ),
  126. TextFormField(
  127. obscureText: _secureText,
  128. onSaved: (e) => password = e,
  129. decoration: InputDecoration(
  130. suffixIcon: IconButton(
  131. icon: Icon(_secureText
  132. ? Icons.visibility_off
  133. : Icons.visibility),
  134. onPressed: showHide),
  135. labelText: "Password"),
  136. ),
  137. MaterialButton(
  138. onPressed: () {
  139. check();
  140. },
  141. child: Text("Login"),
  142. )
  143. ],
  144. ),
  145. ),
  146. );
  147. break;
  148. case LoginStatus.signIn :
  149. return HomePanitia(signOut);
  150. break;
  151. }
  152.  
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement