Skip to content

Add CSS rules to style custom example containers #4243

Description

@dkwingsmt

Background

The Flutter team is decoupling the material and cupertino libraries into independent packages. As part of this transition, their API documentation will use standard pub.dev generation processes. This decoupling work is scheduled to complete around June 2026.

While we want to use standard generation processes where possible, some visual elements are necessary to maintain the usability of the documentation.

The need for a styled example container

Flutter API documentation has historically enclosed code examples inside a blue container. We evaluated this design and concluded that this container is important for readability. This makes the container a blocking requirement for our decoupling plans.

We explored alternative implementations, such as using blockquote highlights. However, the visual distinction is insufficient, and the @example directive does not parse correctly inside a blockquote.

To resolve this, we propose adding support for a custom <example> HTML tag.

Proposed implementation

Because markdown supports raw HTML tags, this approach does not require complex changes to the dartdoc parser. It only requires adding CSS rules to dartdoc to style the <example> tag as a styled container.

We understand that adding styles specifically for Flutter can increase the maintenance burden of dartdoc. We propose this CSS-only approach because it is self-contained and minimizes changes to the core codebase.

Below is the CSS we have tested and verified:

  example {
    padding: 10px;
    overflow: auto;
    display: block;
    unicode-bidi: isolate;
    margin: 1em 0;
  }

  example p {
    margin: 8px 40px 12px 8px;
  }

  example pre {
    margin: 0;
  }

  example code.hljs {
    max-height: 500px;
  }

  .light-theme example {
    background-color: rgb(215, 235, 252);
  }

  .dark-theme example {
    background-color: rgb(30, 40, 51);
  }

Below is how API doc will look like in practice:

///
/// <example>
///
/// This example shows a toggleable [CupertinoSwitch]. When the thumb slides to
/// the other side of the track, the switch is toggled between on/off.
///
/// {@example /examples/api/switch/cupertino_switch.0.dart}
///
/// </example>
///
Image

We would appreciate your feedback on this approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions