【问题标题】:Flutter YouTube Player not Rotate When I rotate the screen当我旋转屏幕时,颤振 YouTube 播放器不旋转
【发布时间】:2021-04-30 21:59:49
【问题描述】:

Flutter 开发者你好吗?我的代码中出现的问题是,当我旋转屏幕时,我的颤动应用程序中的 YouTube 播放器不旋转,这意味着 youtube 播放器始终处于纵向形式,但我希望当设备处于纵向形式时,youtube 播放器会自动进入纵向形式,当设备处于横向形式时,youtube 播放器以横向形式进入并像 YouTube 一样覆盖屏幕。

这是我的颤振代码

class YoutubePlayerWidget extends StatefulWidget {
  final String videoUrl;
  final UniqueKey newKey;
  YoutubePlayerWidget({@required this.videoUrl, this.newKey});

  @override
  _YoutubePlayerWidgetState createState() => _YoutubePlayerWidgetState();
}

class _YoutubePlayerWidgetState extends State<YoutubePlayerWidget> {
  // String videoURL = "https://www.youtube.com/watch?v=n8X9_MgEdCg";

  YoutubePlayerController _controller;

  void initState() {
    _controller = YoutubePlayerController(
        initialVideoId: YoutubePlayer.convertUrlToId(widget.videoUrl));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => false,
      child: Scaffold(
        body: Container(
          color: Colors.black,
          child: Align(
            alignment: Alignment.center,
            child: FittedBox(
              fit: BoxFit.cover,
              child: YoutubePlayer(
                controller: _controller,
                showVideoProgressIndicator: true,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

我在这个项目中的第二个代码在这里

class YoutubeScreen extends StatefulWidget {
  static const routeName = '/youtube';
  @override
  _YoutubeScreenState createState() => _YoutubeScreenState();
}

class _YoutubeScreenState extends State<YoutubeScreen> {
  String videoURL = "https://www.youtube.com/watch?v=n8X9_MgEdCg";

  YoutubePlayerController _controller;

  void initState() {
    _controller = YoutubePlayerController(
        initialVideoId: YoutubePlayer.convertUrlToId(videoURL));

    super.initState();
  }

  Future<bool> _willPopCallback() async {
    // await showDialog or Show add banners or whatever
    // then
    return true; // return true if the route to be popped
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _willPopCallback,
      child: OrientationBuilder(
          builder: (BuildContext context, Orientation orientation) {
        if (orientation == Orientation.landscape) {
          return Scaffold(
            body: Container(
              color: Colors.black,
              child: Align(
                alignment: Alignment.center,
                child: FittedBox(
                  fit: BoxFit.fill,
                  child: YoutubePlayer(
                    controller: _controller,
                    showVideoProgressIndicator: true,
                  ),
                ),
              ),
            ),
          );
        } else {
          return Scaffold(
            appBar: AppBar(
              iconTheme: IconThemeData(
                color: kSecondaryColor, //change your color here
              ),
              title: Image.asset(
                'assets/images/logo.png',
                fit: BoxFit.contain,
                height: 32,
              ),
              backgroundColor: kBackgroundColor,
            ),
            body: Container(
              color: Colors.black,
              child: Align(
                alignment: Alignment.center,
                child: FittedBox(
                  fit: BoxFit.fill,
                  child: YoutubePlayer(
                    controller: _controller,
                    showVideoProgressIndicator: true,
                  ),
                ),
              ),
            ),
          );
        }
      }),
    );
  }
}

【问题讨论】:

    标签: flutter rotation youtube-api flutter-layout


    【解决方案1】:

    重写你的 dispose 方法如下:

    @override
      void dispose() {
        _controller.dispose();
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.portraitUp,
        ]);
        super.dispose();
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2013-12-21
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多