import 'dart:io'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:share_plus/share_plus.dart'; import 'package:path/path.dart' as p; import '../database/siab_database.dart'; import '../services/ble_service.dart'; /// Screen that shows the current timbang (weight) value on a scale-style /// display matching the SIAB scale design (dark frame, glowing red digits). /// When connected, title shows "TIMBANGAN [label] SIAB" from registered device. class TimbangScreen extends StatelessWidget { const TimbangScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.black, body: SafeArea( child: Consumer( builder: (context, ble, _) { final weightKg = ble.lastWeightKg; final displayValue = weightKg != null ? weightKg.toStringAsFixed(1) : '0.0'; final isConnected = ble.isConnected; final address = ble.deviceAddress; return Center( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 32), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ // Outer frame with decorative corners Stack( children: [ // Outer dark grey/blue frame Container( padding: const EdgeInsets.all(4), decoration: BoxDecoration( color: const Color(0xFF2A2A3A), // Dark grey/blue borderRadius: BorderRadius.circular(16), ), child: Stack( children: [ // Inner border (lighter grey/blue line) Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(12), border: Border.all( color: const Color(0xFF4A4A5A), width: 2, ), ), child: // Deep black display area Container( padding: const EdgeInsets.symmetric( vertical: 40, horizontal: 24, ), decoration: BoxDecoration( color: Colors.black, borderRadius: BorderRadius.circular(10), ), child: Column( mainAxisSize: MainAxisSize.min, children: [ // Large glowing red weight value Text( displayValue, style: TextStyle( fontSize: 72, fontWeight: FontWeight.w400, color: Colors.red.shade400, fontFamily: 'monospace', fontFeatures: const [ FontFeature.tabularFigures(), ], shadows: [ Shadow( color: Colors.red.shade400 .withOpacity(0.8), blurRadius: 20, ), Shadow( color: Colors.red.shade400 .withOpacity(0.5), blurRadius: 40, ), ], ), ), const SizedBox(height: 8), // Smaller glowing red "kg" unit Text( 'kg', style: TextStyle( fontSize: 24, fontWeight: FontWeight.w400, color: Colors.red.shade400, fontFamily: 'monospace', shadows: [ Shadow( color: Colors.red.shade400 .withOpacity(0.8), blurRadius: 15, ), Shadow( color: Colors.red.shade400 .withOpacity(0.5), blurRadius: 30, ), ], ), ), ], ), ), ), // Corner decorative elements (rivets/screws) Positioned( top: 2, left: 2, child: _CornerRivet(), ), Positioned( top: 2, right: 2, child: _CornerRivet(), ), Positioned( bottom: 2, left: 2, child: _CornerRivet(), ), Positioned( bottom: 2, right: 2, child: _CornerRivet(), ), ], ), ), ], ), const SizedBox(height: 24), // "TIMBANGAN [label] SIAB" when connected, else "TIMBANGAN SIAB" FutureBuilder( future: address != null ? SiabDatabase().getByUuid(address).then((d) => d?.label ?? '') : Future.value(''), builder: (context, snapshot) { final label = snapshot.data?.trim() ?? ''; final title = isConnected && label.isNotEmpty ? 'TIMBANGAN $label SIAB' : 'TIMBANGAN SIAB'; return Text( title, style: TextStyle( fontSize: 16, color: Colors.grey.shade400, fontWeight: FontWeight.w300, letterSpacing: 1.2, ), ); }, ), const SizedBox(height: 16), // Connection status + actions if (!isConnected) Text( 'Connect scale in Devices tab', style: TextStyle( fontSize: 12, color: Colors.grey.shade600, ), ) else ...[ Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.bluetooth_connected, size: 20, color: Colors.green.shade300, ), const SizedBox(width: 12), TextButton.icon( onPressed: () async { await ble.disconnect(); }, icon: const Icon(Icons.power_settings_new, size: 18), label: const Text('Disconnect'), style: TextButton.styleFrom( foregroundColor: Colors.red.shade300, ), ), ], ), const SizedBox(height: 12), ElevatedButton.icon( onPressed: ble.hasStableWeight ? () async { final ok = await ble.saveStableMeasurement(); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(ok ? 'Stable weight saved to file' : 'No stable value to save yet'), ), ); } : null, icon: const Icon(Icons.save), label: const Text('Save stable value'), ), const SizedBox(height: 8), OutlinedButton.icon( onPressed: () async { try { final file = await ble.getTodayMeasurementFile(); final exists = await file.exists(); if (!exists || await file.length() == 0) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text( 'No measurements file for today yet. Save a value first.', ), ), ); return; } await Share.shareXFiles( [ XFile( file.path, mimeType: 'text/csv', name: p.basename(file.path), ), ], subject: 'SIAB Laporan hasil Timbang ', text: 'SIAB Laporan hasil Timbang harian.', ); } catch (e) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('gagal mengirim file: $e'), ), ); } }, icon: const Icon(Icons.email), label: const Text('email terkirim'), ), ], ], ), ), ); }, ), ), ); } } /// Decorative corner rivet/screw element class _CornerRivet extends StatelessWidget { @override Widget build(BuildContext context) { return Container( width: 8, height: 8, decoration: BoxDecoration( color: Colors.grey.shade500, shape: BoxShape.circle, boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.3), blurRadius: 2, offset: const Offset(0, 1), ), ], ), ); } }