BlocPopBuilder<B extends StateStreamable<S>, S> class

A convenience widget that toggles the ability to pop the current route based on a BLoC/Cubit state.

BlocPopBuilder listens to a B extends StateStreamable<S> (e.g. a Bloc or Cubit) and evaluates blockWhen for every new state. If blockWhen returns true, the current route cannot be popped (i.e. back navigation is blocked). If it returns false, the route can be popped.

Internally it:

  • uses a BlocListener to observe state changes;
  • stores the computed canPop value in a ValueNotifier (via ValueNotifierWrapper);
  • exposes that value to a PopScope so the system back gesture / back button honors the computed canPop.

The child subtree is passed as the child parameter of ValueListenableBuilder so it doesn't rebuild when canPop changes; only the PopScope wrapper is rebuilt.

{@tool snippet}

Basic usage

Block back navigation while a form is saving:

BlocPopBuilder<MyBloc, MyState>(
  bloc: context.read<MyBloc>(),
  // Block when we're busy (e.g. saving / loading).
  blockWhen: (state) => state is MySavingState,
  child: const MyFormScreen(),
)

{@end-tool}

See also:

  • PopScope, which controls whether the route can pop.
  • BlocListener, which is used to react to BLoC/Cubit state changes.
  • ValueListenableBuilder, which rebuilds only the wrapper when canPop changes.
Inheritance

Constructors

BlocPopBuilder({required B bloc, required Widget child, required bool blockWhen(S state), Key? key})
Creates a BlocPopBuilder.
const

Properties

bloc → B
The BLoC/Cubit whose state drives the pop-blocking behavior.
final
blockWhen bool Function(S state)
A predicate that decides whether back navigation should be blocked for a given state.
final
child Widget
The subtree to display. It is not rebuilt when the canPop value changes.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

build(BuildContext context) Widget
Describes the part of the user interface represented by this widget.
override
createElement() StatelessElement
Creates a StatelessElement to manage this widget's location in the tree.
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited