Created
January 13, 2021 00:11
-
-
Save jtmuller5/9725d0cf5d67eb0081da6962916c9e5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// (1) Use the WidgetsBindingObserver to get the VideoThumbnail's size | |
/// after it is laid out | |
class VideoThumbnail extends ViewModelWidget<StackedVideoViewModel> with WidgetsBindingObserver{ | |
@override | |
Widget build(BuildContext context, StackedVideoViewModel model) { | |
/// (3) Get the size of the widget after it is rendered on screen | |
WidgetsBinding.instance | |
.addPostFrameCallback((_) { | |
if(!model.gotThumbnailSize)model.getVideoSize(); | |
}); | |
return Builder( | |
builder: (context) { | |
// If we want to show the full video, we need to scale it to fit the longest side | |
if (model.showFull) { | |
bool wideVideo = | |
model.videoPlayerController.value.size.width > | |
model.videoPlayerController.value.size.height; | |
if (wideVideo) { | |
return Row( | |
/// (2) Mark the final video thumbnail with a GlobalObject Key | |
key: model.thumbnailKey, | |
children: [ | |
Expanded( | |
child: AspectRatio( | |
aspectRatio: | |
model.videoPlayerController.value.aspectRatio, | |
child: VideoPlayer(model.videoPlayerController), | |
)) | |
], | |
); | |
} else { | |
return Column( | |
/// (2) Mark the final video thumbnail with a GlobalObject Key | |
key: model.thumbnailKey, | |
children: [ | |
Expanded( | |
child: AspectRatio( | |
aspectRatio: | |
model.videoPlayerController.value.aspectRatio, | |
child: VideoPlayer(model.videoPlayerController), | |
)) | |
], | |
); | |
} | |
} | |
// Else just show a portion of the video viewport | |
else { | |
return FittedBox( | |
/// (2) Mark the final video thumbnail with a GlobalObject Key | |
key: model.thumbnailKey, | |
fit: BoxFit.cover, | |
alignment: Alignment(model.x, model.y), | |
child: SizedBox( | |
height: | |
model.videoPlayerController.value.size?.height ?? 0, | |
width: model.videoPlayerController.value.size?.width ?? 0, | |
child: VideoPlayer(model.videoPlayerController), | |
), | |
); | |
} | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment