first commit
This commit is contained in:
222
lib/page/login.dart
Normal file
222
lib/page/login.dart
Normal file
@ -0,0 +1,222 @@
|
||||
import 'dart:async';
|
||||
// import 'dart:convert';
|
||||
import 'package:barcode_absensi/page/scanQRcode.dart';
|
||||
import 'package:barcode_absensi/states/statePetugas.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
@override
|
||||
_LoginPageState createState() => _LoginPageState();
|
||||
}
|
||||
|
||||
class _LoginPageState extends State<LoginPage> {
|
||||
bool _isLoading = false;
|
||||
// bool _login = false;
|
||||
late SharedPreferences sharedPreferences;
|
||||
|
||||
// @override
|
||||
// void initState() {
|
||||
// // ignore: todo
|
||||
// ignore: todo
|
||||
// // TODO: implement initState
|
||||
// super.initState();
|
||||
// checkLoginStatus();
|
||||
// }
|
||||
|
||||
// ignore: always_declare_return_types
|
||||
// ignore: avoid_void_async
|
||||
// void checkLoginStatus() async {
|
||||
// sharedPreferences = await SharedPreferences.getInstance();
|
||||
// final json = sharedPreferences.getString("dataPetugas");
|
||||
// final data1 = json != null ? jsonDecode(json) : null;
|
||||
// // print(data1);
|
||||
// if (sharedPreferences.getString("dataPetugas") != null) {
|
||||
// if (data1?['level'] == 'petugas') {
|
||||
// final StatePetugas _petugas =
|
||||
// Provider.of<StatePetugas>(context, listen: false);
|
||||
|
||||
// try {
|
||||
// final String _returnString = await _petugas.loginPetugas(
|
||||
// data1!['username'].toString(), data1!['password'].toString());
|
||||
|
||||
// // print(_returnString);
|
||||
|
||||
// if (_returnString == 'success') {
|
||||
// Navigator.pushAndRemoveUntil(
|
||||
// context,
|
||||
// MaterialPageRoute(
|
||||
// builder: (context) => ScanQRCode(),
|
||||
// ),
|
||||
// (route) => false);
|
||||
// } else {
|
||||
// sharedPreferences.remove('dataPetugas');
|
||||
// ScaffoldMessenger.of(context).showSnackBar(
|
||||
// const SnackBar(content: Text('Sila Login Kembali')));
|
||||
// setState(() {
|
||||
// // _login = false;
|
||||
// _isLoading = false;
|
||||
// });
|
||||
// }
|
||||
// } catch (e) {
|
||||
// // print(e);
|
||||
// ScaffoldMessenger.of(context).showSnackBar(
|
||||
// const SnackBar(content: Text('Sila Login Kembali')));
|
||||
// setState(() {
|
||||
// _isLoading = false;
|
||||
// // _login = false;
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
final _usernameController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
|
||||
final GlobalKey<FormState> formkey = GlobalKey<FormState>();
|
||||
|
||||
// ignore: avoid_void_async
|
||||
void _loginPetugas(
|
||||
{required String username,
|
||||
required String password,
|
||||
required BuildContext context}) async {
|
||||
final StatePetugas _petugas =
|
||||
Provider.of<StatePetugas>(context, listen: false);
|
||||
|
||||
try {
|
||||
final String _returnString =
|
||||
await _petugas.loginPetugas(username, password);
|
||||
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
|
||||
if (_returnString == 'success') {
|
||||
// print(_returnString);
|
||||
// sharedPreferences = await SharedPreferences.getInstance();
|
||||
// // print(sharedPreferences);
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
Future.delayed(const Duration(seconds: 2), () {
|
||||
Navigator.of(context).pop(true);
|
||||
Navigator.pushAndRemoveUntil(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => ScanQRCode(),
|
||||
),
|
||||
(route) => false);
|
||||
});
|
||||
return const AlertDialog(
|
||||
title: Text("Sukses"),
|
||||
content: Text("Selamat Betugas"),
|
||||
);
|
||||
},
|
||||
);
|
||||
} else {
|
||||
ScaffoldMessenger.of(context)
|
||||
.showSnackBar(SnackBar(content: Text(_returnString.toString())));
|
||||
}
|
||||
} catch (e) {
|
||||
// print(e);
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text("Jaringan Bermasalahsadsad")));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Halaman Login Petugas"),
|
||||
),
|
||||
body: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: SingleChildScrollView(
|
||||
child: Form(
|
||||
key: formkey,
|
||||
child: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
// focusNode: usernameFocus,
|
||||
controller: _usernameController,
|
||||
keyboardType: TextInputType.text,
|
||||
decoration: const InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
hintText: "Masukkan Username ",
|
||||
labelText: "Username",
|
||||
),
|
||||
validator: (value) {
|
||||
if (value!.isEmpty) {
|
||||
return "Username Harus Terisi";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(
|
||||
height: 20,
|
||||
),
|
||||
TextFormField(
|
||||
// focusNode: passwordFocus,
|
||||
controller: _passwordController,
|
||||
keyboardType: TextInputType.text,
|
||||
obscureText: true,
|
||||
decoration: const InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
hintText: "Masukkan Password ",
|
||||
labelText: "Password",
|
||||
),
|
||||
validator: (value) {
|
||||
if (value!.isEmpty) {
|
||||
return "Password Harus Terisi";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(
|
||||
height: 20,
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
if (formkey.currentState!.validate()) {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
_loginPetugas(
|
||||
username: _usernameController.text,
|
||||
password: _passwordController.text,
|
||||
context: context,
|
||||
);
|
||||
}
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
primary: Colors.blue,
|
||||
onPrimary: Colors.white,
|
||||
),
|
||||
child: const Text("Login"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
472
lib/page/pencarian.dart
Normal file
472
lib/page/pencarian.dart
Normal file
@ -0,0 +1,472 @@
|
||||
import 'dart:convert';
|
||||
import 'package:barcode_absensi/models/modelKaryawan.dart';
|
||||
import 'package:barcode_absensi/page/login.dart';
|
||||
import 'package:barcode_absensi/page/scanQRcode.dart';
|
||||
import 'package:barcode_absensi/states/statePetugas.dart';
|
||||
import 'package:barcode_absensi/widgets/myWidgets.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_slidable/flutter_slidable.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
// ignore_for_file: all
|
||||
|
||||
class Pencarian extends StatefulWidget {
|
||||
@override
|
||||
_PencarianState createState() => _PencarianState();
|
||||
}
|
||||
|
||||
class _PencarianState extends State<Pencarian> {
|
||||
final int _selectedIndex = 1;
|
||||
late SharedPreferences sharedPreferences;
|
||||
|
||||
// ignore: non_constant_identifier_names
|
||||
int _cek_list = 1;
|
||||
|
||||
// var data;
|
||||
|
||||
final List<Note> _notes = <Note>[];
|
||||
List<Note> _notesForDisplay = <Note>[];
|
||||
|
||||
Future<List<Note>> fetchNotes() async {
|
||||
const url =
|
||||
'http://192.168.43.125/barcode_absensi_admin/api_server/list_karyawan';
|
||||
|
||||
try {
|
||||
final response = await http.get(Uri.parse(url));
|
||||
final notes = <Note>[];
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final notesJson = json.decode(response.body);
|
||||
for (final noteJson in notesJson) {
|
||||
notes.add(Note.fromJson(noteJson));
|
||||
}
|
||||
setState(() {
|
||||
_cek_list = 2;
|
||||
});
|
||||
return notes;
|
||||
} else {
|
||||
setState(() {
|
||||
_cek_list = 0;
|
||||
});
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
||||
content: Text('Jaringan Bermaslah, Sila Coba Kembali')));
|
||||
throw Exception("Error on server");
|
||||
}
|
||||
} on Exception catch (_) {
|
||||
// print('sini tidak dapat data');
|
||||
setState(() {
|
||||
_cek_list = 0;
|
||||
});
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
||||
content: Text('Jaringan Bermasalah, Sila Coba Kembali')));
|
||||
throw Exception("Error on server");
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
fetchNotes().then((value) {
|
||||
setState(() {
|
||||
_notes.addAll(value);
|
||||
_notesForDisplay = _notes;
|
||||
});
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void _onItemTapped(int index) {
|
||||
if (index == 0) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => ScanQRCode(),
|
||||
));
|
||||
} else if (index == 1) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => Pencarian(),
|
||||
));
|
||||
} else {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text("Logout ?"),
|
||||
content: const Text("Anda Akan Logout Dari Sistem?"),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Batalkan')),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
sharedPreferences =
|
||||
await SharedPreferences.getInstance();
|
||||
sharedPreferences.remove('dataPetugas');
|
||||
Navigator.pushAndRemoveUntil(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => LoginPage(),
|
||||
),
|
||||
(route) => false,
|
||||
);
|
||||
},
|
||||
child: const Text('Ya')),
|
||||
],
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Cari Karyawan"),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () => Navigator.pop(context, true)),
|
||||
],
|
||||
),
|
||||
body: (_cek_list == 1)
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: ((_cek_list == 2)
|
||||
? ListView.builder(
|
||||
itemBuilder: (context, index) {
|
||||
return index == 0
|
||||
? _searchBar()
|
||||
: _listItem(index, context);
|
||||
},
|
||||
itemCount: _notesForDisplay.length + 1,
|
||||
)
|
||||
: const Center(
|
||||
child: Text("Masalah Jaringan, Sila Coba Kembali"))),
|
||||
drawer: MyDrawer(),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
items: const <BottomNavigationBarItem>[
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.camera),
|
||||
label: 'Scan QRCode',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.search),
|
||||
label: 'Pencarian',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.logout),
|
||||
label: 'Logout',
|
||||
),
|
||||
],
|
||||
currentIndex: _selectedIndex,
|
||||
selectedItemColor: Colors.amber[800],
|
||||
onTap: _onItemTapped,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Padding _searchBar() {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
|
||||
child: TextField(
|
||||
decoration: const InputDecoration(hintText: 'Search...'),
|
||||
onChanged: (text) {
|
||||
text = text.toLowerCase();
|
||||
setState(() {
|
||||
_notesForDisplay = _notes.where((note) {
|
||||
final noteTitle = note.nik_karyawan.toLowerCase();
|
||||
return noteTitle.contains(text);
|
||||
}).toList();
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Card _listItem(index, context) {
|
||||
return Card(
|
||||
child: Slidable(
|
||||
actionPane: const SlidableDrawerActionPane(),
|
||||
secondaryActions: [
|
||||
IconSlideAction(
|
||||
caption: 'Absensi',
|
||||
color: Colors.blue,
|
||||
icon: Icons.archive,
|
||||
onTap: () => _cek_absensi(
|
||||
_notesForDisplay[index - 1].nik_karyawan.toString(), context),
|
||||
),
|
||||
],
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
_notesForDisplay[index - 1].nik_karyawan.toString(),
|
||||
style:
|
||||
const TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text(
|
||||
_notesForDisplay[index - 1].nama.toString(),
|
||||
style: TextStyle(color: Colors.grey.shade600),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ignore: non_constant_identifier_names
|
||||
Future<void> _cek_absensi(String nik_karyawan, BuildContext context) async {
|
||||
final StatePetugas _petugas =
|
||||
Provider.of<StatePetugas>(context, listen: false);
|
||||
|
||||
// print(_petugas);
|
||||
try {
|
||||
final String _returnString = await _petugas.scanPetugas(nik_karyawan);
|
||||
sharedPreferences = await SharedPreferences.getInstance();
|
||||
// print(_returnString);
|
||||
if (_returnString == 'error') {
|
||||
alertDialognya('Jaringan Bermasalah111, Sila Coba Kembali');
|
||||
} else if (_returnString == 'suda_absen') {
|
||||
final json = jsonDecode(sharedPreferences.getString("errornya")!);
|
||||
|
||||
alertDialognya(json['message'].toString());
|
||||
sharedPreferences.remove('errornya');
|
||||
} else if (_returnString == 'data_tiada') {
|
||||
final json = jsonDecode(sharedPreferences.getString("errornya")!);
|
||||
|
||||
alertDialognya(json['message'].toString());
|
||||
sharedPreferences.remove('errornya');
|
||||
} else {
|
||||
final data = jsonDecode(_returnString);
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.info,
|
||||
color: Colors.blue,
|
||||
),
|
||||
const Text(
|
||||
'Absensi Karyawan',
|
||||
style: TextStyle(color: Colors.blue),
|
||||
)
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
masukkanAbsensi(
|
||||
data['nik'].toString(),
|
||||
data['jam_masuk'].toString(),
|
||||
data['jam_keluar'].toString(),
|
||||
data['tanggal'].toString(),
|
||||
context);
|
||||
},
|
||||
child: const Text('Masukkan Absensi')),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Batalkan')),
|
||||
],
|
||||
content: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 8.0),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Text(
|
||||
'Nama : ',
|
||||
style: TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Flexible(
|
||||
child: Text(
|
||||
data['nama'].toString(),
|
||||
style: const TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Text(
|
||||
'NIK : ',
|
||||
style: TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Flexible(
|
||||
child: Text(
|
||||
data['nik'].toString(),
|
||||
style: const TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Text(
|
||||
'Tanggal : ',
|
||||
style: TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
// ignore: prefer_const_constructors
|
||||
Flexible(
|
||||
child: Text(
|
||||
data['tanggal'].toString(),
|
||||
style: const TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Text(
|
||||
'Jam Masuk : ',
|
||||
style: TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Flexible(
|
||||
child: Text(
|
||||
data['jam_masuk'].toString(),
|
||||
style: const TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Text(
|
||||
'Jam Keluar : ',
|
||||
style: TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Flexible(
|
||||
child: Text(
|
||||
data['jam_keluar'].toString(),
|
||||
style: const TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
alertDialognya('Jaringan Bermasalah222, Sila Coba Kembali');
|
||||
}
|
||||
}
|
||||
|
||||
void alertDialognya(String message) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
Future.delayed(const Duration(seconds: 5), () {
|
||||
Navigator.of(context).pop(true);
|
||||
});
|
||||
return AlertDialog(
|
||||
title: Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.dangerous,
|
||||
color: Colors.red,
|
||||
),
|
||||
const Text(
|
||||
'Gagal',
|
||||
style: TextStyle(color: Colors.red),
|
||||
)
|
||||
],
|
||||
),
|
||||
content: Text(message),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// ignore: avoid_void_async
|
||||
void masukkanAbsensi(
|
||||
// ignore: non_constant_identifier_names
|
||||
String NIK,
|
||||
String jamMasuk,
|
||||
String jamKeluar,
|
||||
String tanggal,
|
||||
BuildContext context) async {
|
||||
final StatePetugas _petugas =
|
||||
Provider.of<StatePetugas>(context, listen: false);
|
||||
Navigator.pop(context, true);
|
||||
try {
|
||||
final String _returnString =
|
||||
await _petugas.absensiPetugas(NIK, jamMasuk, jamKeluar, tanggal);
|
||||
if (_returnString == 'success') {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
Future.delayed(const Duration(seconds: 6), () {
|
||||
Navigator.of(context).pop(true);
|
||||
});
|
||||
return AlertDialog(
|
||||
title: Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Icon(Icons.info, color: Colors.blue),
|
||||
const Text(
|
||||
'Sukses',
|
||||
style: TextStyle(color: Colors.blue),
|
||||
)
|
||||
],
|
||||
),
|
||||
content: const Text("Karyawan Sukses Diabsensi"),
|
||||
);
|
||||
},
|
||||
);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Karyawan Sukses Diabsensi')));
|
||||
} else {
|
||||
alertDialognya('Jaringan Bermasalah333, Sila Coba Kembali');
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ignore: non_constant_identifier_names
|
||||
401
lib/page/scanQRcode.dart
Normal file
401
lib/page/scanQRcode.dart
Normal file
@ -0,0 +1,401 @@
|
||||
import 'dart:convert';
|
||||
import 'package:barcode_absensi/page/login.dart';
|
||||
import 'package:barcode_absensi/page/pencarian.dart';
|
||||
import 'package:barcode_absensi/states/statePetugas.dart';
|
||||
import 'package:barcode_absensi/widgets/myWidgets.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class ScanQRCode extends StatefulWidget {
|
||||
@override
|
||||
_ScanQRCodeState createState() => _ScanQRCodeState();
|
||||
}
|
||||
|
||||
class _ScanQRCodeState extends State<ScanQRCode> {
|
||||
final int _selectedIndex = 0;
|
||||
late SharedPreferences sharedPreferences;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void _onItemTapped(int index) {
|
||||
// if (index == 1) {
|
||||
// print('sini logout');
|
||||
// }
|
||||
// setState(() {
|
||||
// _selectedIndex = index;
|
||||
// });
|
||||
if (index == 0) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => ScanQRCode(),
|
||||
));
|
||||
} else if (index == 1) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => Pencarian(),
|
||||
));
|
||||
} else {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text("Logout ?"),
|
||||
content: const Text("Anda Akan Logout Dari Sistem?"),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Batalkan')),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
sharedPreferences =
|
||||
await SharedPreferences.getInstance();
|
||||
sharedPreferences.remove('dataPetugas');
|
||||
Navigator.pushAndRemoveUntil(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => LoginPage(),
|
||||
),
|
||||
(route) => false,
|
||||
);
|
||||
},
|
||||
child: const Text('Ya')),
|
||||
],
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return WillPopScope(
|
||||
onWillPop: _onWillPop,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Halaman Scan QR-Code"),
|
||||
),
|
||||
body: const Center(
|
||||
child: Text(
|
||||
"Tekan tombol kamera pada sebelah kanan bawah skrin untuk scan QRCode karyawan",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
drawer: MyDrawer(),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () => AbsensiScanner(context),
|
||||
child: const Icon(Icons.center_focus_strong),
|
||||
),
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
items: const <BottomNavigationBarItem>[
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.camera),
|
||||
label: 'Scan QRCode',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.search),
|
||||
label: 'Pencarian',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.logout),
|
||||
label: 'Logout',
|
||||
),
|
||||
],
|
||||
currentIndex: _selectedIndex,
|
||||
selectedItemColor: Colors.amber[800],
|
||||
onTap: _onItemTapped,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> _onWillPop() async {
|
||||
bool ini = false;
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text("Keluar"),
|
||||
content: const Text("Yakin Ingin Keluar Dari Aplikasi ?"),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('No')),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
SystemChannels.platform
|
||||
.invokeMethod('SystemNavigator.pop');
|
||||
ini = true;
|
||||
},
|
||||
child: const Text('Yes')),
|
||||
],
|
||||
));
|
||||
return ini;
|
||||
}
|
||||
|
||||
// ignore: non_constant_identifier_names
|
||||
Future<void> AbsensiScanner(BuildContext context) async {
|
||||
// print("hehe");
|
||||
final StatePetugas _petugas =
|
||||
Provider.of<StatePetugas>(context, listen: false);
|
||||
|
||||
try {
|
||||
final qrCode = await FlutterBarcodeScanner.scanBarcode(
|
||||
'#ff6666', 'Cancel', true, ScanMode.QR);
|
||||
// print(qrCode);
|
||||
|
||||
try {
|
||||
final String _returnString =
|
||||
await _petugas.scanPetugas(qrCode.toString());
|
||||
sharedPreferences = await SharedPreferences.getInstance();
|
||||
// print(_returnString);
|
||||
if (_returnString == 'error') {
|
||||
alertDialognya('Jaringan Bermasalah111, Sila Coba Kembali');
|
||||
} else if (_returnString == 'suda_absen') {
|
||||
final json = jsonDecode(sharedPreferences.getString("errornya")!);
|
||||
|
||||
alertDialognya(json['message'].toString());
|
||||
sharedPreferences.remove('errornya');
|
||||
} else if (_returnString == 'data_tiada') {
|
||||
final json = jsonDecode(sharedPreferences.getString("errornya")!);
|
||||
|
||||
alertDialognya(json['message'].toString());
|
||||
sharedPreferences.remove('errornya');
|
||||
} else {
|
||||
final data = jsonDecode(_returnString);
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.info,
|
||||
color: Colors.blue,
|
||||
),
|
||||
const Text(
|
||||
'Absensi Karyawan',
|
||||
style: TextStyle(color: Colors.blue),
|
||||
)
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
masukkanAbsensi(
|
||||
data['nik'].toString(),
|
||||
data['jam_masuk'].toString(),
|
||||
data['jam_keluar'].toString(),
|
||||
data['tanggal'].toString(),
|
||||
context);
|
||||
},
|
||||
child: const Text('Masukkan Absensi')),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Batalkan')),
|
||||
],
|
||||
content: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 8.0),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Text(
|
||||
'Nama : ',
|
||||
style: TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Flexible(
|
||||
child: Text(
|
||||
data['nama'].toString(),
|
||||
style: const TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Text(
|
||||
'NIK : ',
|
||||
style: TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Flexible(
|
||||
child: Text(
|
||||
data['nik'].toString(),
|
||||
style: const TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Text(
|
||||
'Tanggal : ',
|
||||
style: TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
// ignore: prefer_const_constructors
|
||||
Flexible(
|
||||
child: Text(
|
||||
data['tanggal'].toString(),
|
||||
style: const TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Text(
|
||||
'Jam Masuk : ',
|
||||
style: TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Flexible(
|
||||
child: Text(
|
||||
data['jam_masuk'].toString(),
|
||||
style: const TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Text(
|
||||
'Jam Keluar : ',
|
||||
style: TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Flexible(
|
||||
child: Text(
|
||||
data['jam_keluar'].toString(),
|
||||
style: const TextStyle(),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 15,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
alertDialognya('Jaringan Bermasalah222, Sila Coba Kembali');
|
||||
}
|
||||
} catch (e) {
|
||||
alertDialognya('Scanner Barcode Gagal Berfungsi');
|
||||
}
|
||||
|
||||
// return 'hehe';
|
||||
}
|
||||
|
||||
// ignore: avoid_void_async
|
||||
void masukkanAbsensi(
|
||||
// ignore: non_constant_identifier_names
|
||||
String NIK,
|
||||
String jamMasuk,
|
||||
String jamKeluar,
|
||||
String tanggal,
|
||||
BuildContext context) async {
|
||||
final StatePetugas _petugas =
|
||||
Provider.of<StatePetugas>(context, listen: false);
|
||||
Navigator.pop(context, true);
|
||||
try {
|
||||
final String _returnString =
|
||||
await _petugas.absensiPetugas(NIK, jamMasuk, jamKeluar, tanggal);
|
||||
if (_returnString == 'success') {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
Future.delayed(const Duration(seconds: 6), () {
|
||||
Navigator.of(context).pop(true);
|
||||
});
|
||||
return AlertDialog(
|
||||
title: Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Icon(Icons.info, color: Colors.blue),
|
||||
const Text(
|
||||
'Sukses',
|
||||
style: TextStyle(color: Colors.blue),
|
||||
)
|
||||
],
|
||||
),
|
||||
content: const Text("Karyawan Sukses Diabsensi"),
|
||||
);
|
||||
},
|
||||
);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Karyawan Sukses Diabsensi')));
|
||||
} else {
|
||||
alertDialognya('Jaringan Bermasalah333, Sila Coba Kembali');
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
// ignore: always_declare_return_types
|
||||
void alertDialognya(String message) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
Future.delayed(const Duration(seconds: 5), () {
|
||||
Navigator.of(context).pop(true);
|
||||
});
|
||||
return AlertDialog(
|
||||
title: Row(
|
||||
// ignore: prefer_const_literals_to_create_immutables
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.dangerous,
|
||||
color: Colors.red,
|
||||
),
|
||||
const Text(
|
||||
'Gagal',
|
||||
style: TextStyle(color: Colors.red),
|
||||
)
|
||||
],
|
||||
),
|
||||
content: Text(message),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user