flutter - 登录时 Flutter 中的全屏视频背景

标签 flutter

我想在我的登录页面(如 Spotify)中显示一个视频,您可以在其中播放视频和登录或注册按钮:

enter image description here

这是我目前找到的插件:

video_player 0.7.2但我认为目的不同。

我知道如何显示图像,但我无法使用上面的插件代替图像。这就是我现在使用图像作为背景的内容

body: new Container(
    height: MediaQuery.of(context).size.height,
    decoration: BoxDecoration(
      color: Colors.redAccent,
      image: DecorationImage(
        colorFilter: new ColorFilter.mode(
            Colors.black.withOpacity(0.1), BlendMode.dstATop),
        image: AssetImage('assets/my_image.jpg'),
        fit: BoxFit.cover,
      ),
    ),
    ...
  ..

感谢和良好的编码

最佳答案

使用 FittedBox 小部件处理溢出的解决方案:

Stack(
  children: <Widget>[
    SizedBox.expand(
      child: FittedBox(
        fit: BoxFit.cover,
        child: SizedBox(
          width: _controller.value.size?.width ?? 0,
          height: _controller.value.size?.height ?? 0,
          child: VideoPlayer(_controller),
        ),
      ),
    ),
    LoginWidget()
  ],
)

一个完整的例子是:

import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';

void main() => runApp(BackgroundVideo());

class BackgroundVideo extends StatefulWidget {
  @override
  _BackgroundVideoState createState() => _BackgroundVideoState();
}

class _BackgroundVideoState extends State<BackgroundVideo> {
  VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(
        'http://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4')
      ..initialize().then((_) {
        _controller.play();
        _controller.setLooping(true);
        // Ensure the first frame is shown after the video is initialized
        setState(() {});
      });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Stack(
          children: <Widget>[
            SizedBox.expand(
              child: FittedBox(
                fit: BoxFit.cover,
                child: SizedBox(
                  width: _controller.value.size?.width ?? 0,
                  height: _controller.value.size?.height ?? 0,
                  child: VideoPlayer(_controller),
                ),
              ),
            ),
            LoginWidget()
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}

class LoginWidget extends StatelessWidget {
  const LoginWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[
        Container(),
        Container(
          padding: EdgeInsets.all(16),
          width: 300,
          height: 200,
          color: Colors.grey.withAlpha(200),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              TextField(
                decoration: InputDecoration(
                  hintText: 'Username',
                ),
              ),
              TextField(
                decoration: InputDecoration(
                  hintText: 'Password',
                ),
              ),
              RaisedButton(
                child: Text('Login'),
                onPressed: () {},
              ),
            ],
          ),
        ),
      ],
    );
  }
}

关于flutter - 登录时 Flutter 中的全屏视频背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53075320/

相关文章:

flutter - 是否可以通过 flutter app 共享您的位置

flutter - flutter :如何解决底部溢出

flutter - 如何在 Flutter 中点击卡片打开另一个页面

FlutterDriver - 如何关闭 showDialog

firebase - 使用 map 的 Flutter Firestore where 子句

flutter - 在 flutter 中将字符串读取为 dart 代码的方法?

user-interface - 防止内容在 SliverAppBar 下方滚动

android - Flutter 中任务 ':app:stripDebugDebugSymbols' 执行失败

flutter - 相当于flutter中的wrap_content和match_parent?

flutter - Flutter SharedPreferences getInstance返回null