Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:convert';
- import 'package:http/http.dart' as http;
- import 'package:flutter/material.dart';
- import 'package:sekolahku/project/listSiswa.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- // class response json
- class Response {
- final String status;
- final String message;
- Response({this.status, this.message});
- factory Response.fromJson(Map<String, dynamic> json) {
- return Response(
- status: json['status'],
- message: json['message'],
- );
- }
- }
- class Login extends StatefulWidget {
- @override
- LoginState createState() => new LoginState();
- }
- class LoginState extends State<Login> {
- bool _secureText = true;
- showHide() {
- setState(() {
- _secureText = !_secureText;
- });
- }
- // variabel member class
- final _username = TextEditingController();
- final _password = TextEditingController();
- // member response
- String _response = '';
- bool _apiCall = false;
- // login shared prefs
- bool alreadyLogin = false;
- // fungsi untuk kirim http post
- Future<Response> post(String url,var body)async{
- return await http
- .post(Uri.encodeFull(url), body: body, headers: {"Accept":"application/json"})
- .then((http.Response response) {
- final int statusCode = response.statusCode;
- if (statusCode < 200 || statusCode > 400 || json == null) {
- throw new Exception("Error while fetching data");
- }
- return Response.fromJson(json.decode(response.body));
- });
- }
- // fungsi panggil API
- void _callPostAPI() {
- post(
- 'https://flutterapp.ilmucoding.com/login.php',
- {
- 'username': _username.text,
- 'password': _password.text
- }).then((response) async {
- // jika respon normal
- setState(() {
- _apiCall = false;
- _response = response.message;
- });
- if (response.status == "success") {
- // simpan shared prefs sudah login
- final prefs = await SharedPreferences.getInstance();
- setState(() {
- alreadyLogin = true;
- prefs.setBool('login', alreadyLogin);
- });
- // menuju route list product
- Navigator.push(
- context,
- MaterialPageRoute(builder: (context) => ListSiswa())
- );
- }
- },
- // jika respon error
- onError: (error) {
- _apiCall = false;
- _response = error.toString();
- }
- );
- }
- Widget progressWidget() {
- if (_apiCall)
- // jika masih proses kirim API
- return AlertDialog(
- content: new Column(
- children: <Widget>[
- CircularProgressIndicator(),
- Text("Please wait")
- ],
- ),
- );
- else
- // jika sudah selesai kirim API
- return Center(
- child: Text(
- _response,
- style: new TextStyle(fontSize: 15.0),
- ),
- );
- }
- Future<bool> getLoginStatus() async{
- final prefs = await SharedPreferences.getInstance();
- bool loginStatus = prefs.getBool('login') ?? false;
- print('login status $loginStatus');
- return loginStatus;
- }
- @override
- Widget build(BuildContext context) {
- return FutureBuilder<bool>(
- future: getLoginStatus(),
- builder: (context, snapshot) {
- return (snapshot.data) ?
- // jika sudah login tampilkan list product
- new ListSiswa() :
- // jika belum login tampilkan form login
- loginForm();
- },
- );
- }
- Widget loginForm() {
- return Scaffold(
- body: Center(
- child: ListView(
- shrinkWrap: true,
- padding: EdgeInsets.only(left: 24.0, right: 24.0),
- children: <Widget>[
- Padding(
- padding: EdgeInsets.only(top: 25.0),
- ),
- Hero(
- tag: 'hero',
- child: CircleAvatar(
- backgroundColor: Colors.transparent,
- radius: 48.0,
- child: Image.asset('images/logo.png'),
- ),
- ),
- SizedBox(height: 24.0),
- Center(
- child: Column(
- children: <Widget>[
- Text(
- "SEKOLAHKU",
- style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.grey),
- ),
- ],
- ),
- ),
- SizedBox(height: 24.0),
- TextFormField(
- controller: _username,
- keyboardType: TextInputType.text,
- autofocus: false,
- decoration: InputDecoration(
- hintText: 'username',
- contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
- border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
- ),
- ),
- // spasi
- SizedBox(height: 8.0),
- TextFormField(
- autofocus: false,
- obscureText: _secureText,
- controller: _password,
- keyboardType: TextInputType.text,
- decoration: InputDecoration(
- hintText: 'Password',
- filled: true,
- contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
- border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
- suffixIcon: IconButton(
- onPressed: showHide,
- icon: Icon(_secureText
- ? Icons.visibility_off
- : Icons.visibility),
- ),
- ),
- ),
- // spasi
- SizedBox(height: 24.0),
- // tombol
- Padding(
- padding: EdgeInsets.symmetric(vertical: 16.0),
- child: Material(
- borderRadius: BorderRadius.circular(30.0),
- shadowColor: Colors.lightBlueAccent.shade100,
- elevation: 5.0,
- child: MaterialButton(
- minWidth: 200.0,
- height: 42.0,
- onPressed: () {
- setState(() {
- _apiCall = true;
- });
- _callPostAPI();
- },
- color: Colors.lightBlueAccent,
- child: Text('Log In', style: TextStyle(color: Colors.white, fontSize: 20.0)),
- ),
- ),
- ),
- progressWidget()
- ],
- )
- )
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement