import 'package:flutter/material.
dart';
import 'package:http/[Link]' as http;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ' تحكم فيLED',
home: LedControlPage(),
);
}
}
class LedControlPage extends StatefulWidget {
@override
_LedControlPageState createState() => _LedControlPageState();
}
class _LedControlPageState extends State<LedControlPage> {
String esp32IP = "[Link]"; // غير هذا العنوان لIP ESP32
bool isLoading = false;
String ledStatus = ";"غير معروف
Future<void> controlLed(String action) async {
setState(() {
isLoading = true;
});
try {
final response = await [Link](
[Link]('[Link]
headers: {'Content-Type': 'application/json'},
);
if ([Link] == 200) {
setState(() {
ledStatus = action;
});
print(' تم$action LED ;)'بنجاح
} else {
print('فشل في التحكم: ${[Link]}');
}
} catch (e) {
print('خطأ في االتصال: $e');
} finally {
setState(() {
isLoading = false;
});
}
}
Future<void> getLedStatus() async {
try {
final response = await [Link](
[Link]('[Link]
);
if ([Link] == 200) {
// تحليل الresponse لمعرفة حالة الLED
setState(() {
ledStatus = [Link]('on') ? 'on' : 'off';
});
}
} catch (e) {
print('خطأ في جلب الحالة: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(' تحكم فيLED'),
backgroundColor: [Link],
),
body: Center(
child: Column(
mainAxisAlignment: [Link],
children: [
Text(
' حالة الLED: $ledStatus',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 30),
if (isLoading) CircularProgressIndicator(),
if (!isLoading) ...[
ElevatedButton(
onPressed: () => controlLed('on'),
child: Text(' تشغيل الLED'),
style: [Link](
backgroundColor: [Link],
foregroundColor: [Link],
padding: [Link](horizontal: 30, vertical: 15),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () => controlLed('off'),
child: Text(' إطفاء الLED'),
style: [Link](
backgroundColor: [Link],
foregroundColor: [Link],
padding: [Link](horizontal: 30, vertical: 15),
),
),
],
SizedBox(height: 20),
ElevatedButton(
onPressed: getLedStatus,
child: Text(')'تحديث الحالة,
),
],
),
),
);
}
}