@@ -50,6 +50,33 @@ class _TableElement {
50
50
final List <TableRow > rows = < TableRow > [];
51
51
}
52
52
53
+ /// Holds configuration data for an image in a Markdown document.
54
+ class MarkdownImageConfig {
55
+ /// Creates a new [MarkdownImageConfig] instance.
56
+ MarkdownImageConfig ({
57
+ required this .uri,
58
+ this .title,
59
+ this .alt,
60
+ this .width,
61
+ this .height,
62
+ });
63
+
64
+ /// The URI of the image.
65
+ final Uri uri;
66
+
67
+ /// The title of the image, displayed on hover.
68
+ final String ? title;
69
+
70
+ /// The alternative text for the image, displayed if the image cannot be loaded.
71
+ final String ? alt;
72
+
73
+ /// The desired width of the image.
74
+ final double ? width;
75
+
76
+ /// The desired height of the image.
77
+ final double ? height;
78
+ }
79
+
53
80
/// A collection of widgets that should be placed adjacent to (inline with)
54
81
/// other inline elements in the same parent block.
55
82
///
@@ -105,7 +132,8 @@ class MarkdownBuilder implements md.NodeVisitor {
105
132
required this .selectable,
106
133
required this .styleSheet,
107
134
required this .imageDirectory,
108
- required this .imageBuilder,
135
+ @Deprecated ('Use sizedImageBuilder instead' ) this .imageBuilder,
136
+ required this .sizedImageBuilder,
109
137
required this .checkboxBuilder,
110
138
required this .bulletBuilder,
111
139
required this .builders,
@@ -115,7 +143,8 @@ class MarkdownBuilder implements md.NodeVisitor {
115
143
this .onSelectionChanged,
116
144
this .onTapText,
117
145
this .softLineBreak = false ,
118
- });
146
+ }) : assert (imageBuilder == null || sizedImageBuilder == null ,
147
+ 'Only one of imageBuilder or sizedImageBuilder may be specified.' );
119
148
120
149
/// A delegate that controls how link and `pre` elements behave.
121
150
final MarkdownBuilderDelegate delegate;
@@ -131,9 +160,41 @@ class MarkdownBuilder implements md.NodeVisitor {
131
160
/// The base directory holding images referenced by Img tags with local or network file paths.
132
161
final String ? imageDirectory;
133
162
134
- /// Call when build an image widget.
163
+ /// {@template flutter_markdown.builder.MarkdownBuilder.imageBuilder}
164
+ /// Called to build an image widget.
165
+ ///
166
+ /// This builder allows for custom rendering of images within the Markdown content.
167
+ /// It provides the image `Uri` , `title` , and `alt` text.
168
+ ///
169
+ /// **Deprecated:** Use [sizedImageBuilder] instead, which offers more comprehensive
170
+ /// image information.
171
+ ///
172
+ /// Only one of [imageBuilder] or [sizedImageBuilder] may be specified.
173
+ ///
174
+ /// {@endtemplate}
175
+ @Deprecated ('Use sizedImageBuilder instead' )
135
176
final MarkdownImageBuilder ? imageBuilder;
136
177
178
+ /// {@template flutter_markdown.builder.MarkdownBuilder.sizedImageBuilder}
179
+ /// Called to build an image widget with size information.
180
+ ///
181
+ /// This builder allows for custom rendering of images within the Markdown content
182
+ /// when size information is available. It provides a [MarkdownImageConfig]
183
+ /// containing the `Uri` , `title` , `alt` , `width` , and `height` of the image.
184
+ ///
185
+ /// If both [imageBuilder] and [sizedImageBuilder] are `null` , a default image builder
186
+ /// will be used.
187
+ /// when size information is available. It provides a [MarkdownImageConfig]
188
+ /// containing the `Uri` , `title` , `alt` , `width` , and `height` of the image.
189
+ ///
190
+ /// If both [imageBuilder] and [sizedImageBuilder] are `null` , a default
191
+ /// image builder will be used.
192
+ ///
193
+ /// Only one of [imageBuilder] or [sizedImageBuilder] may be specified.
194
+ ///
195
+ /// {@endtemplate}
196
+ final MarkdownSizedImageBuilder ? sizedImageBuilder;
197
+
137
198
/// Call when build a checkbox widget.
138
199
final MarkdownCheckboxBuilder ? checkboxBuilder;
139
200
@@ -619,8 +680,12 @@ class MarkdownBuilder implements md.NodeVisitor {
619
680
}
620
681
621
682
Widget child;
622
- if (imageBuilder != null ) {
623
- child = imageBuilder !(uri, title, alt);
683
+ if (sizedImageBuilder != null ) {
684
+ final MarkdownImageConfig config = MarkdownImageConfig (
685
+ uri: uri, alt: alt, title: title, height: height, width: width);
686
+ child = sizedImageBuilder !(config);
687
+ } else if (imageBuilder != null ) {
688
+ child = imageBuilder !(uri, alt, title);
624
689
} else {
625
690
child = kDefaultImageBuilder (uri, imageDirectory, width, height);
626
691
}
0 commit comments