Skip to content

Commit

Permalink
Fix RenderSliver mode error when html input is empty (#1320) (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvixi authored Aug 22, 2024
1 parent 71d644a commit 8d5f959
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/core/lib/src/core_html_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ class HtmlWidgetState extends State<HtmlWidget> {
wf: _wf,
);

Widget get _sliverOrWidget0 => _sliverToBoxAdapterIfNeeded(widget0);

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -222,9 +224,13 @@ class HtmlWidgetState extends State<HtmlWidget> {
Future<bool> scrollToAnchor(String id) => _wf.onTapUrl('#$id');

Future<Widget> _buildAsync() async {
if (widget.html.isEmpty) {
return Future.sync(() => _sliverOrWidget0);
}

final domNodes = await compute(_parseHtml, widget.html);
if (!mounted) {
return widget0;
return _sliverOrWidget0;
}

Timeline.startSync('Build $widget (async)');
Expand All @@ -235,6 +241,10 @@ class HtmlWidgetState extends State<HtmlWidget> {
}

Widget _buildSync() {
if (widget.html.isEmpty) {
return _sliverOrWidget0;
}

Timeline.startSync('Build $widget (sync)');

Widget built;
Expand All @@ -253,7 +263,9 @@ class HtmlWidgetState extends State<HtmlWidget> {

Widget _sliverToBoxAdapterIfNeeded(Widget child) =>
widget.renderMode == RenderMode.sliverList
? SliverToBoxAdapter(child: child)
? child == widget0
? const SliverToBoxAdapter(child: widget0)
: SliverToBoxAdapter(child: child)
: child;

Widget _wrapper(Widget child) =>
Expand Down Expand Up @@ -302,7 +314,8 @@ Widget _buildBody(HtmlWidgetState state, dom.NodeList domNodes) {
final rootTree = state._rootTree;
rootTree.addBitsFromNodes(domNodes);

final built = rootTree.build()?.wrapWith(wf.buildBodyWidget) ?? widget0;
final built =
rootTree.build()?.wrapWith(wf.buildBodyWidget) ?? state._sliverOrWidget0;

_logger.fine('Built body successfuly.');

Expand Down
29 changes: 29 additions & 0 deletions packages/core/test/core_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,35 @@ void main() {
reason: '$explained has too many `CssBlock`s',
);
});

testWidgets('renders empty string', (WidgetTester tester) async {
// https://github.com/daohoangson/flutter_widget_from_html/issues/1320
final e = await explain(tester, RenderMode.sliverList, html: '');
expect(e, contains('└SizedBox.shrink'));
});

testWidgets('renders empty string (async)', (WidgetTester tester) async {
await explain(tester, RenderMode.sliverList, buildAsync: true, html: '');
await tester.runAsync(() => Future.delayed(const Duration(seconds: 1)));
await tester.pump();

final success = await helper.explainWithoutPumping(useExplainer: false);
expect(success, contains('└SizedBox.shrink'));
});

testWidgets('renders single space', (WidgetTester tester) async {
final e = await explain(tester, RenderMode.sliverList, html: ' ');
expect(e, contains('└SizedBox.shrink'));
});

testWidgets('renders single space (async)', (WidgetTester tester) async {
await explain(tester, RenderMode.sliverList, buildAsync: true, html: ' ');
await tester.runAsync(() => Future.delayed(const Duration(seconds: 1)));
await tester.pump();

final success = await helper.explainWithoutPumping(useExplainer: false);
expect(success, contains('└SizedBox.shrink'));
});
});

group('textStyle', () {
Expand Down

1 comment on commit 8d5f959

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.