最終效果
項目地址
https://github.com/Tecode/flutter_widget
實現方法
安裝插件
安裝video_player
,我安裝的是最新的版本蛮穿,請根據你自己的flutter
版本去安裝對應的版本赶袄,安卓可以直接使用虛擬機,IOS
需要真機才可以播放斤富。
dev_dependencies:
flutter_test:
sdk: flutter
video_player: ^0.10.1+6
我的Flutter
版本
Flutter 1.7.8+hotfix.4 ? channel stable ? https://github.com/flutter/flutter.git
Framework ? revision 2e540931f7 (3 days ago) ? 2019-07-09 13:14:38 -0700
Engine ? revision 54ad777fd2
Tools ? Dart 2.4.0
使用
import 'package:video_player/video_player.dart';
初始化播放
@override
void initState() {
// TODO: implement initState
super.initState();
_controller = VideoPlayerController.network(videoUrl)
..initialize().then((_) {
setState(() {});
_controller.play();
_controller.setLooping(true);
// _controller.setVolume(0.0);
Timer.periodic(Duration(seconds: 15), (Timer time) {
print(time);
});
});
}
銷毀時暫停
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_controller.pause();
}
布局
主要部分
使用Transform.scale
對視頻進行縮放,我們想要的效果就是不管視頻是什么比率与涡,都可以平鋪無拉伸的顯示峭梳。Center
讓視頻放大以后居中顯示,縮放比為_controller.value.aspectRatio /MediaQuery.of(context).size.aspectRatio
虱而,用視頻的寬高比除以設備的寬高比筏餐。
如果我們對視頻進行處理也會鋪滿全部屏幕,但是會被拉伸薛窥,看起來很丑胖烛,可以拉下代碼試一下。
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_controller.pause();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
fit: StackFit.expand,
children: <Widget>[
Transform.scale(
scale: _controller.value.aspectRatio /
MediaQuery.of(context).size.aspectRatio,
child: Center(
child: Container(
child: _controller.value.initialized
? AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),
)
: Text("正在初始化"),
),
),
),
Positioned(
width: MediaQuery.of(context).size.width,
bottom: 26.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: MaterialButton(
onPressed: () {},
child: Text(
"微信登錄",
style:
TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
),
color: Color(0xffFFDB2E),
textColor: Color(0xff202326),
height: 44.0,
minWidth: 240.0,
elevation: 0.0,
),
),
SizedBox(
height: 20.0,
),
ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: MaterialButton(
onPressed: () {},
child: Text(
"手機號登錄",
style:
TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
),
color: Color(0xff202326),
height: 44.0,
minWidth: 240.0,
elevation: 0.0,
textColor: Color(0xffededed),
),
),
SizedBox(
height: 60.0,
),
Text(
"我已閱讀并同意《服務協議》及《隱私政策》",
style: TextStyle(color: Colors.white, fontSize: 13.0),
)
],
),
),
Positioned(
width: MediaQuery.of(context).size.width,
top: 80.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"登錄",
style: TextStyle(
fontSize: 40.0,
fontWeight: FontWeight.w400,
color: Colors.white),
),
SizedBox(
height: 10.0,
),
Text(
"視頻背景登錄頁面",
style: TextStyle(color: Colors.white, fontSize: 15.0),
)
],
),
)
],
));
}
寫在最后
平時會不定期更新其它的組件歡迎關注诅迷,歡迎star。