Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Login
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:flutter_login_bloc/src/custom/clipper.dart';
- import 'package:http/http.dart' as http;
- import 'package:http/http.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import '../resources/api/api_provider.dart';
- import '../resources/utils/shared_preferences.dart';
- import 'home_page.dart';
- class Login extends StatefulWidget {
- @override
- _LoginState createState() => _LoginState();
- }
- enum LoginStatus{
- notSignIn,
- signIn
- }
- class _LoginState extends State<Login> {
- LoginStatus _loginStatus = LoginStatus.notSignIn;
- String username, password;
- final _key = GlobalKey<FormState>();
- bool _secureText = true;
- Client client = Client();
- static final String baseUrl = 'http://192.3.168.178/restapi/';
- showHide() {
- setState(() {
- _secureText = !_secureText;
- });
- }
- check() {
- final form = _key.currentState;
- if(form.validate()) {
- form.save();
- ApiProvider().login(username,password).then((value){
- setState(() {
- _loginStatus = value;
- });
- });
- }
- }
- // login() async {
- // final response = await http.post(baseUrl + "login.php", body: {
- // "username" : username,
- // "password" : password
- // });
- //
- // final data = json.decode(response.body);
- // String status = data['status'];
- // String message = data['message'];
- // if (status == "success") {
- // setState(() {
- // SessionManager().savePref(status).then((value){
- // setState(() {
- // _loginStatus = value;
- // });
- // });
- // });
- // print(message);
- // } else {
- // print(message);
- // }
- // }
- // savePref(String status) async {
- // SharedPreferences preferences = await SharedPreferences.getInstance();
- // setState(() {
- // preferences.setString("status", status);
- // });
- // }
- // var status;
- // getPref() async {
- // SharedPreferences preferences = await SharedPreferences.getInstance();
- // setState(() {
- // status = preferences.getString("status");
- // _loginStatus = status == "success" ? LoginStatus.signIn : LoginStatus.notSignIn;
- // });
- // }
- // signOut() async {
- // SharedPreferences preferences = await SharedPreferences.getInstance();
- // setState(() {
- // preferences.setString("status", null);
- //// preferences.commit();
- // _loginStatus = LoginStatus.notSignIn;
- // });
- // }
- @override
- void initState() {
- super.initState();
- SessionManager().getPref().then((value){
- setState(() {
- _loginStatus = value;
- });
- });
- }
- @override
- Widget build(BuildContext context) {
- switch (_loginStatus) {
- case LoginStatus.notSignIn:
- return Scaffold(
- backgroundColor: Colors.white,
- body: Form(
- key: _key,
- child: SafeArea(
- child: ListView(
- physics: ClampingScrollPhysics(),
- children: <Widget>[
- ClipPath(
- clipper: MyClipper(),
- child: Container(
- decoration: BoxDecoration(
- image: DecorationImage(
- image: NetworkImage('https://raw.githubusercontent.com/samarthagarwal/FlutterScreens/master/assets/images/full-bloom.png'),
- fit: BoxFit.cover,
- ),
- ),
- alignment: Alignment.center,
- padding: EdgeInsets.only(top: 150.0, bottom: 100.0),
- ),
- ),
- formCardLogin(),
- loginButton(),
- ],
- ),
- ),
- ),
- );
- break;
- case LoginStatus.signIn:
- return Home();
- break;
- }
- }
- Widget formCardLogin() {
- return Container(
- child: Column(
- children: <Widget>[
- Container(
- margin: EdgeInsets.only(top: 20.0, right: 20.0, left: 20.0, bottom: 10.0),
- alignment: Alignment.centerLeft,
- child: Text(
- "Username",
- style: TextStyle(color: Colors.grey, fontSize: 16.0),
- ),
- ),
- Container(
- margin: EdgeInsets.only(right: 20.0, left: 20.0),
- decoration: BoxDecoration(
- border: Border.all(
- color: Colors.grey.withOpacity(0.5),
- width: 1.0,
- ),
- borderRadius: BorderRadius.circular(20.0),
- ),
- child: Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- Padding(
- padding:
- EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
- child: Icon(
- Icons.person_outline,
- color: Colors.grey,
- ),
- ),
- Container(
- height: 30.0,
- width: 1.0,
- color: Colors.grey.withOpacity(0.5),
- margin: EdgeInsets.only(left: 00.0, right: 10.0),
- ),
- Flexible(
- child: TextFormField(
- validator: (e) {
- if(e.isEmpty) {
- return "Please Insert Username";
- }
- },
- onSaved: (e) => username = e,
- decoration: InputDecoration(
- border: InputBorder.none,
- hintText: 'Enter your email',
- hintStyle: TextStyle(color: Colors.grey),
- ),
- ),
- )
- ],
- ),
- ),
- Container(
- margin: EdgeInsets.only(top: 10.0, right: 20.0, left: 20.0, bottom: 10.0),
- alignment: Alignment.centerLeft,
- child: Text(
- "Password",
- style: TextStyle(color: Colors.grey, fontSize: 16.0),
- ),
- ),
- Container(
- margin: EdgeInsets.only(right: 20.0, left: 20.0),
- decoration: BoxDecoration(
- border: Border.all(
- color: Colors.grey.withOpacity(0.5),
- width: 1.0,
- ),
- borderRadius: BorderRadius.circular(20.0),
- ),
- child: Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- Padding(
- padding:
- EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
- child: Icon(
- Icons.lock_open,
- color: Colors.grey,
- ),
- ),
- Container(
- height: 30.0,
- width: 1.0,
- color: Colors.grey.withOpacity(0.5),
- margin: EdgeInsets.only(left: 00.0, right: 10.0),
- ),
- Flexible(
- child: TextFormField(
- obscureText: _secureText,
- onSaved: (e) => password = e,
- decoration: InputDecoration(
- border: InputBorder.none,
- suffixIcon: IconButton(
- icon: Icon(_secureText ? Icons.visibility_off : Icons.visibility),
- onPressed: showHide
- ),
- hintText: 'Enter your password',
- hintStyle: TextStyle(color: Colors.grey),
- ),
- ),
- )
- ],
- ),
- ),
- ],
- ),
- );
- }
- Widget loginButton() {
- return Column(
- children: <Widget>[
- GestureDetector(
- child: Container(
- margin: EdgeInsets.only(top: 40.0),
- padding: EdgeInsets.only(left: 20.0, right: 20.0),
- child: DecoratedBox(
- decoration: BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(30.0)),
- color: Colors.blue
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- Padding(
- padding: EdgeInsets.only(left: 20.0),
- child: Text(
- "LOGIN",
- style: TextStyle(color: Colors.white),
- ),
- ),
- Transform.translate(
- offset: Offset(15.0, 0.0),
- child: Container(
- margin: EdgeInsets.only(right: 20.0),
- padding: EdgeInsets.all(10.0),
- child: DecoratedBox(
- decoration: BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(28.0)),
- color: Colors.white
- ),
- child: Icon(
- Icons.arrow_forward,
- color: Colors.blue,
- ),
- ),
- ),
- )
- ],
- ),
- ),
- ),
- onTap: () {
- check();
- },
- ),
- ],
- );
- }
- }
- api_porvider
- import 'dart:async';
- import 'package:flutter_login_bloc/src/models/test.dart';
- import 'package:http/http.dart' show Client;
- import 'dart:convert';
- import '../../ui/login_page.dart';
- import '../utils/shared_preferences.dart';
- class ApiProvider {
- Client client = Client();
- static final String baseUrl = 'http://192.3.168.178/restapi/';
- Future<ResultTest> loginProvider(var body) async {
- final response = await client.post(baseUrl + 'login.php', body: body, headers: {"Accept":"application/json"});
- if (response.statusCode == 200) {
- return ResultTest.fromJson(json.decode(response.body));
- } else {
- throw Exception('Failed');
- }
- }
- LoginStatus _loginStatus ;
- Future<LoginStatus> login(String username, String password) async {
- final response = await client.post(baseUrl + "login.php", body: {
- "username" : username,
- "password" : password
- });
- final data = json.decode(response.body);
- String status = data['status'];
- String message = data['message'];
- if (status == "success") {
- SessionManager().savePref(status);
- print(message);
- _loginStatus = LoginStatus.signIn;
- } else {
- print(message);
- _loginStatus = LoginStatus.notSignIn;
- }
- return _loginStatus;
- }
- // static final String baseUrl = '';
- // static final String body = "{\"kode\": 1,\"pesan\": \"Login Berhasil\",\"result\": [{\"npm\": \"1154089\",\"kd_prodi\": \"14\",\"nm_dosen\": \"SYAFRIAL FACHRI PANE\",\"kd_matkul\": \"T4I811B4\",\"nm_matkul\": \"TUGAS AKHIR\",\"semester\": \"8\",\"status\": \"1\"},{\"npm\": \"1154089\",\"kd_prodi\": \"14\",\"nm_dosen\": \"MOHAMAD HARRY KHOMAS SAPUTRA\",\"kd_matkul\": \"T4I811C4\",\"nm_matkul\": \"INTERNSHIP II\",\"semester\": \"8\",\"status\": \"1\"}]}";
- //
- // Future<Users> loginProvider(String username, String password) async {
- //// final response = await client.get(baseUrl + 'login.php' + username + password);
- //// final response = await client.get(baseUrl + 'login.php?npm=$username&password=$password');
- // if (1 == 1) {
- // return Users.fromJson(json.decode(body));
- // } else {
- // throw Exception('Failed');
- // }
- // }
- }
- shared_prefre
- import 'package:shared_preferences/shared_preferences.dart';
- import '../../ui/login_page.dart';
- class SessionManager {
- // Future<LoginStatus> savePref(String status) async {
- // SharedPreferences preferences = await SharedPreferences.getInstance();
- // preferences.setString("status", status);
- // return LoginStatus.signIn;
- // }
- savePref(String status) async {
- SharedPreferences preferences = await SharedPreferences.getInstance();
- preferences.setString("status", status);
- }
- LoginStatus _loginStatus = LoginStatus.notSignIn;
- Future<LoginStatus> getPref() async {
- SharedPreferences preferences = await SharedPreferences.getInstance();
- if(preferences.getString("status") == "success"){
- _loginStatus = LoginStatus.signIn;
- }
- print(_loginStatus);
- return _loginStatus;
- }
- signOut() async {
- SharedPreferences preferences = await SharedPreferences.getInstance();
- preferences.setString("status", null);
- }
- }
- home
- import 'package:flutter/material.dart';
- import 'package:flutter_login_bloc/src/resources/utils/shared_preferences.dart';
- import 'login_page.dart';
- class Home extends StatefulWidget {
- @override
- _HomeState createState() => _HomeState();
- }
- class _HomeState extends State<Home> {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- actions: <Widget>[
- IconButton(
- icon: Icon(Icons.lock_open),
- onPressed: () {
- setState(() {
- SessionManager().signOut();
- Navigator.pushReplacement(context, MaterialPageRoute(builder:(context) => Login() ));
- });
- },
- ),
- ],
- ),
- body: SafeArea(
- child: Center(
- child: Text("Home"),
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement