Skip to content
Open
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
11 changes: 10 additions & 1 deletion lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,16 @@ class MarkdownBuilder implements md.NodeVisitor {
child: child,
);
} else if (tag == 'hr') {
child = Container(decoration: styleSheet.horizontalRuleDecoration);
if (!builders.containsKey(tag)) {
child = Container(decoration: styleSheet.horizontalRuleDecoration);
}
}

if (tag == 'hr' && paddingBuilders.containsKey(tag)) {
child = Padding(
padding: paddingBuilders[tag]!.getPadding(),
child: child,
);
}

_addBlockChild(child);
Expand Down
66 changes: 65 additions & 1 deletion test/horizontal_rule_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:markdown/markdown.dart' as md;
import 'utils.dart';

void main() => defineTests();
Expand Down Expand Up @@ -84,5 +85,68 @@ void defineTests() {
]);
},
);
testWidgets(
'custom builder for hr is respected',
(WidgetTester tester) async {
const String data = '---';
await tester.pumpWidget(
boilerplate(
MarkdownBody(
data: data,
builders: <String, MarkdownElementBuilder>{
'hr': _CustomHrBuilder(),
},
),
),
);

expect(find.byType(ColoredBox), findsOneWidget);
final ColoredBox box = tester.widget(find.byType(ColoredBox));
expect(box.color, Colors.red);
},
);

testWidgets(
'paddingBuilders for hr is applied',
(WidgetTester tester) async {
const String data = '---';
const double paddingX = 16.0;
await tester.pumpWidget(
boilerplate(
MarkdownBody(
data: data,
paddingBuilders: <String, MarkdownPaddingBuilder>{
'hr': _CustomPaddingBuilder(paddingX),
},
),
),
);

final Finder paddingFinder = find.byType(Padding);
final List<Padding> paddings = tester.widgetList<Padding>(paddingFinder).toList();
final bool hasHrPadding = paddings.any(
(Padding p) => p.padding.along(Axis.horizontal) == paddingX * 2,
);
expect(hasHrPadding, isTrue);
},
);
});
}

class _CustomHrBuilder extends MarkdownElementBuilder {
@override
Widget visitElementAfter(md.Element element, TextStyle? preferredStyle) {
return const ColoredBox(color: Colors.red, child: SizedBox(height: 2, width: double.infinity));
}
}

class _CustomPaddingBuilder extends MarkdownPaddingBuilder {
_CustomPaddingBuilder(this.paddingX);

final double paddingX;

@override
EdgeInsets getPadding() {
return EdgeInsets.symmetric(horizontal: paddingX);
}
}