Skip to content
Talk to an engineerRequest access

Heading

Six levels of heading, and the rule under an h1.

Three headings — “Release notes” as an h1 with a rule under it, then “What changed” and “Parser” — over a line of text

# to ###### start a heading; closing #s are optional. A line underlined with === or --- is a heading too (setext), which is why a --- directly under text makes a heading rather than a rule — leave a blank line before a horizontal rule.

Sizes come from GptMarkdownThemeData’s h1 to h6, which default to the Material text theme; HeadingStyle changes everything else.

GptMarkdown(
text,
styleSheet: const GptMarkdownStyleSheet(
heading: HeadingStyle(
textStyle: TextStyle(letterSpacing: -0.5),
padding: EdgeInsets.only(top: 8, bottom: 4),
showDivider: false,
),
),
)

textStyle is merged over the per-level style, so you change one property without restating the size. Per-level sizes still come from the theme:

GptMarkdownThemeData(
brightness: Brightness.light,
h1: Theme.of(context).textTheme.headlineMedium,
h2: Theme.of(context).textTheme.titleLarge,
)

showDivider: false removes the rule an h1 draws by default. Leave it null to keep following autoAddDividerLineAfterH1.

Restructure with a builder — for example, anchors on every heading:

GptMarkdown(
text,
headingBuilder: (context, level, content, style) => Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Flexible(child: content),
IconButton(icon: const Icon(Icons.link), onPressed: () {}),
],
),
)

level is 1–6, so one builder handles all six.

How a # heading is drawn.

Set it as GptMarkdownStyleSheet(heading: HeadingStyle(…)) on the widget, or in GptMarkdownThemeData for the whole app. Every field is optional; an unset one falls back to the theme, then to the default.

Field Type Description
textStyle TextStyle? Applied on top of the level style from the theme.
padding EdgeInsetsGeometry? Space around the heading. Defaults to none.
showDivider bool? Whether a rule is drawn under the heading.
dividerColor Color? Colour of that rule.
dividerThickness double? Thickness of that rule.
dividerPadding EdgeInsetsGeometry? Space around that rule.
Member Type Description
merge(HeadingStyle? other) HeadingStyle This style, with any unset field taken from other, field by field.
resolve(ColorScheme scheme) HeadingStyle This style with every remaining default filled in.
copyWith({…}) HeadingStyle A copy with the given fields replaced.
static lerp(HeadingStyle? a, HeadingStyle? b, double t) HeadingStyle? Linearly interpolates between two styles.
resolve: more

Heading defaults live on GptMarkdownThemeData as h1-h6, autoAddDividerLineAfterH1 and the hrLine* fields. Anything left null here keeps using those, so existing themes keep working unchanged.

Replaces the whole heading widget, including the rule an h1 draws.

Reach for styleSheet first — HeadingStyle covers the text style, padding and the rule without giving up the default structure.

typedef HeadingBuilder = Widget Function(
BuildContext context,
int level,
Widget content,
HeadingStyle style,
);