Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure each code block specified in the markdown uses its own ScrollController. #6904

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/flutter_markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.7.2

* Multiple code blocks within a single markdown will now use separate ScrollControllers.

## 0.7.1

* Allows for choosing a custom font feature to create superscript in footnotes when the font does not support the `supr` font feature.
Expand Down
6 changes: 3 additions & 3 deletions packages/flutter_markdown/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ class MarkdownBuilder implements md.NodeVisitor {
final List<_TableElement> _tables = <_TableElement>[];
final List<_InlineElement> _inlines = <_InlineElement>[];
final List<GestureRecognizer> _linkHandlers = <GestureRecognizer>[];
final ScrollController _preScrollController = ScrollController();
String? _currentBlockTag;
String? _lastVisitedTag;
bool _isInBlockquote = false;
Expand Down Expand Up @@ -347,10 +346,11 @@ class MarkdownBuilder implements md.NodeVisitor {
child = builders[_blocks.last.tag!]!
.visitText(text, styleSheet.styles[_blocks.last.tag!]);
} else if (_blocks.last.tag == 'pre') {
final ScrollController preScrollController = ScrollController();
child = Scrollbar(
controller: _preScrollController,
controller: preScrollController,
child: SingleChildScrollView(
controller: _preScrollController,
controller: preScrollController,
scrollDirection: Axis.horizontal,
padding: styleSheet.codeblockPadding,
child: _buildRichText(delegate.formatText(styleSheet, text.text)),
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_markdown/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
formatted with simple Markdown tags.
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
version: 0.7.1
version: 0.7.2

environment:
sdk: ^3.3.0
Expand Down
28 changes: 28 additions & 0 deletions packages/flutter_markdown/test/scrollable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ void defineTests() {
},
);

testWidgets(
'two code blocks use different scroll controllers',
(WidgetTester tester) async {
const String data =
"```\nvoid main() {\n print('Hello World!');\n}\n```"
'\n'
"```\nvoid main() {\n print('Hello World!');\n}\n```";

await tester.pumpWidget(
boilerplate(
const MediaQuery(
data: MediaQueryData(),
child: MarkdownBody(data: data),
),
),
);

final Iterable<Widget> widgets = tester.allWidgets;
final Iterable<SingleChildScrollView> scrollViews =
widgets.whereType<SingleChildScrollView>();
expect(scrollViews, hasLength(2));
expect(scrollViews.first.controller, isNotNull);
expect(scrollViews.last.controller, isNotNull);
expect(scrollViews.first.controller,
isNot(equals(scrollViews.last.controller)));
},
);

testWidgets(
'controller',
(WidgetTester tester) async {
Expand Down