Skip to content

A simple standard counter application using InheritedWidget to simplify passing data to dependent descendant widgets.

Notifications You must be signed in to change notification settings

Alok-Sci/flutter--inherited-widget-counter-example

Repository files navigation

Flutter Counter App Using InheritedWidget

This is a simple Flutter counter application demonstrating state management using a custom implementation of InheritedWidget. The app consists of a button that increments a count displayed on the screen.

Overview

This project showcases how to manage and propagate state efficiently in Flutter by creating a custom InheritedWidget named Counter. The counter value changes when the button is pressed, and the updated value is reflected across the widget tree.

Unlike typical mutable state management solutions, this approach embraces Flutter’s built-in mechanism for efficient widget rebuilds using immutable inherited widgets.

Key Features

  • Uses InheritedWidget to propagate state down the widget tree.
  • Demonstrates how to rebuild widgets depending on InheritedWidget when the state changes.
  • Basic UI with a button and a text widget showing the counter value.
  • Teaches the importance of immutability and rebuilding the InheritedWidget on state changes.

How to Run

  1. Clone or download this repository.
  2. Open the project in your preferred IDE (e.g., VSCode, Android Studio).
  3. Run flutter pub get to install dependencies.
  4. Start the app on an emulator or physical device using flutter run.

Understanding the Code

This project is a great starting point for grasping Flutter’s state management fundamentals through InheritedWidget. For a more detailed and thorough explanation of how InheritedWidget works to power state management in Flutter (including its limitations and how to manage rebuilds properly), please read the following Medium blog post:

How InheritedWidget powers Flutter’s state management

The blog covers:

  • The philosophy behind InheritedWidget and why it must be immutable.
  • How Flutter decides when to rebuild widgets based on dependencies.
  • Common pitfalls when using InheritedWidget directly.
  • How advanced state management solutions (like Provider) build on top of InheritedWidget.

Feel free to open issues or contribute enhancements!

About

A simple standard counter application using InheritedWidget to simplify passing data to dependent descendant widgets.

Topics

Resources

Stars

Watchers

Forks