Flutter实现用视频背景的登录页的示例代码
作者:Tecode 发布时间:2022-01-02 05:45:54
标签:Flutter,登录页
最终效果
项目地址
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。
来源:https://juejin.im/post/5d455196e51d4561ea1a93df


猜你喜欢
- 测试springboot项目出现Test Ignored今天在写springBoot项目运行测试类时出现了以下问题:Test ignored
- 在我们实际开发项目中,经常会遇到一些常量的配置,比如url,暂时不会改变的字段参数,这个时候我们最好是不要直接写死在代码里的,因为这样编写的
- using System;using System.Collections.Generic;using System.Linq;
- 1、概述首先和大家一起回顾一下Java 消息服务,在我之前的博客《Java消息队列-JMS概述》中,我为大家分析了:1.消息服务:一个中间件
- ArrayList中存放引用数据类型ArrayList中存放引用类型时,存放的是一个引用,因此在放入ArrayList之后再进行改动会影响到
- 尽管Java提供了一个可以处理文件的IO操作类。 但是没有一个复制文件的方法。 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时
- import android.app.ListActivity; import android.database.Cursor; impor
- 最近做了个自定义键盘,但面对不同分辨率的机型其中数字键盘不能根据界面大小自已铺满,但又不能每种机型都做一套吧,所以要做成自适应,那这里主讲思
- 1.在实体类中添加@TableId注解:2.在navicat中设置id自动增长:3.测试一下,当我们再次插入的时候,就会看到id4.对注解中
- public class TimeUtil { public static final int SECO
- 1:定义一个自己的父级容器,让它继承自一个布局(LinearLayout、RelativeLayout都可以)public class Si
- 在微信公众号支付的API中没有这个接口,如果企业需要给用户转账,或者让用户提现或者给用户发红包等需要再商户平台中的产品中心分别开通。一、开通
- 我就废话不多说了,大家还是直接看代码吧~package com.zejian.annotationdemo; import java.lan
- 本文实例为大家分享了Android实现圆形云标签效果展示的具体代码,供大家参考,具体内容如下下面是实现的效果图:这个适合用于选择 用户的一些
- 这篇文章主要介绍了Spring Cloud Zuul添加过滤器过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 一、容器初始化1、源码分析在jdk8的ConcurrentHashMap中一共有5个构造方法,这四个构造方法中都没有对内部的数组
- 1.C语言传统的处理错误的方式传统的错误处理机制:1. 终止程序,如assert,缺陷:用户难以接受。如发生内存错误,除0错误时就会终止程序
- 本文实例为大家分享了微信小程序支付C#后端源码,供大家参考,具体内容如下using System;using System.Collecti
- 前言我通常是不太关心代码的具体实现的,因为我的开发语言很杂,倾向于一些最简单通用的方式去解决。今儿不小心在群里看到一位朋友发了下面的java
- using System.Runtime.InteropServices; using System.Text; publicclass F