144 lines
4.8 KiB
Dart
144 lines
4.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../services/ble_service.dart';
|
|
import '../widgets/data_packet_card.dart';
|
|
import '../widgets/statistics_card.dart';
|
|
import '../widgets/connection_card.dart';
|
|
|
|
class HomeScreen extends StatefulWidget {
|
|
const HomeScreen({super.key});
|
|
|
|
@override
|
|
State<HomeScreen> createState() => _HomeScreenState();
|
|
}
|
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
|
final ScrollController _scrollController = ScrollController();
|
|
|
|
@override
|
|
void dispose() {
|
|
_scrollController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Timbangan Bluetooh'),
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
actions: [
|
|
Consumer<BLEService>(
|
|
builder: (context, ble, _) {
|
|
if (ble.isConnected) {
|
|
return IconButton(
|
|
icon: const Icon(Icons.delete_outline),
|
|
tooltip: 'Clear Data',
|
|
onPressed: () {
|
|
ble.clearData();
|
|
},
|
|
);
|
|
}
|
|
return const SizedBox.shrink();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
body: Consumer<BLEService>(
|
|
builder: (context, ble, _) {
|
|
return Column(
|
|
children: [
|
|
// Connection Card
|
|
ConnectionCard(ble: ble),
|
|
|
|
// Statistics Card
|
|
if (ble.isConnected) StatisticsCard(ble: ble),
|
|
|
|
// Data Packets List
|
|
Expanded(
|
|
child: ble.isConnected && ble.dataPackets.isNotEmpty
|
|
? ListView.builder(
|
|
controller: _scrollController,
|
|
padding: const EdgeInsets.all(8),
|
|
itemCount: ble.dataPackets.length,
|
|
reverse: true, // Show newest first
|
|
itemBuilder: (context, index) {
|
|
final packet = ble.dataPackets[ble.dataPackets.length - 1 - index];
|
|
return DataPacketCard(packet: packet);
|
|
},
|
|
)
|
|
: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
ble.isConnected
|
|
? Icons.bluetooth_connected
|
|
: Icons.bluetooth_disabled,
|
|
size: 64,
|
|
color: Colors.grey,
|
|
),
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
ble.isConnected
|
|
? 'Waiting for data...'
|
|
: 'Not connected',
|
|
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
|
color: Colors.grey,
|
|
),
|
|
),
|
|
if (ble.isConnected) ...[
|
|
const SizedBox(height: 8),
|
|
const Text(
|
|
'Data packets will appear here',
|
|
style: TextStyle(color: Colors.grey),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
floatingActionButton: Consumer<BLEService>(
|
|
builder: (context, ble, _) {
|
|
if (ble.isConnecting || ble.isScanning) {
|
|
return const FloatingActionButton(
|
|
onPressed: null,
|
|
child: CircularProgressIndicator(color: Colors.white),
|
|
);
|
|
}
|
|
|
|
if (ble.isConnected) {
|
|
return FloatingActionButton(
|
|
onPressed: () async {
|
|
await ble.disconnect();
|
|
ble.clearData();
|
|
},
|
|
backgroundColor: Colors.red,
|
|
child: const Icon(Icons.bluetooth_disabled),
|
|
);
|
|
}
|
|
|
|
return FloatingActionButton(
|
|
onPressed: () async {
|
|
final success = await ble.autoConnect();
|
|
if (!success && mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Koneksi ke timbangan gagal'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
},
|
|
child: const Icon(Icons.bluetooth_searching),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|