Skip to content

Commit 6ae1735

Browse files
authored
Merge pull request Sub6Resources#1040 from vrtdev/bugfix/1033-video-size-crash
Don't crash when video ir iframe uses unsupported height/width
2 parents e0fb909 + 283c3af commit 6ae1735

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

packages/flutter_html_iframe/lib/iframe_mobile.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import 'package:webview_flutter/webview_flutter.dart';
77
CustomRender iframeRender({NavigationDelegate? navigationDelegate}) => CustomRender.widget(widget: (context, buildChildren) {
88
final sandboxMode = context.tree.element?.attributes["sandbox"];
99
final UniqueKey key = UniqueKey();
10+
final givenWidth = double.tryParse(context.tree.element?.attributes['width'] ?? "");
11+
final givenHeight = double.tryParse(context.tree.element?.attributes['height'] ?? "");
1012
return Container(
11-
width: double.tryParse(context.tree.element?.attributes['width'] ?? "")
12-
?? (double.tryParse(context.tree.element?.attributes['height'] ?? "") ?? 150) * 2,
13-
height: double.tryParse(context.tree.element?.attributes['height'] ?? "")
14-
?? (double.tryParse(context.tree.element?.attributes['width'] ?? "") ?? 300) / 2,
13+
width: givenWidth ?? (givenHeight ?? 150) * 2,
14+
height: givenHeight ?? (givenWidth ?? 300) / 2,
1515
child: ContainerSpan(
1616
style: context.style,
1717
newContext: context,

packages/flutter_html_iframe/lib/iframe_web.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import 'dart:html' as html;
1010
import 'package:webview_flutter/webview_flutter.dart';
1111

1212
CustomRender iframeRender({NavigationDelegate? navigationDelegate}) => CustomRender.widget(widget: (context, buildChildren) {
13+
final givenWidth = double.tryParse(context.tree.element?.attributes['width'] ?? "");
14+
final givenHeight = double.tryParse(context.tree.element?.attributes['height'] ?? "");
1315
final html.IFrameElement iframe = html.IFrameElement()
14-
..width = (double.tryParse(context.tree.element?.attributes['width'] ?? "")
15-
?? (double.tryParse(context.tree.element?.attributes['height'] ?? "") ?? 150) * 2).toString()
16-
..height = (double.tryParse(context.tree.element?.attributes['height'] ?? "")
17-
?? (double.tryParse(context.tree.element?.attributes['width'] ?? "") ?? 300) / 2).toString()
16+
..width = (givenWidth ?? (givenHeight ?? 150) * 2).toString()
17+
..height = (givenHeight ?? (givenWidth ?? 300) / 2).toString()
1818
..src = context.tree.element?.attributes['src']
1919
..style.border = 'none';
2020
final String createdViewId = getRandString(10);

packages/flutter_html_video/lib/flutter_html_video.dart

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ class VideoWidget extends StatefulWidget {
3030
}
3131

3232
class _VideoWidgetState extends State<VideoWidget> {
33-
ChewieController? chewieController;
34-
VideoPlayerController? videoController;
33+
ChewieController? _chewieController;
34+
VideoPlayerController? _videoController;
3535
double? _width;
3636
double? _height;
37-
late final List<String?> sources;
3837

3938
@override
4039
void initState() {
@@ -44,12 +43,14 @@ class _VideoWidgetState extends State<VideoWidget> {
4443
attributes['src'],
4544
...ReplacedElement.parseMediaSources(widget.context.tree.element!.children),
4645
];
46+
final givenWidth = double.tryParse(attributes['width'] ?? "");
47+
final givenHeight = double.tryParse(attributes['height'] ?? "");
4748
if (sources.isNotEmpty && sources.first != null) {
48-
_width = double.tryParse(attributes['width'] ?? (attributes['height'] ?? 150) * 2);
49-
_height = double.tryParse(attributes['height'] ?? (attributes['width'] ?? 300) / 2);
50-
videoController = VideoPlayerController.network(sources.first!);
51-
chewieController = ChewieController(
52-
videoPlayerController: videoController!,
49+
_width = givenWidth ?? (givenHeight ?? 150) * 2;
50+
_height = givenHeight ?? (givenWidth ?? 300) / 2;
51+
_videoController = VideoPlayerController.network(sources.first!);
52+
_chewieController = ChewieController(
53+
videoPlayerController: _videoController!,
5354
placeholder: attributes['poster'] != null && attributes['poster']!.isNotEmpty
5455
? Image.network(attributes['poster']!)
5556
: Container(color: Colors.black),
@@ -59,32 +60,29 @@ class _VideoWidgetState extends State<VideoWidget> {
5960
autoInitialize: true,
6061
aspectRatio: _width == null || _height == null ? null : _width! / _height!,
6162
);
62-
widget.callback?.call(widget.context.tree.element, chewieController!, videoController!);
63+
widget.callback?.call(widget.context.tree.element, _chewieController!, _videoController!);
6364
}
6465
super.initState();
6566
}
6667

6768
@override
6869
void dispose() {
69-
chewieController?.dispose();
70-
videoController?.dispose();
70+
_chewieController?.dispose();
71+
_videoController?.dispose();
7172
super.dispose();
7273
}
7374

7475
@override
7576
Widget build(BuildContext bContext) {
76-
if (sources.isEmpty || sources.first == null) {
77+
if (_chewieController == null) {
7778
return Container(height: 0, width: 0);
7879
}
7980
final child = Container(
8081
key: widget.context.key,
8182
child: Chewie(
82-
controller: chewieController!,
83+
controller: _chewieController!,
8384
),
8485
);
85-
if (_width == null || _height == null) {
86-
return child;
87-
}
8886
return AspectRatio(
8987
aspectRatio: _width! / _height!,
9088
child: child,

0 commit comments

Comments
 (0)