Skip to content

Scope

Shares a value with the widgets below it.

Learn more: State management

Import: package:fleury/fleury.dart, or package:fleury/fleury_core.dart in browser code.

// Share a value owned elsewhere:
Scope(project, child: const ProjectPath())
// Create and own a model for this subtree:
Scope.create(Cart.new, child: const ShopScreen())
// Read the nearest one in a descendant's build (it rebuilds on change):
final cart = context.scope<Cart>();

Provide a value once with Scope(model, child: ...), then read the nearest scope with context.scope<Model>() or a ScopeBuilder<Model> — the same read, as a call or as a widget. Widgets between the provider and its readers need not pass the value along. The declared type is the lookup key; an inner scope of that type overrides an outer one.

Readers rebuild when an ordinary value changes according to ==, when a Listenable is replaced by a different instance, or when it notifies. Mutable models extend Notifier and call Notifier.notify after a change. A scope listens on its readers’ behalf, so they need no additional builder.

Scope.create creates and owns a value. Scope.createWithContext also supplies the scope’s own context to a factory that needs ancestor scopes. A reader stays subscribed until it leaves the tree; see BuildContext.scope for where a scope can be read. A value needed in dispose should be saved earlier, before the element leaves the tree.

Shares value without taking ownership or disposing it.

ParameterTypeDefaultDescription
valueTrequiredThe value exposed to descendant readers under the declared type T.
child:WidgetrequiredThe subtree that can read this value.

Creates a value once when this scope mounts and owns its lifetime.

Rebuilding retains the value and its original dispose callback. A Notifier is disposed automatically unless a custom callback is supplied. Other resources can be cleaned up with dispose. Children unmount first. Switching from a supplied value to an owned scope also runs create.

ParameterTypeDefaultDescription
createT Function()requiredCreates the value when this scope begins owning it.
dispose:void Function(T value)?—Optional cleanup, replacing automatic disposal for a Notifier.
child:WidgetrequiredThe subtree that can read this value.

Creates an owned value with access to ancestor scopes.

create receives this scope’s own context and may use context.scope to read ancestors. Its lifetime and disposal follow Scope.create.

ParameterTypeDefaultDescription
createT Function(BuildContext context)requiredCreates the value with this scope’s context for ancestor lookups.
dispose:void Function(T value)?—Optional cleanup, replacing automatic disposal for a Notifier.
child:WidgetrequiredThe subtree that can read this value.

Scope is defined in packages/fleury/lib/src/widgets/framework.dart.

Category: State & async · All widgets