import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'sensor_provider.dart'; import 'package:timeago/timeago.dart' as timeago; void main() { runApp( ChangeNotifierProvider( create: (context) => SensorProvider(), child: const MyApp(), ), ); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Sensor App', theme: ThemeData(primarySwatch: Colors.blue), home: const SensorScreen(), ); } } class SensorScreen extends StatefulWidget { const SensorScreen({super.key}); @override _SensorScreenState createState() => _SensorScreenState(); } class _SensorScreenState extends State { @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { Provider.of(context, listen: false).fetchData(); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Mercury')), body: Center( child: Consumer( builder: (context, sensorProvider, child) { if (sensorProvider.sensorData == null) { if (sensorProvider.error != null) { return Text('Error: ${sensorProvider.error}'); } return const CircularProgressIndicator(); } else { final data = sensorProvider.sensorData!; final timeAgo = timeago.format( DateTime.fromMillisecondsSinceEpoch(data.timestamp), ); return Scrollbar( child: SingleChildScrollView( primary: true, padding: const EdgeInsets.all(16.0), child: Column( children: [ // Temperature Card Card( elevation: 4, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), ), child: Container( width: double.infinity, padding: const EdgeInsets.all(20), decoration: BoxDecoration( gradient: LinearGradient( colors: [ Colors.blue.shade200, Colors.blue.shade400, ], begin: Alignment.topLeft, end: Alignment.bottomRight, ), borderRadius: BorderRadius.circular(16), ), child: Column( children: [ const Icon( Icons.thermostat, size: 40, color: Colors.white, ), const SizedBox(height: 10), Text( '${data.temperature}°C', style: const TextStyle( fontSize: 32, fontWeight: FontWeight.bold, color: Colors.white, ), ), const SizedBox(height: 5), const Text( 'Temperature', style: TextStyle( fontSize: 16, color: Colors.white70, ), ), ], ), ), ), const SizedBox(height: 20), // Humidity Card Card( elevation: 4, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), ), child: Container( width: double.infinity, padding: const EdgeInsets.all(20), decoration: BoxDecoration( gradient: LinearGradient( colors: [ Colors.green.shade200, Colors.green.shade400, ], begin: Alignment.topLeft, end: Alignment.bottomRight, ), borderRadius: BorderRadius.circular(16), ), child: Column( children: [ const Icon( Icons.water_drop, size: 40, color: Colors.white, ), const SizedBox(height: 10), Text( '${data.humidity}%', style: const TextStyle( fontSize: 32, fontWeight: FontWeight.bold, color: Colors.white, ), ), const SizedBox(height: 5), const Text( 'Humidity', style: TextStyle( fontSize: 16, color: Colors.white70, ), ), ], ), ), ), const SizedBox(height: 20), // Dew Point Card Card( elevation: 4, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), ), child: Container( width: double.infinity, padding: const EdgeInsets.all(20), decoration: BoxDecoration( gradient: LinearGradient( colors: [ Colors.purple.shade200, Colors.purple.shade400, ], begin: Alignment.topLeft, end: Alignment.bottomRight, ), borderRadius: BorderRadius.circular(16), ), child: Column( children: [ const Icon( Icons.ac_unit, size: 40, color: Colors.white, ), const SizedBox(height: 10), Text( '${data.dewPoint}°C', style: const TextStyle( fontSize: 32, fontWeight: FontWeight.bold, color: Colors.white, ), ), const SizedBox(height: 5), const Text( 'Dew Point', style: TextStyle( fontSize: 16, color: Colors.white70, ), ), ], ), ), ), const SizedBox(height: 20), // Timestamp Text( '🕒 Last updated: $timeAgo', style: const TextStyle( fontSize: 16, color: Colors.grey, ), ), ], ), ), ); } }, ), ), floatingActionButton: FloatingActionButton( onPressed: () { Provider.of(context, listen: false).fetchData(); }, child: const Icon(Icons.refresh), ), ); } }