Skip to content

Commit 17efea1

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 0e6afa3 + f3d2635 commit 17efea1

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ Once the above issue is resolved, the aforementioned compromises will go away. C
185185
| `style` | A powerful API that allows you to customize the style that should be used when rendering a specific HTMl tag. |
186186
| `navigationDelegateForIframe` | Allows you to set the `NavigationDelegate` for the `WebView`s of all the iframes rendered by the `Html` widget. |
187187
| `customImageRender` | A powerful API that allows you to fully customize how images are loaded. |
188+
| `selectionControls` | A custom text selection controls that allow you to override default toolbar and build toolbar with custom text selection options. See an [example](https://github.com/justinmc/flutter-text-selection-menu-examples/blob/master/lib/custom_menu_page.dart). |
188189

189190
### Getters:
190191

@@ -293,7 +294,7 @@ Widget html = Html(
293294
style: (context.tree.element!.attributes['horizontal'] != null)
294295
? FlutterLogoStyle.horizontal
295296
: FlutterLogoStyle.markOnly,
296-
textColor: context.style.color,
297+
textColor: context.style.color!,
297298
size: context.style.fontSize!.size! * 5,
298299
);
299300
},
@@ -803,14 +804,15 @@ Then, use the `customRender` parameter to add the widget to render Tex. It could
803804
Widget htmlWidget = Html(
804805
data: r"""<tex>i\hbar\frac{\partial}{\partial t}\Psi(\vec x,t) = -\frac{\hbar}{2m}\nabla^2\Psi(\vec x,t)+ V(\vec x)\Psi(\vec x,t)</tex>""",
805806
customRender: {
806-
"tex": (_, __, ___, element) => Math.tex(
807-
element.text,
807+
"tex": (RenderContext context, _) => Math.tex(
808+
context.tree.element!.text,
808809
onErrorFallback: (FlutterMathException e) {
809810
//return your error widget here e.g.
810811
return Text(e.message);
811812
},
812813
),
813-
}
814+
},
815+
tagsList: Html.tags..add('tex'),
814816
);
815817
```
816818

lib/flutter_html.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,28 +213,39 @@ class SelectableHtml extends StatelessWidget {
213213
214214
SelectableHtml({
215215
Key? key,
216+
GlobalKey? anchorKey,
216217
required this.data,
217218
this.onLinkTap,
218219
this.onAnchorTap,
219220
this.onCssParseError,
220221
this.shrinkWrap = false,
221222
this.style = const {},
222223
this.tagsList = const [],
224+
this.selectionControls
223225
}) : document = null,
226+
assert(data != null),
227+
_anchorKey = anchorKey ?? GlobalKey(),
224228
super(key: key);
225229

226230
SelectableHtml.fromDom({
227231
Key? key,
232+
GlobalKey? anchorKey,
228233
required this.document,
229234
this.onLinkTap,
230235
this.onAnchorTap,
231236
this.onCssParseError,
232237
this.shrinkWrap = false,
233238
this.style = const {},
234239
this.tagsList = const [],
240+
this.selectionControls
235241
}) : data = null,
242+
assert(document != null),
243+
_anchorKey = anchorKey ?? GlobalKey(),
236244
super(key: key);
237245

246+
/// A unique key for this Html widget to ensure uniqueness of anchors
247+
final GlobalKey _anchorKey;
248+
238249
/// The HTML data passed to the widget as a String
239250
final String? data;
240251

@@ -261,6 +272,10 @@ class SelectableHtml extends StatelessWidget {
261272
/// An API that allows you to override the default style for any HTML element
262273
final Map<String, Style> style;
263274

275+
/// Custom Selection controls allows you to override default toolbar and build custom toolbar
276+
/// options
277+
final TextSelectionControls? selectionControls;
278+
264279
static List<String> get tags => new List<String>.from(SELECTABLE_ELEMENTS);
265280

266281
@override
@@ -271,7 +286,7 @@ class SelectableHtml extends StatelessWidget {
271286
return Container(
272287
width: width,
273288
child: HtmlParser(
274-
key: null,
289+
key: _anchorKey,
275290
htmlData: doc,
276291
onLinkTap: onLinkTap,
277292
onAnchorTap: onAnchorTap,
@@ -286,6 +301,7 @@ class SelectableHtml extends StatelessWidget {
286301
imageRenders: defaultImageRenders,
287302
tagsList: tagsList.isEmpty ? SelectableHtml.tags : tagsList,
288303
navigationDelegateForIframe: null,
304+
selectionControls: selectionControls,
289305
),
290306
);
291307
}

lib/html_parser.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class HtmlParser extends StatelessWidget {
5858
final List<String> tagsList;
5959
final NavigationDelegate? navigationDelegateForIframe;
6060
final OnTap? _onAnchorTap;
61+
final TextSelectionControls? selectionControls;
6162

6263
HtmlParser({
6364
required this.key,
@@ -75,6 +76,7 @@ class HtmlParser extends StatelessWidget {
7576
required this.imageRenders,
7677
required this.tagsList,
7778
required this.navigationDelegateForIframe,
79+
this.selectionControls
7880
}) : this._onAnchorTap = onAnchorTap != null
7981
? onAnchorTap
8082
: key != null
@@ -125,6 +127,7 @@ class HtmlParser extends StatelessWidget {
125127
tree: cleanedTree,
126128
style: cleanedTree.style,
127129
),
130+
selectionControls: selectionControls,
128131
);
129132
}
130133
return StyledText(
@@ -1052,13 +1055,15 @@ class StyledText extends StatelessWidget {
10521055
final RenderContext renderContext;
10531056
final AnchorKey? key;
10541057
final bool _selectable;
1058+
final TextSelectionControls? selectionControls;
10551059

10561060
const StyledText({
10571061
required this.textSpan,
10581062
required this.style,
10591063
this.textScaleFactor = 1.0,
10601064
required this.renderContext,
10611065
this.key,
1066+
this.selectionControls,
10621067
}) : _selectable = false,
10631068
super(key: key);
10641069

@@ -1068,6 +1073,7 @@ class StyledText extends StatelessWidget {
10681073
this.textScaleFactor = 1.0,
10691074
required this.renderContext,
10701075
this.key,
1076+
this.selectionControls
10711077
}) : textSpan = textSpan,
10721078
_selectable = true,
10731079
super(key: key);
@@ -1082,6 +1088,7 @@ class StyledText extends StatelessWidget {
10821088
textDirection: style.direction,
10831089
textScaleFactor: textScaleFactor,
10841090
maxLines: style.maxLines,
1091+
selectionControls: selectionControls,
10851092
);
10861093
}
10871094
return SizedBox(

0 commit comments

Comments
 (0)