Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- import 'package:ospek_unai/homepanitia.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- class LoginPanitia extends StatefulWidget {
- @override
- _LoginPanitiaPage createState() => _LoginPanitiaPage();
- }
- enum LoginStatus{
- notSignIn,
- signIn
- }
- class _LoginPanitiaPage extends State<LoginPanitia> {
- LoginStatus _loginStatus = LoginStatus.notSignIn;
- String nomorindukpanitia, password;
- final _key = new GlobalKey<FormState>();
- bool _secureText = true;
- showHide() {
- setState(() {
- _secureText = !_secureText;
- });
- }
- check() {
- final form = _key.currentState;
- if (form.validate()) {
- form.save();
- //print("$nomorindukpanitia, $password");
- login();
- }
- }
- login() async {
- final response = await http.post(
- "http://10.0.2.2/aplikasi_ospekunai/loginpanitia.php",
- body: {"nomorinduk_panitia": nomorindukpanitia, "password": password});
- final data = jsonDecode(response.body);
- int value = data['value'];
- String pesan = data['message'];
- if (value==1) {
- setState(() {
- _loginStatus = LoginStatus.signIn;
- savePref(value);
- });
- print(pesan);
- } else {
- print(pesan);
- }
- //print(data);
- }
- savePref(int value)async{
- SharedPreferences preferences = await SharedPreferences.getInstance();
- setState(() {
- preferences.setInt("value", value);
- preferences.commit();
- });
- }
- var value;
- getPref()async{
- SharedPreferences preferences = await SharedPreferences.getInstance();
- setState(() {
- value = preferences.getInt("value");
- _loginStatus = value == 1 ? LoginStatus.signIn : LoginStatus.notSignIn;
- });
- }
- signOut()async{
- SharedPreferences preferences = await SharedPreferences.getInstance();
- setState(() {
- preferences.setInt("value", null);
- preferences.commit();
- _loginStatus = LoginStatus.notSignIn;
- });
- }
- @override
- void initState() {
- super.initState();
- getPref();
- }
- @override
- Widget build(BuildContext context) {
- switch (_loginStatus) {
- case LoginStatus.notSignIn:
- return Scaffold(
- appBar: AppBar(
- title: new Text("Login Panitia"),
- ),
- body: Form(
- key: _key,
- child: ListView(
- padding: EdgeInsets.all(16.0),
- children: <Widget>[
- TextFormField(
- validator: (e) {
- if (e.isEmpty) {
- return "Please insert";
- }
- },
- onSaved: (e) => nomorindukpanitia = e,
- decoration: InputDecoration(labelText: "Nomor Induk"),
- ),
- TextFormField(
- obscureText: _secureText,
- onSaved: (e) => password = e,
- decoration: InputDecoration(
- suffixIcon: IconButton(
- icon: Icon(_secureText
- ? Icons.visibility_off
- : Icons.visibility),
- onPressed: showHide),
- labelText: "Password"),
- ),
- MaterialButton(
- onPressed: () {
- check();
- },
- child: Text("Login"),
- )
- ],
- ),
- ),
- );
- break;
- case LoginStatus.signIn :
- return HomePanitia(signOut);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement