Change parent widget model within child

Flutter tips Published on

Changing things in a parent widget from a child can sometimes feel complicated in Flutter. The Actions and Intent system gives us a neat and organized way to handle this.

If you are not using any state management solution, you might find yourself passing callbacks down the widget tree. This can lead to a lot of boilerplate code and make your widget tree hard to follow. This is especially true when you have a deep widget tree and need to pass callbacks through multiple layers of widgets. It can become messy and hard to maintain. In this article, we will explore a pattern that uses Actions and Intents to simplify this process. This pattern allows you to define clear actions that can be triggered from anywhere in the widget tree, making your code cleaner and easier to understand.

We will only use the built-in Flutter widgets and classes, so you don't need to worry about any third-party libraries. This pattern is part of the Flutter framework itself and is designed to work seamlessly with it. I found this pattern inside the official Flutter source code.

The Solution: Actions and Intent

This pattern involves creating simple objects to describe what you want to do (the Intent) and classes that know how to do it (the Action).

Understanding Intent

First, you define a simple class that extends Intent. Think of this as a message or a request. It holds the information you need to pass to the parent. In the example from the image, AppBarBuildIntent likely carries data related to building an AppBar.

Creating the Action

Next, you create a class that extends Action<YourIntentType>. This class is responsible for handling the specific Intent. It has an invoke method where you write the logic to perform the update on the parent's model or state. The example shows BartAppBarAction which updates a ValueNotifier using the data from the AppBarBuildIntent.

Setting up the Parent Widget

In the parent widget, you use the Actions widget. You wrap the part of your widget tree (including the child that will trigger the action) with Actions. You provide a map that links your Intent type to an instance of your Action class.

Calling the Action from the Child

From any child widget within the Actions scope, you can trigger the action. You simply call Actions.invoke(context, YourIntent(...)), passing a new instance of your Intent with the required data. Flutter finds the correct Action for that Intent and runs its invoke method.

Why use this approach?

Using Actions and Intents makes the communication flow from child to parent more explicit and testable. It provides a structured alternative to callbacks, especially when dealing with actions that might be triggered from various places deep within the widget tree.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is an Intent in this Flutter pattern?

An Intent is an object, usually a simple class extending `Intent`, that defines a request or carries data from a child to a parent widget.

What is an Action in this context?

An Action is a class that extends `Action<IntentType>`. It contains the logic (in its `invoke` method) for how to handle a specific Intent and perform an operation, like updating parent state.

How does a child widget trigger the action?

A child widget calls `Actions.invoke(context, YourIntent(...))` within the scope of an `Actions` widget in the parent tree.

Is this better than using callbacks?

Actions and Intents offer a more structured approach, especially for actions that need to be invoked from deep within the widget tree or when you want a clear separation of concerns between the request (Intent) and the handler (Action).

Read more
You may also be interested in
Draw text on a canvas  blog card image
Draw text on a canvas
Published on 2025-05-12T09:29:00.038Z
Flutter tips - How to chain animations  blog card image
Flutter tips - How to chain animations
Published on 2025-05-12T10:01:28.465Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved