Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:convert';
- import 'package:day4_flutter2/page/page_home_berita.dart';
- import 'package:day4_flutter2/ui_view/page_home.dart';
- import 'package:day4_flutter2/ui_view/page_register.dart';
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- import 'package:shared_preferences/shared_preferences.dart';
- class PageLogin extends StatefulWidget {
- @override
- _PageLoginState createState() => _PageLoginState();
- }
- enum LoginStatus {notSignIn, signIn}
- class _PageLoginState extends State<PageLogin> {
- LoginStatus _LoginStatus = LoginStatus.notSignIn;
- String email, password;
- final _key = new GlobalKey<FormState>();
- bool _secureText = true;
- showHide() {
- setState(() {
- _secureText = !_secureText;
- });
- }
- check() {
- final form = _key.currentState;
- if (form.validate()) {
- form.save();
- login();
- }
- }
- login() async {
- final response = await http.post(
- "http://192.168.10.232/server_berita/login.php",
- body: {"email": email, "password": password});
- final data = jsonDecode(response.body);
- int value = data['value'];
- String pesan = data['message'];
- String emailAPI = data['email'];
- String namaAPI = data['username'];
- String id = data['id_user'];
- if (value == 1) {
- setState(() {
- _LoginStatus = LoginStatus.signIn;
- savePref(value, emailAPI, namaAPI, id);
- });
- print(pesan);
- } else {
- print(pesan);
- }
- }
- savePref(int value, String email, String nama, String id) async {
- SharedPreferences preferences = await SharedPreferences.getInstance();
- setState(() {
- preferences.setInt("value", value);
- preferences.setString("username", nama);
- preferences.setString("email", email);
- preferences.setString("id_user", id);
- 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() {
- // TODO: implement initState
- getPref();
- }
- @override
- Widget build(BuildContext context) {
- switch (_LoginStatus) {
- case LoginStatus.notSignIn :
- return Scaffold(
- appBar: AppBar(
- title: Text('Login'),
- ),
- body: Form(
- key: _key,
- child: ListView(
- padding: EdgeInsets.all(16),
- children: <Widget>[
- TextFormField(
- validator: (e) {
- if(e.isEmpty){
- return "Please inser Email";
- }
- },
- onSaved: (e) => email =e,
- decoration: InputDecoration(
- labelText: "email",
- ),
- ),
- TextFormField(
- obscureText: _secureText,
- onSaved: (e) => password =e,
- decoration: InputDecoration(
- labelText: "Password",
- suffixIcon: IconButton(
- onPressed: showHide,
- icon: Icon(_secureText
- ? Icons.visibility
- : Icons.visibility_off),
- )
- ),
- ),
- MaterialButton(
- onPressed: (){
- check();
- },
- child: Text("Login"),
- ),
- InkWell(
- onTap: (){
- Navigator.of(context).push(MaterialPageRoute(builder: (context) => RegisterPage()));
- },
- child: Text(
- "Create a new Accunt, in here",textAlign: TextAlign.center,
- ),
- )
- ],
- ),
- ),
- );
- break;
- case LoginStatus.signIn:
- return PageHomeBerita(signOut);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement