Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
- import 'package:printer/printer.dart';
- void main() {
- runApp(MyApp());
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Master Barang App',
- home: MasterBarangApp(),
- );
- }
- }
- class MasterBarangApp extends StatefulWidget {
- @override
- _MasterBarangAppState createState() => _MasterBarangAppState();
- }
- class _MasterBarangAppState extends State<MasterBarangApp> {
- TextEditingController kodeBarangController = TextEditingController();
- TextEditingController barcodeController = TextEditingController();
- TextEditingController namaBarangController = TextEditingController();
- TextEditingController hargaJualController = TextEditingController();
- TextEditingController qtyController = TextEditingController();
- TextEditingController lokasiController = TextEditingController();
- final String apiUrl =
- "http://192.168.0.1:3000/master_barang"; // Ganti dengan URL backend Anda
- Future<void> fetchProduct(String kodeBarang) async {
- final response = await http.get(Uri.parse('$apiUrl/$kodeBarang'));
- if (response.statusCode == 200) {
- final product = json.decode(response.body);
- if (product != null) {
- setState(() {
- kodeBarangController.text = product['kodeBarang'] ?? '';
- barcodeController.text = product['barcode'] ?? '';
- namaBarangController.text = product['namaBarang'] ?? '';
- hargaJualController.text = (product['hargaJual'] ?? '').toString();
- qtyController.text = (product['qty'] ?? '').toString();
- lokasiController.text = product['lokasi'] ?? '';
- /*print('Response Body: ${response.body}');
- print('Product Object: $product');
- print('Type of namaBarang: ${product['namaBarang'].runtimeType}');
- print('Type of hargaJual: ${product['hargaJual'].runtimeType}');*/
- });
- } else {
- clearControllers();
- }
- }
- }
- Future<void> addProduct() async {
- await http.post(
- Uri.parse(apiUrl),
- headers: {"Content-Type": "application/json"},
- body: json.encode({
- "kodeBarang": kodeBarangController.text,
- "barcode": barcodeController.text,
- "namaBarang": namaBarangController.text,
- "hargaJual": hargaJualController.text,
- "qty": qtyController.text,
- "lokasi": lokasiController.text,
- }),
- );
- clearControllers();
- kodeBarangController.clear();
- }
- Future<void> deleteProduct() async {
- await http.delete(
- Uri.parse('$apiUrl/${kodeBarangController.text}'),
- );
- clearControllers();
- kodeBarangController.clear();
- }
- void clearControllers() {
- barcodeController.clear();
- namaBarangController.clear();
- hargaJualController.clear();
- qtyController.clear();
- lokasiController.clear();
- }
- Future<void> printBarcode() async {
- // Implement your barcode printing logic here
- // This could involve using a third-party library or a native plugin
- // to communicate with a barcode printer.
- // For example, you might use the `esc_pos` package:
- // https://pub.dev/packages/esc_pos
- print('Printing barcode for ${kodeBarangController.text}');
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Master Barang'),
- ),
- body: SingleChildScrollView(
- child: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- TextField(
- controller: kodeBarangController,
- decoration: InputDecoration(labelText: 'Kode Barang'),
- ),
- ElevatedButton(
- onPressed: () async {
- if (kodeBarangController.text != null) {
- await fetchProduct(kodeBarangController.text);
- }
- },
- child: Text('Cari'),
- ),
- TextField(
- controller: barcodeController,
- decoration: InputDecoration(labelText: 'Barcode'),
- ),
- TextField(
- controller: namaBarangController,
- decoration: InputDecoration(labelText: 'Nama Barang'),
- ),
- TextField(
- controller: hargaJualController,
- decoration: InputDecoration(labelText: 'Harga Jual'),
- ),
- TextField(
- controller: qtyController,
- decoration: InputDecoration(labelText: 'Qty'),
- ),
- TextField(
- controller: lokasiController,
- decoration: InputDecoration(labelText: 'Lokasi'),
- ),
- ElevatedButton(
- onPressed: addProduct,
- child: Text('Simpan'),
- ),
- if (qtyController.text == '0' || qtyController.text.isEmpty)
- ElevatedButton(
- onPressed: deleteProduct,
- child: Text('Hapus'),
- ),
- ElevatedButton(
- onPressed: printBarcode,
- child: Text('Cetak Barcode'),
- ),
- ],
- ),
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement