Created
April 10, 2025 21:39
-
-
Save prof3ssorSt3v3/aef6d4a580175e66bb0966b7aedec833 to your computer and use it in GitHub Desktop.
orientation and key for video
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
import 'package:video_player/video_player.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed( | |
seedColor: Colors.blue, | |
), | |
), | |
home: const HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({super.key}); | |
@override | |
State<HomePage> createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
late VideoPlayerController _controller; | |
UniqueKey videoKey = UniqueKey(); | |
@override | |
void initState() { | |
super.initState(); | |
_controller = VideoPlayerController.networkUrl( | |
Uri.parse( | |
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4', | |
), | |
) | |
..initialize().then((_) { | |
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed. | |
setState(() {}); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: | |
Theme.of(context).colorScheme.inversePrimary, | |
title: Text('Home Page'), | |
), | |
body: OrientationBuilder( | |
builder: (context, orientation) { | |
return Center( | |
child: | |
orientation == Orientation.portrait | |
? _controller.value.isInitialized | |
? AspectRatio( | |
aspectRatio: | |
_controller.value.aspectRatio, | |
child: VideoPlayer( | |
_controller, | |
key: videoKey, | |
), | |
) | |
: Text('not initialized') | |
: _controller.value.isInitialized | |
? AspectRatio( | |
aspectRatio: _controller.value.aspectRatio, | |
child: VideoPlayer( | |
_controller, | |
key: videoKey, | |
), | |
) | |
: Text('not initialized'), | |
); | |
}, | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
setState(() { | |
_controller.value.isPlaying | |
? _controller.pause() | |
: _controller.play(); | |
}); | |
}, | |
child: Icon( | |
_controller.value.isPlaying | |
? Icons.pause | |
: Icons.play_arrow, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment