Siopas Inventory PETI for ISTW Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
7.6 KiB

import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:data_table_2/data_table_2.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:siopas/models/asset_status_model.dart';
import 'package:siopas/models/m_asset_status_model.dart';
import 'package:siopas/pages/pengembalian_barang/show.dart';
import 'package:siopas/pages/transfer_peti/show.dart';
import 'package:siopas/providers/asset_status_provider.dart';
import 'package:http/http.dart' as http;
import '../../connection/connection.dart';
import '../peminjaman_barang/show.dart';
class TransferPetiPage extends StatefulWidget {
const TransferPetiPage({super.key});
@override
State<TransferPetiPage> createState() => TransferPetiPageState();
}
class TransferPetiPageState extends State<TransferPetiPage> {
String? token;
int _currentPage = 1;
int _pageSize = 10;
List<PetiAssetModel> _data = [];
bool _isLoading = false;
@override
void initState() {
super.initState();
_getUserToken();
fetchData();
}
void _getUserToken() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (mounted) {
setState(() {
token = prefs.getString('token');
});
}
}
Future<void> fetchData() async {
if (mounted) {
setState(() {
_isLoading = true;
});
try {
final response = await http.get(Uri.parse('$baseUrl/peti-asset'));
if (response.statusCode == 200) {
final jsonData = json.decode(response.body)['data']['petis'];
final List<PetiAssetModel> newData = (jsonData as List)
.map((item) => PetiAssetModel.fromJson(item))
.toList();
if (mounted) {
setState(() {
_data.addAll(newData);
_isLoading = false;
});
}
} else {
if (mounted) {
setState(() {
_isLoading = false;
});
}
throw Exception('Failed to fetch data');
}
} catch (e) {
if (mounted) {
setState(() {
_isLoading = false;
});
}
print('Error fetching data: $e');
}
}
}
void _loadMoreData() {
if (mounted && !_isLoading) {
setState(() {
_currentPage++;
});
fetchData();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.indigo[700],
elevation: 0,
title: Text('Data Peti (Transfer Peti)',
style: TextStyle(
fontSize: 16,
)),
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
Navigator.pushNamed(context, '/home');
},
),
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: SingleChildScrollView(
child: SizedBox(
width: double.infinity,
child: PaginatedDataTable(
header: const Text('Menu Peti'),
rowsPerPage: _pageSize,
availableRowsPerPage: const [10, 25, 50],
onRowsPerPageChanged: (value) {
setState(() {
_pageSize = value!;
});
},
columns: const [
DataColumn(label: Text('No')),
DataColumn(label: Text('')),
DataColumn(label: Text('Customer')),
DataColumn(label: Text('Gudang')),
DataColumn(label: Text('Fix Lot')),
DataColumn(label: Text('Tipe Peti')),
DataColumn(label: Text('Ukuran Peti')),
DataColumn(label: Text('Lot No')),
DataColumn(label: Text('Status Peti')),
DataColumn(label: Text('Packing No')),
],
source: _DataSource(data: _data, context: context),
),
),
),
bottomNavigationBar: BottomAppBar(
color: Color.fromARGB(255, 5, 28, 158), // Warna latar belakang
child: Container(
height: 65.0,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
InkWell(
customBorder: CircleBorder(),
onTap: () {
// Aksi ketika ikon diklik
Navigator.pushNamed(context, '/transfer-peti/edit');
},
child: Container(
width: 45,
height: 45,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.yellow[800],
),
child: Icon(
Icons.local_shipping,
size: 30,
color: Colors.white,
),
),
),
],
),
),
),
);
}
}
class _DataSource extends DataTableSource {
final List<PetiAssetModel> data;
final BuildContext context;
_DataSource({required this.data, required this.context});
@override
DataRow? getRow(int index) {
if (index >= data.length) {
return null;
}
final item = data[index];
return DataRow(cells: [
DataCell(
Text(
(index + 1).toString(),
),
),
DataCell(
GestureDetector(
onTap: () {
if (item.id != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailTransferPetiPage(
petiId: item.id!,
),
),
);
print('asset id: ${item.id}');
}
},
child: Icon(
Icons.article_outlined,
size: 30,
color: Colors.indigo[700],
),
),
),
DataCell(
Text(
item.customer!.name!.toString() != 'null'
? item.customer!.name.toString()
: '-',
),
),
DataCell(
Text(
item.warehouse!.name!.toString() != 'null'
? item.warehouse!.name.toString()
: '-',
),
),
DataCell(
Text(
item.fix_lot.toString() != 'null' ? item.fix_lot.toString() : '-',
),
),
DataCell(
Text(
item.tipe_peti!.type.toString() != 'null'
? item.tipe_peti!.type.toString()
: '-',
),
),
DataCell(
Text(item.tipe_peti!.size_peti.toString() != 'null'
? item.tipe_peti!.size_peti.toString()
: '-'),
),
DataCell(
Text(
item.customer!.lot_no.toString() != 'null'
? item.customer!.lot_no.toString()
: '-',
),
),
DataCell(
Text(
item.status_disposal.toString() != 'null'
? item.status_disposal.toString()
: '-',
),
),
DataCell(
Text(
item.packing_no.toString() != 'null'
? item.packing_no.toString()
: '-',
),
),
]);
}
@override
bool get isRowCountApproximate => false;
@override
int get rowCount => data.length;
@override
int get selectedRowCount => 0;
}