Skip to content

feat(portable_text): expose block index to BlockContainerBuilder - #58

Open
Ortes wants to merge 1 commit into
vyuh-tech:mainfrom
Fullphysio:feat/portable-text-block-index
Open

feat(portable_text): expose block index to BlockContainerBuilder#58
Ortes wants to merge 1 commit into
vyuh-tech:mainfrom
Fullphysio:feat/portable-text-block-index

Conversation

@Ortes

@Ortes Ortes commented Jun 10, 2026

Copy link
Copy Markdown

What

Threads the 0-based block index through buildBlock and the default block builder into BlockContainerBuilder and BlockWidgetBuilder.

Why

Block containers currently receive only (context, child), so they have no way to know a block's position in the list. That makes position-dependent styling impossible — e.g. suppressing the leading space above the first heading while keeping it for headings in the middle of the content.

With the index available, a container can do:

PortableTextConfig.shared.blockContainers['h2'] = (context, child, index) =>
    index == 0 ? child : Padding(padding: const EdgeInsets.only(top: 24), child: child);

Changes

  • BlockContainerBuilder: Function(BuildContext, Widget)Function(BuildContext, Widget, int index)
  • BlockWidgetBuilder: adds a trailing int index
  • PortableTextConfig.buildBlock: takes and forwards index
  • PortableTextBlock: new index field (defaults to 0 for standalone use), forwarded to the container builder
  • defaultListBuilder passes the ListView.builder index through
  • Defaults (defaultBlockContainerBuilder, blockquote, block) updated to the new arity
  • README example and tests updated

Breaking change

BlockContainerBuilder and BlockWidgetBuilder now take a trailing int index argument. Existing custom builders need the extra parameter (ignore it if unused).

All package tests pass; dart analyze is clean.

Thread the 0-based block index through buildBlock and the default
'block' builder into BlockContainerBuilder and BlockWidgetBuilder, so
containers can vary by position — e.g. suppressing leading space on the
first block.

Breaking change: BlockContainerBuilder and BlockWidgetBuilder now take a
trailing int index argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant