اكمل هذا import 'dart:async'; import 'dart:convert'; import…
- User
اكمل هذا import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:flutter_dotenv/flutter_dotenv.dart';
class DashboardScreen extends StatefulWidget { final String username; final Map<String, dynamic> userData;
const DashboardScreen({ Key? key, required this.username, required this.userData, }) : super(key: key);
@override State createState() => _DashboardScreenState(); }
class _DashboardScreenState extends State { late Map<String, dynamic> currentUserData; Timer? updateTimer; bool isLoggingOut = false;
@override void initState() { super.initState(); currentUserData = widget.userData; _startAutoUpdate(); }
void startAutoUpdate() { updateTimer = Timer.periodic(const Duration(seconds: 5), () { _fetchUserData(); }); }
Future fetchUserData() async { final String baseUrl = dotenv.env['API_BASE_URL'] ?? 'http://172.16.0.1'; try { final res = await http .get(Uri.parse('$baseUrl/login?var=json')) .timeout(const Duration(seconds: 10)); if (res.statusCode == 200) { final data = json.decode(res.body); if (!mounted) return; if (!data.containsKey('errors')) { setState(() { currentUserData = data; }); } } } catch () { // تجاهل الأخطاء } }
Future _logout() async { final String baseUrl = dotenv.env['API_BASE_URL'] ?? 'http://172.16.0.1'; setState(() => isLoggingOut = true); try { final res = await http .post( Uri.parse('$baseUrl/logout'), headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: {'username': widget.username}, ) .timeout(const Duration(seconds: 10));
codeif (res.statusCode == 200 && mounted) { Navigator.pop(context); } else if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('فشل في تسجيل الخروج'), backgroundColor: Colors.red, ), ); } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('حدث خطأ: $e'), backgroundColor: Colors.red), ); } } finally { if (mounted) { setState(() => isLoggingOut = false); } }}
String _formatBytes(dynamic bytes) { int value = 0; if (bytes is String) { value = int.tryParse(bytes) ?? 0; } else if (bytes is int) { value = bytes; } if (value < 1024) return '{(value / 1024).toStringAsFixed(1)} كيلوبايت'; if (value < 1024 * 1024 * 1024) return '{(value / (1024 * 1024 * 1024)).toStringAsFixed(1)} جيجابايت'; }
- Assistantdart
CircleAvatar( radius: 24, backgroundColor: color.withOpacity(0.1), child: Icon( icon, color: color, size: 28, ), ), const SizedBox(height: 12), Text( title, style: TextStyle( fontWeight: FontWeight.bold, color: Colors.grey[800], ), ), const SizedBox(height: 6), Text( value, style: TextStyle( fontSize: 16, color: color, fontWeight: FontWeight.w600, ), textAlign: TextAlign.center, ), ], ), ), ); } }