Sobes.tech
Middle

InheritedWidget nədir və Flutter-də necə istifadə olunur?

sobes.tech Süni İntellekt

AI-dan cavab

InheritedWidget Flutter-da, widget ağacında məlumatların səmərəli şəkildə aşağıya yayılmasına imkan verən xüsusi bir widget növüdür. Bu, hər bir aralıq widget-ın konstruktivləri vasitəsilə onları açıq şəkildə ötürməyə ehtiyac olmadan, məlumatlar və xidmətləri widget ağacına təqdim etmək üçün istifadə olunur.

InheritedWidget-in əsas xüsusiyyətləri:

  • Effektiv məlumat yayımı: InheritedWidget dəyişdikdə, Flutter avtomatik olaraq ona bağlı olan widget-ları yenidən qurur.
  • Məlumatlara asan giriş: Uşaq widget-lar of() statik metodu vasitəsilə InheritedWidget-dən məlumatlara daxil ola bilər.
  • Asılılıqların idarə olunması: updateShouldNotify metodu, hansı zaman asılı widget-ların yenidən qurulmalı olduğunu idarə etməyə imkan verir.

InheritedWidget necə istifadə olunur:

  1. InheritedWidget-dən miras alan sinfi yaradın:

    class MyInheritedWidget extends InheritedWidget {
      const MyInheritedWidget({
        Key? key,
        required this.data,
        required Widget child,
      }) : super(key: key, child: child);
    
      final String data; // Yayım üçün məlumatlar
    
      // InheritedWidget nümunəsinə çıxış metodu
      static MyInheritedWidget of(BuildContext context) {
        final MyInheritedWidget? result = context.dependOnInheritedWidgetOfExactType<MyInheritedWidget>();
        assert(result != null, 'Context-də MyInheritedWidget tapılmadı');
        return result!;
      }
    
      // Asılı widget-ların dəyişikliklər barədə məlumatlandırılması
      @override
      bool updateShouldNotify(MyInheritedWidget oldWidget) {
        return data != oldWidget.data; // Məlumatlar dəyişərsə, bildiriş ver
      }
    }
    
  2. InheritedWidget-i widget ağacına yerləşdirin:

    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MyInheritedWidget( // Widget ağacını əhatə edir
          data: 'Salam InheritedWidget-dən',
          child: MaterialApp(
            title: 'Demo InheritedWidget',
            home: Scaffold(
              appBar: AppBar(
                title: const Text('Demo InheritedWidget'),
              ),
              body: const Center(
                child: MyDependentWidget(), // Məlumatlardan istifadə edən widget
              ),
            ),
          ),
        );
      }
    }
    
  3. Uşaq widget-dan məlumatlara daxil olun:

    class MyDependentWidget extends StatelessWidget {
      const MyDependentWidget({super.key});
    
      @override
      Widget build(BuildContext context) {
        // of() metodundan istifadə etməklə məlumatlara daxil olun
        final String receivedData = MyInheritedWidget.of(context).data;
    
        return Text(
          receivedData,
          style: Theme.of(context).textTheme.headlineMedium,
        );
      }
    }
    

InheritedWidget Flutter-da çoxsaylı vəziyyət idarəetmə həllərinin əsasını təşkil edir, məsələn Provider, Bloc. O, məlumatların widget ağacında effektiv və deklarativ şəkildə mübadiləsini təmin edir.