InheritedWidget in Flutter

Suren
3 min readMar 21, 2023

--

InheritedWidget is a fundamental widget in the Flutter framework that allows you to share data down the widget tree. It is a simple yet powerful tool that enables you to manage the state of your application efficiently. In this article, we will explore how InheritedWidget works and how it can be used in your Flutter applications.

What is InheritedWidget?

InheritedWidget is a widget that allows you to pass data down the widget tree. It is a special widget that is designed to hold data that can be shared by all the widgets below it in the widget tree. InheritedWidget is an immutable widget, which means that once it is created, it cannot be changed.

How does InheritedWidget work?

InheritedWidget works by creating a widget that holds data and passing it down the widget tree. The data can be any object that you want to share, such as a list of items, a user profile, or a theme. When a widget needs to access the data, it calls the InheritedWidget's ancestorInheritedElementForWidgetOfExactType method, which returns the InheritedWidget's element.

Once the element is retrieved, the widget can access the data by calling the InheritedWidget's getter method. If the data changes, the InheritedWidget's notifyListeners method can be called, which triggers a rebuild of all the widgets that are dependent on the data.

How to use InheritedWidget in your Flutter application?

To use InheritedWidget in your Flutter application, you need to create a new class that extends InheritedWidget. This class will hold the data that you want to share across the widget tree. Here is an example of how to create an InheritedWidget class:

class MyData extends InheritedWidget {
final int count;

MyData({
Key? key,
required Widget child,
required this.count,
}) : super(key: key, child: child);

@override
bool updateShouldNotify(MyData oldWidget) {
return count != oldWidget.count;
}

static MyData of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<MyData>()!;
}
}

In this example, we create a new class called MyData that extends InheritedWidget. We pass the data that we want to share in the constructor and call the super method with the key and child arguments.

The updateShouldNotify method is called whenever the data changes. In this example, we compare the current count value with the old count value and return true if they are different.

The of method is a helper method that retrieves the MyData instance from the widget tree. It is used by the widgets that need to access the shared data.

To use the MyData class, we wrap the widget tree with it, like this:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MyData(
count: 0,
child: MaterialApp(
title: 'My App',
home: MyHomePage(),
),
);
}
}

In this example, we wrap the MaterialApp widget with the MyData widget and pass the initial count value of 0.

To access the shared data in a widget, we call the static of method:

class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final myData = MyData.of(context);
return Scaffold(
appBar: AppBar(
title: Text('My Home Page'),
),
body: Center(
child: Text('Count: ${myData.count}'),
),
);
}
}

In this example, we retrieve the MyData instance from the context and display the count value in the Text widget.

Conclusion

InheritedWidget is a powerful tool in Flutter that allows you to share data down the widget tree. It provides a way to manage the state of your application efficiently and can be used to pass any object down the widget tree.

In this article, we explored how InheritedWidget works, how to create an InheritedWidget class, and how to use it in your Flutter application. I hope this article has given you a good understanding of InheritedWidget and how to use it to manage state in your Flutter applications. Happy coding!

Stay connected for more!

The more you share… more you learn… follow for more….

--

--

Suren

Software Developer with 2 of experience. Sharing insights, knowledge, and expertise through my blog. #Java #Flutter #js #ContinuousLearning #Collaboration