addSection method
Adds a section to the message.
A section is a top-level layout component that allows you to contextually associate text content with an optional accessory component (either a button or a thumbnail). Sections are useful for organizing related content with interactive or visual elements.
The section's builder should contain only text components. The optional accessory
can be either a button or a thumbnail, but not both.
Example:
// Section with text only
builder.addSection(
builder: MessageBuilder()
..addText('## Important Notice')
..addText('Please read this carefully.'),
);
// Section with text and button accessory
builder.addSection(
builder: MessageBuilder()
..addText('**New Feature Available**')
..addText('Click to learn more about our latest update.'),
button: MessageButton.link('https://example.com', label: 'Learn More'),
);
// Section with text and thumbnail accessory
builder.addSection(
builder: MessageBuilder()
..addText('## User Profile')
..addText('John Doe - Senior Developer'),
thumbnail: Thumbnail(
MediaItem.fromNetwork('https://example.com/avatar.png'),
),
);
Throws:
- FormatException if the section contains non-text components
- FormatException if both button and thumbnail are provided
See also:
- addContainer for general-purpose containers that support any component type
- Section for section configuration details
- Button for button types and options
- Thumbnail for thumbnail accessory options
Implementation
void addSection({
required MessageBuilder builder,
Button? button,
Thumbnail? thumbnail,
}) {
for (final component in builder._components) {
if (component is! TextDisplay) {
throw FormatException('Section components must be text only');
}
}
_components.add(Section(
builder: builder,
button: button,
thumbnail: thumbnail,
));
}