Flutter實(shí)現(xiàn)用視頻背景的登錄頁(yè)的示例代碼
最終效果
項(xiàng)目地址
https://github.com/Tecode/flutter_widget
實(shí)現(xiàn)方法
安裝插件
安裝video_player,我安裝的是最新的版本,請(qǐng)根據(jù)你自己的flutter版本去安裝對(duì)應(yīng)的版本,安卓可以直接使用虛擬機(jī),IOS需要真機(jī)才可以播放。
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); }); }); }
銷毀時(shí)暫停
@override void dispose() { // TODO: implement dispose super.dispose(); _controller.pause(); }
布局
主要部分
使用Transform.scale對(duì)視頻進(jìn)行縮放,我們想要的效果就是不管視頻是什么比率,都可以平鋪無(wú)拉伸的顯示。Center讓視頻放大以后居中顯示,縮放比為_(kāi)controller.value.aspectRatio /MediaQuery.of(context).size.aspectRatio,用視頻的寬高比除以設(shè)備的寬高比。
如果我們對(duì)視頻進(jìn)行處理也會(huì)鋪滿全部屏幕,但是會(huì)被拉伸,看起來(lái)很丑,可以拉下代碼試一下。
@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( "手機(jī)號(hào)登錄", 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( "我已閱讀并同意《服務(wù)協(xié)議》及《隱私政策》", 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( "視頻背景登錄頁(yè)面", style: TextStyle(color: Colors.white, fontSize: 15.0), ) ], ), ) ], )); }
寫在最后
平時(shí)會(huì)不定期更新其它的組件歡迎關(guān)注,歡迎star。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用Flutter實(shí)現(xiàn)錄音插件
這篇文章主要介紹了基于flutter實(shí)現(xiàn)錄音功能,介紹了如何錄音,如何把文件存放到本地,這些都是我們平常使用這個(gè)功能會(huì)遇到的問(wèn)題。在使用的過(guò)程中遇到的問(wèn)題也有列出,需要的朋友可以參考下2022-08-08Android頁(yè)面之間進(jìn)行數(shù)據(jù)回傳的方法分析
這篇文章主要介紹了Android頁(yè)面之間進(jìn)行數(shù)據(jù)回傳的方法,結(jié)合實(shí)例形式分析了Android頁(yè)面之間進(jìn)行數(shù)據(jù)的傳遞與處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06Android實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10Kotlin與Java相互調(diào)用的完整實(shí)例
Kotlin的設(shè)計(jì)過(guò)程中就考慮到了與Java的互操作性,在Kotlin中可以直接調(diào)用既有的Java代碼,反過(guò)來(lái)在Java中也可以很流暢地使用Kotlin代碼,這篇文章主要給大家介紹了關(guān)于Kotlin與Java相互調(diào)用的相關(guān)資料,需要的朋友可以參考下2021-12-12uniapp打包Android的apk(原生APP-云打包)及發(fā)布測(cè)試全過(guò)程
uni-app本地打包apk需要提前做非常多的準(zhǔn)備工作,而且可能會(huì)勸退一些開(kāi)發(fā)者,下面這篇文章主要給大家介紹了關(guān)于uniapp打包Android的apk(原生APP-云打包)及發(fā)布測(cè)試的相關(guān)資料,需要的朋友可以參考下2023-02-02Android 屏幕實(shí)現(xiàn)上下翻轉(zhuǎn)
這篇文章主要介紹了Android 屏幕實(shí)現(xiàn)上下翻轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2017-07-07基于Android實(shí)現(xiàn)個(gè)性彩色好看的二維碼
二維碼在我們?nèi)粘I钪袩o(wú)處不在,今天小編通過(guò)本教程給大家介紹基于Android實(shí)現(xiàn)個(gè)性彩色好看的二維碼,需要的朋友參考下吧2016-02-02