إدارة الحالة تشير إلى كيفية إدارة التطبيق وتحديثه البيانات (الحالة) وعكس التغييرات في واجهة المستخدم. مع نمو التطبيقات، تصبح إدارة الحالة بشكل جيد مهمة، و Flutter توفر آليات مدمجة (setState) وعديد من المكتبات (Provider و Riverpod و Bloc) لها.
ما هي الحالة
STATE = data that can change and affects the UI:
→ UI state: is a checkbox checked? what's in a text field? is a menu open?
→ app state: the logged-in user, items in a cart, fetched data
→ When state changes, the UI must UPDATE to reflect it.
الحالة المحلية: setState
// for state local to a single widget → StatefulWidget + setState (built-in, simple)
setState(() {
count++; // change state → triggers a rebuild → UI updates
});
// → good for simple, LOCAL state within one widget
