Auto play video only when loaded directly

This commit is contained in:
Vishnu Mohandas 2020-06-21 23:14:21 +05:30
parent e040de1d55
commit 985fd2f38a
2 changed files with 9 additions and 3 deletions

View file

@ -30,6 +30,7 @@ class _DetailPageState extends State<DetailPage> {
List<File> _files;
PageController _pageController;
int _selectedIndex = 0;
bool _hasPageChanged = false;
@override
void initState() {
@ -75,7 +76,10 @@ class _DetailPageState extends State<DetailPage> {
},
);
} else if (file.fileType == FileType.video) {
content = VideoWidget(file);
content = VideoWidget(
file,
autoPlay: !_hasPageChanged, // Autoplay if it was opened directly
);
} else {
content = Icon(Icons.error);
}
@ -85,6 +89,7 @@ class _DetailPageState extends State<DetailPage> {
onPageChanged: (index) {
setState(() {
_selectedIndex = index;
_hasPageChanged = true;
});
_preloadFiles(index);
},

View file

@ -9,7 +9,8 @@ import 'loading_widget.dart';
class VideoWidget extends StatefulWidget {
final File file;
VideoWidget(this.file, {Key key}) : super(key: key);
final bool autoPlay;
VideoWidget(this.file, {this.autoPlay = false, Key key}) : super(key: key);
@override
_VideoWidgetState createState() => _VideoWidgetState();
@ -59,7 +60,7 @@ class _VideoWidgetState extends State<VideoWidget> {
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController,
aspectRatio: _videoPlayerController.value.aspectRatio,
autoPlay: true,
autoPlay: widget.autoPlay,
autoInitialize: true,
looping: true,
allowFullScreen: false,