Skip to content

Commit 3140820

Browse files
authored
Ensure each code block specified in the markdown uses its own ScrollController. (flutter#6904)
Currently all code blocks share the same ScrollController causing an exception when they appear on screen together. This fixes that situation.
1 parent 61ed839 commit 3140820

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

packages/flutter_markdown/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.7.2
2+
3+
* Multiple code blocks within a single markdown will now use separate ScrollControllers.
4+
15
## 0.7.1
26

37
* Allows for choosing a custom font feature to create superscript in footnotes when the font does not support the `supr` font feature.

packages/flutter_markdown/lib/src/builder.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ class MarkdownBuilder implements md.NodeVisitor {
174174
final List<_TableElement> _tables = <_TableElement>[];
175175
final List<_InlineElement> _inlines = <_InlineElement>[];
176176
final List<GestureRecognizer> _linkHandlers = <GestureRecognizer>[];
177-
final ScrollController _preScrollController = ScrollController();
178177
String? _currentBlockTag;
179178
String? _lastVisitedTag;
180179
bool _isInBlockquote = false;
@@ -347,10 +346,11 @@ class MarkdownBuilder implements md.NodeVisitor {
347346
child = builders[_blocks.last.tag!]!
348347
.visitText(text, styleSheet.styles[_blocks.last.tag!]);
349348
} else if (_blocks.last.tag == 'pre') {
349+
final ScrollController preScrollController = ScrollController();
350350
child = Scrollbar(
351-
controller: _preScrollController,
351+
controller: preScrollController,
352352
child: SingleChildScrollView(
353-
controller: _preScrollController,
353+
controller: preScrollController,
354354
scrollDirection: Axis.horizontal,
355355
padding: styleSheet.codeblockPadding,
356356
child: _buildRichText(delegate.formatText(styleSheet, text.text)),

packages/flutter_markdown/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
44
formatted with simple Markdown tags.
55
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
7-
version: 0.7.1
7+
version: 0.7.2
88

99
environment:
1010
sdk: ^3.3.0

packages/flutter_markdown/test/scrollable_test.dart

+28
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,34 @@ void defineTests() {
3434
},
3535
);
3636

37+
testWidgets(
38+
'two code blocks use different scroll controllers',
39+
(WidgetTester tester) async {
40+
const String data =
41+
"```\nvoid main() {\n print('Hello World!');\n}\n```"
42+
'\n'
43+
"```\nvoid main() {\n print('Hello World!');\n}\n```";
44+
45+
await tester.pumpWidget(
46+
boilerplate(
47+
const MediaQuery(
48+
data: MediaQueryData(),
49+
child: MarkdownBody(data: data),
50+
),
51+
),
52+
);
53+
54+
final Iterable<Widget> widgets = tester.allWidgets;
55+
final Iterable<SingleChildScrollView> scrollViews =
56+
widgets.whereType<SingleChildScrollView>();
57+
expect(scrollViews, hasLength(2));
58+
expect(scrollViews.first.controller, isNotNull);
59+
expect(scrollViews.last.controller, isNotNull);
60+
expect(scrollViews.first.controller,
61+
isNot(equals(scrollViews.last.controller)));
62+
},
63+
);
64+
3765
testWidgets(
3866
'controller',
3967
(WidgetTester tester) async {

0 commit comments

Comments
 (0)