231 lines
6.7 KiB
Dart
231 lines
6.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:bluetooth_print/bluetooth_print.dart';
|
|
import 'package:bluetooth_print/bluetooth_print_model.dart';
|
|
|
|
class PrinterSetupScreen extends StatefulWidget {
|
|
const PrinterSetupScreen({super.key});
|
|
|
|
@override
|
|
State<PrinterSetupScreen> createState() => _PrinterSetupScreenState();
|
|
}
|
|
|
|
class _PrinterSetupScreenState extends State<PrinterSetupScreen> {
|
|
BluetoothPrint bluetoothPrint = BluetoothPrint.instance;
|
|
bool _isScanning = false;
|
|
List<BluetoothDevice> _devices = [];
|
|
BluetoothDevice? _selectedDevice;
|
|
bool _connected = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initBluetooth();
|
|
}
|
|
|
|
Future<void> _initBluetooth() async {
|
|
// Periksa status koneksi
|
|
final isConnected = await bluetoothPrint.isConnected ?? false;
|
|
setState(() {
|
|
_connected = isConnected;
|
|
});
|
|
|
|
// Listen to bluetooth state changes
|
|
bluetoothPrint.state.listen((state) {
|
|
if (mounted) {
|
|
switch (state) {
|
|
case BluetoothPrint.CONNECTED:
|
|
setState(() {
|
|
_connected = true;
|
|
});
|
|
break;
|
|
case BluetoothPrint.DISCONNECTED:
|
|
setState(() {
|
|
_connected = false;
|
|
_selectedDevice = null;
|
|
});
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _scanDevices() async {
|
|
setState(() {
|
|
_isScanning = true;
|
|
_devices = [];
|
|
});
|
|
|
|
try {
|
|
// Mulai scan perangkat Bluetooth
|
|
await bluetoothPrint.startScan(timeout: const Duration(seconds: 4));
|
|
|
|
// Listen to scan results
|
|
bluetoothPrint.scanResults.listen((devices) {
|
|
if (mounted) {
|
|
setState(() {
|
|
_devices = devices;
|
|
});
|
|
}
|
|
});
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Gagal memindai perangkat: $e')),
|
|
);
|
|
}
|
|
} finally {
|
|
setState(() {
|
|
_isScanning = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> _connectToDevice(BluetoothDevice device) async {
|
|
try {
|
|
await bluetoothPrint.connect(device);
|
|
setState(() {
|
|
_selectedDevice = device;
|
|
});
|
|
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Berhasil terhubung ke printer')),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Gagal terhubung ke printer: $e')),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _disconnect() async {
|
|
try {
|
|
await bluetoothPrint.disconnect();
|
|
setState(() {
|
|
_selectedDevice = null;
|
|
});
|
|
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Printer terputus')),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Gagal memutus koneksi: $e')),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Pengaturan Printer'),
|
|
centerTitle: true,
|
|
),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
children: [
|
|
const Text(
|
|
'Setup Printer Bluetooth',
|
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
|
),
|
|
const SizedBox(height: 16),
|
|
const Text(
|
|
'Hubungkan printer thermal Anda melalui Bluetooth untuk mencetak struk.',
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// Status koneksi
|
|
Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: Colors.grey),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
const Text('Status Printer:'),
|
|
Text(
|
|
_connected ? 'Terhubung' : 'Terputus',
|
|
style: TextStyle(
|
|
color: _connected ? Colors.green : Colors.red,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
// Tombol scan
|
|
ElevatedButton.icon(
|
|
onPressed: _isScanning ? null : _scanDevices,
|
|
icon: _isScanning
|
|
? const SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(Icons.bluetooth_searching),
|
|
label: Text(_isScanning ? 'Memindai...' : 'Cari Printer'),
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
// Daftar perangkat
|
|
Expanded(
|
|
child: _devices.isEmpty
|
|
? const Center(
|
|
child: Text('Tidak ada perangkat ditemukan'),
|
|
)
|
|
: ListView.builder(
|
|
itemCount: _devices.length,
|
|
itemBuilder: (context, index) {
|
|
final device = _devices[index];
|
|
final isSelected = _selectedDevice?.address == device.address;
|
|
|
|
return Card(
|
|
child: ListTile(
|
|
title: Text(device.name ?? 'Unknown Device'),
|
|
subtitle: Text(device.address ?? ''),
|
|
trailing: isSelected
|
|
? const Icon(Icons.check, color: Colors.green)
|
|
: null,
|
|
onTap: () => _connectToDevice(device),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
|
|
// Tombol disconnect
|
|
if (_connected)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 16),
|
|
child: ElevatedButton.icon(
|
|
onPressed: _disconnect,
|
|
icon: const Icon(Icons.bluetooth_disabled),
|
|
label: const Text('Putus Koneksi'),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.red,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |