Advertisement
agung_py

Tidak Muncul sepenuhnya

Feb 14th, 2024
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.55 KB | Source Code | 0 0
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
  5. import 'package:printer/printer.dart';
  6.  
  7. void main() {
  8.   runApp(MyApp());
  9. }
  10.  
  11. class MyApp extends StatelessWidget {
  12.   @override
  13.   Widget build(BuildContext context) {
  14.     return MaterialApp(
  15.       title: 'Master Barang App',
  16.       home: MasterBarangApp(),
  17.     );
  18.   }
  19. }
  20.  
  21. class MasterBarangApp extends StatefulWidget {
  22.   @override
  23.   _MasterBarangAppState createState() => _MasterBarangAppState();
  24. }
  25.  
  26. class _MasterBarangAppState extends State<MasterBarangApp> {
  27.   TextEditingController kodeBarangController = TextEditingController();
  28.   TextEditingController barcodeController = TextEditingController();
  29.   TextEditingController namaBarangController = TextEditingController();
  30.   TextEditingController hargaJualController = TextEditingController();
  31.   TextEditingController qtyController = TextEditingController();
  32.   TextEditingController lokasiController = TextEditingController();
  33.  
  34.   final String apiUrl =
  35.       "http://192.168.0.1:3000/master_barang"; // Ganti dengan URL backend Anda
  36.  
  37.   Future<void> fetchProduct(String kodeBarang) async {
  38.     final response = await http.get(Uri.parse('$apiUrl/$kodeBarang'));
  39.  
  40.     if (response.statusCode == 200) {
  41.       final product = json.decode(response.body);
  42.       if (product != null) {
  43.         setState(() {
  44.           kodeBarangController.text = product['kodeBarang'] ?? '';
  45.           barcodeController.text = product['barcode'] ?? '';
  46.           namaBarangController.text = product['namaBarang'] ?? '';
  47.           hargaJualController.text = (product['hargaJual'] ?? '').toString();
  48.           qtyController.text = (product['qty'] ?? '').toString();
  49.           lokasiController.text = product['lokasi'] ?? '';
  50.           /*print('Response Body: ${response.body}');
  51.           print('Product Object: $product');
  52.           print('Type of namaBarang: ${product['namaBarang'].runtimeType}');
  53.           print('Type of hargaJual: ${product['hargaJual'].runtimeType}');*/
  54.         });
  55.       } else {
  56.         clearControllers();
  57.       }
  58.     }
  59.   }
  60.  
  61.   Future<void> addProduct() async {
  62.     await http.post(
  63.       Uri.parse(apiUrl),
  64.       headers: {"Content-Type": "application/json"},
  65.       body: json.encode({
  66.         "kodeBarang": kodeBarangController.text,
  67.         "barcode": barcodeController.text,
  68.         "namaBarang": namaBarangController.text,
  69.         "hargaJual": hargaJualController.text,
  70.         "qty": qtyController.text,
  71.         "lokasi": lokasiController.text,
  72.       }),
  73.     );
  74.     clearControllers();
  75.     kodeBarangController.clear();
  76.   }
  77.  
  78.   Future<void> deleteProduct() async {
  79.     await http.delete(
  80.       Uri.parse('$apiUrl/${kodeBarangController.text}'),
  81.     );
  82.     clearControllers();
  83.     kodeBarangController.clear();
  84.   }
  85.  
  86.   void clearControllers() {
  87.     barcodeController.clear();
  88.     namaBarangController.clear();
  89.     hargaJualController.clear();
  90.     qtyController.clear();
  91.     lokasiController.clear();
  92.   }
  93.  
  94.   Future<void> printBarcode() async {
  95.     // Implement your barcode printing logic here
  96.     // This could involve using a third-party library or a native plugin
  97.     // to communicate with a barcode printer.
  98.     // For example, you might use the `esc_pos` package:
  99.     // https://pub.dev/packages/esc_pos
  100.     print('Printing barcode for ${kodeBarangController.text}');
  101.   }
  102.  
  103.   @override
  104.   Widget build(BuildContext context) {
  105.     return Scaffold(
  106.       appBar: AppBar(
  107.         title: Text('Master Barang'),
  108.       ),
  109.       body: SingleChildScrollView(
  110.         child: Padding(
  111.           padding: const EdgeInsets.all(16.0),
  112.           child: Column(
  113.             crossAxisAlignment: CrossAxisAlignment.stretch,
  114.             children: [
  115.               TextField(
  116.                 controller: kodeBarangController,
  117.                 decoration: InputDecoration(labelText: 'Kode Barang'),
  118.               ),
  119.               ElevatedButton(
  120.                 onPressed: () async {
  121.                   if (kodeBarangController.text != null) {
  122.                     await fetchProduct(kodeBarangController.text);
  123.                   }
  124.                 },
  125.                 child: Text('Cari'),
  126.               ),
  127.               TextField(
  128.                 controller: barcodeController,
  129.                 decoration: InputDecoration(labelText: 'Barcode'),
  130.               ),
  131.               TextField(
  132.                 controller: namaBarangController,
  133.                 decoration: InputDecoration(labelText: 'Nama Barang'),
  134.               ),
  135.               TextField(
  136.                 controller: hargaJualController,
  137.                 decoration: InputDecoration(labelText: 'Harga Jual'),
  138.               ),
  139.               TextField(
  140.                 controller: qtyController,
  141.                 decoration: InputDecoration(labelText: 'Qty'),
  142.               ),
  143.               TextField(
  144.                 controller: lokasiController,
  145.                 decoration: InputDecoration(labelText: 'Lokasi'),
  146.               ),
  147.               ElevatedButton(
  148.                 onPressed: addProduct,
  149.                 child: Text('Simpan'),
  150.               ),
  151.               if (qtyController.text == '0' || qtyController.text.isEmpty)
  152.                 ElevatedButton(
  153.                   onPressed: deleteProduct,
  154.                   child: Text('Hapus'),
  155.                 ),
  156.               ElevatedButton(
  157.                 onPressed: printBarcode,
  158.                 child: Text('Cetak Barcode'),
  159.               ),
  160.             ],
  161.           ),
  162.         ),
  163.       ),
  164.     );
  165.   }
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement