Flutter 颤振,如何将页面从横向设置回纵向模式

Flutter 颤振,如何将页面从横向设置回纵向模式,flutter,dart,Flutter,Dart,我已通过添加将我的应用程序屏幕方向设置为纵向 android:screenOrientation="portrait" 到我的android/app/src/main/AndroidManifest.xml,所以当我启动我的应用程序时,它被锁定为纵向模式。我对此感到高兴 我有一个显示视频的页面,我正在使用chuwie插件,它可以在横向模式下播放视频 这是我的密码: class _VideoState extends State<Video> { Ch

我已通过添加将我的应用程序屏幕方向设置为纵向

android:screenOrientation="portrait"
到我的android/app/src/main/AndroidManifest.xml,所以当我启动我的应用程序时,它被锁定为纵向模式。我对此感到高兴

我有一个显示视频的页面,我正在使用chuwie插件,它可以在横向模式下播放视频

这是我的密码:

    class _VideoState extends State<Video> {
  ChewieController _chewieController;
 

  @override
  void initState() {
    super.initState();
    _chewieController = ChewieController(
      fullScreenByDefault: true,
      videoPlayerController: widget.videoPlayerController,
      aspectRatio:  16 / 9,
      autoInitialize: true,
      autoPlay: true,
      allowedScreenSleep: false,
      allowFullScreen: true,
      deviceOrientationsAfterFullScreen: [
        DeviceOrientation.landscapeRight,
        DeviceOrientation.landscapeLeft,
      ],
      errorBuilder: (context, e) {
        return Center(
          child: Text('Some error occurred'),
        );
      },
    );
}

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Chewie(
            controller: _chewieController,
       ),
    );
  }


  @override
  void dispose() {
    widget.videoPlayerController.dispose();
    widget.videoPlayerController.pause();
    _chewieController.dispose();
    super.dispose();

  }
}
class\u VideoState扩展状态{
咀嚼控制器;
@凌驾
void initState(){
super.initState();
_咀嚼控制器=咀嚼控制器(
FullScreen默认值:true,
videoPlayerController:widget.videoPlayerController,
专题:16/9,
自动初始化:true,
自动播放:对,
允许睡眠:错误,
allowFullScreen:是的,
设备方向全屏显示后:[
设备定向,景观权,
DeviceOrientation.landscapeLeft,
],
errorBuilder:(上下文,e){
返回中心(
子项:文本(“发生了一些错误”),
);
},
);
}
@凌驾
小部件构建(构建上下文){
返回SingleChildScrollView(
孩子:朱伊(
控制器:_cheuicontroller,
),
);
}
@凌驾
无效处置(){
widget.videoPlayerController.dispose();
widget.videoPlayerController.pause();
_chouicontroller.dispose();
super.dispose();
}
}
在我完全以横向模式运行视频之前,这一切都很好,没有任何问题,但当我返回到上一页时,我的应用程序(应该处于纵向模式)现在处于横向模式,这是我不希望发生的。我希望我的应用程序仍锁定为纵向模式

我确实尝试过将SystemChrome.setPreferredOrientations添加到我的视频页面并将其处理掉,但这也不起作用

感谢您的帮助

以下是我得到的错误:

══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
The following assertion was thrown while dispatching notifications for VideoPlayerController:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4182 pos 12:
'_debugLifecycleState != _ElementLifecycle.defunct': is not true.

Either the assertion indicates an error in the framework itself, or we should provide substantially
more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md

When the exception was thrown, this was the stack:
#2      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4182:12)
#3      State.setState (package:flutter/src/widgets/framework.dart:1260:14)
#4      _VideoState.initState.<anonymous closure> (package:/widget/page/video.dart:65:9)
#5      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:209:21)
#6      ValueNotifier.value= (package:flutter/src/foundation/change_notifier.dart:276:5)
#7      VideoPlayerController.pause (package:video_player/video_player.dart:360:5)
#8      _VideoState.dispose (package:/widget/page/video.dart:107:34)
#9      StatefulElement.unmount (package:flutter/src/widgets/framework.dart:4773:12)
(elided 5 frames from class _AssertionError and dart:async)

The VideoPlayerController sending notification was:
  VideoPlayerController#2afe2(VideoPlayerValue(duration: 0:10:55.430000, size: Size(1920.0, 1080.0),
  position: 0:10:55.430000, caption: Instance of 'Caption', buffered: [DurationRange(start:
  0:00:00.000000, end: 0:10:55.430000)], isPlaying: false, isLooping: false, isBuffering:
  falsevolume: 1.0, errorDescription: null))
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by foundation library ════════════════════════════════════════════════════
The following assertion was thrown while dispatching notifications for VideoPlayerController:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4182 pos 12: '_debugLifecycleState != _ElementLifecycle.defunct': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md

When the exception was thrown, this was the stack: 
#2      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4182:12)
#3      State.setState (package:flutter/src/widgets/framework.dart:1260:14)
#4      _VideoState.initState.<anonymous closure> (package:/widget/page/video.dart:65:9)
#5      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:209:21)
#6      ValueNotifier.value= (package:flutter/src/foundation/change_notifier.dart:276:5)
...
The VideoPlayerController sending notification was: VideoPlayerController#2afe2(VideoPlayerValue(duration: 0:10:55.430000, size: Size(1920.0, 1080.0), position: 0:10:55.430000, caption: Instance of 'Caption', buffered: [DurationRange(start: 0:00:00.000000, end: 0:10:55.430000)], isPlaying: false, isLooping: false, isBuffering: falsevolume: 1.0, errorDescription: null))
══╡ 基金会例外╞════════════════════════════════════════════════════════
为VideoPlayerController发送通知时引发了以下断言:
“package:flatter/src/widgets/framework.dart”:失败的断言:第4182行位置12:
"生命周期屋!_ElementLifecycle.defunct':不正确。
要么断言表明框架本身存在错误,要么我们应该提供
此错误消息中的更多信息可帮助您确定并修复根本原因。
在任何一种情况下,请通过在GitHub上提交bug来报告此断言:
https://github.com/flutter/flutter/issues/new?template=BUG.md
引发异常时,这是堆栈:
#2 Element.markNeedsBuild(包:flatter/src/widgets/framework.dart:4182:12)
#3 State.setState(包:flatter/src/widgets/framework.dart:1260:14)
#4_VideoState.initState。(包:/widget/page/video.dart:65:9)
#5 ChangeNotifier.notifyListeners(包:flatter/src/foundation/change\u notifier.dart:209:21)
#6 ValueNotifier.value=(包:flatter/src/foundation/change\u notifier.dart:276:5)
#7 VideoPlayerController.pause(软件包:视频播放器/视频播放器。dart:360:5)
#8 _VideoState.dispose(包:/widget/page/video.dart:107:34)
#9 StatefulElement.unmount(包:flatter/src/widgets/framework.dart:4773:12)
(从类断言错误和dart中删除了5帧:异步)
发送通知的VideoPlayerController为:
VideoPlayerController#2afe2(VideoPlayerValue(持续时间:0:10:55.430000,大小:大小:1920.01080.0),
位置:0:10:55.430000,标题:“标题”的实例,缓冲:[持续时间范围(开始:
0:00:00.000000,结束:0:10:55.430000)],isplay:false,isLooping:false,isBuffering:
falsevolume:1.0,errorDescription:null)
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ 基金会例外════════════════════════════════════════════════════
为VideoPlayerController发送通知时引发了以下断言:
“package:flatter/src/widgets/framework.dart”:失败的断言:第4182行位置12:“\u debugLifecycleState!”_ElementLifecycle.defunct':不正确。
要么断言表明框架本身存在错误,要么我们应该在此错误消息中提供更多信息,以帮助您确定并修复根本原因。
在任何一种情况下,请通过在GitHub上提交bug来报告此断言:
https://github.com/flutter/flutter/issues/new?template=BUG.md
引发异常时,这是堆栈:
#2 Element.markNeedsBuild(包:flatter/src/widgets/framework.dart:4182:12)
#3 State.setState(包:flatter/src/widgets/framework.dart:1260:14)
#4_VideoState.initState。(包:/widget/page/video.dart:65:9)
#5 ChangeNotifier.notifyListeners(包:flatter/src/foundation/change\u notifier.dart:209:21)
#6 ValueNotifier.value=(包:flatter/src/foundation/change\u notifier.dart:276:5)
...
发送通知的VideoPlayerController为:VideoPlayerController#2afe2(VideoPlayerValue(持续时间:0:10:55.430000,大小:大小(1920.01080.0),位置:0:10:55.430000,标题:标题实例,缓冲:[持续时间范围(开始:0:00:00.000000,结束:0:10:55.430000)],isplay:false,isLooping:false,isBuffering:false卷:1.0,errorDescription:null)
这是我的更新代码:

class Video extends StatefulWidget {
  final VideoPlayerController videoPlayerController;

  const Video({
    Key key,
    @required this.videoPlayerController,
  }) : super(key: key);

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

class _VideoState extends State<Video> {
  ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    _chewieController = ChewieController(
      fullScreenByDefault: true,
      videoPlayerController: widget.videoPlayerController,
      aspectRatio: 16 / 9,
      autoInitialize: true,
      autoPlay: true,
      allowedScreenSleep: false,
      allowFullScreen: true,
      deviceOrientationsAfterFullScreen: [
        DeviceOrientation.landscapeRight,
        DeviceOrientation.landscapeLeft,
      ],
      errorBuilder: (context, e) {
        return Center(
          child: Text('Some error occurred'),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    //final length = MediaQuery.of(context).size;
    return SingleChildScrollView(
      child: VideoScaffold(
            Chewie(
              controller: _chewieController,
        ),
      ),
    );
  }

  @override
  void dispose() {
    widget.videoPlayerController.dispose();
    widget.videoPlayerController.pause();
    _chewieController.dispose();
    super.dispose();
  }
}

class VideoScaffold extends StatefulWidget {
  const VideoScaffold({Key key, this.child}) : super(key: key);

  final Widget child;

  @override
  State<StatefulWidget> createState() => _VideoScaffoldState();
}

class _VideoScaffoldState extends State<VideoScaffold> {
  @override
  void initState() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
    ]);
    AutoOrientation.landscapeAutoMode();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }

  @override
  dispose() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    AutoOrientation.landscapeAutoMode();
    AutoOrientation.portraitUpMode();
    super.dispose();
  }
}
class视频扩展StatefulWidget{
最终视频播放控制器视频播放控制器;
康斯特视频({
关键点,
DeviceOrientation.portraitUp,
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
    runApp(MyApp());
}


class MyApp extends StatelessWidget {
     @override
     Widget build(BuildContext context) {

      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
//ensures portrait at all times. you can override this if necessary
      ]);

      return MaterialApp(
        home: Scaffold(
          body: Center(child: Text("A Flutter Example!")),
     ),
   );
  }
}
 @override
  dispose() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    super.dispose();
  }