flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.01k stars 27.19k forks source link

Orientation Locks is not working on Windows Desktop #74979

Closed ugurcan closed 3 years ago

ugurcan commented 3 years ago

We are working on a flutter application which running on Windows Desktop Environment. When we try to lock orientation for windows desktop application it seems "SystemChrome.setPreferredOrientations" doesn't work. You can find our sample code and flutter doctor information at below;

Steps to Reproduce

  1. Build sample application for windows.
  2. Resize application window for portrait or landscape view.

Expected results: Orientation should not be change for portrait view.

Actual results: When resize the window, you can see orieantation is changing according to window size.

Sample Code:

code sample ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]).then( (_) => runApp(MyApp()), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { void initState() { super.initState(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( MediaQuery.of(context).orientation.toString(), style: Theme.of(context).textTheme.headline4, ), ], ), ), ); } } ```

Flutter Doctor Information :

flutter doctor -v ```bash [√] Flutter (Channel master, 1.26.0-18.0.pre.73, on Microsoft Windows [Version 10.0.18363.1256], locale en-GB) • Flutter version 1.26.0-18.0.pre.73 at C:\Src\flutter • Framework revision 791edc230d (7 hours ago), 2021-01-28 21:54:03 -0800 • Engine revision 4ba79a34a0 • Dart version 2.12.0 (build 2.12.0-272.0.dev) [√] Android toolchain - develop for Android devices (Android SDK version 30.0.0) • Android SDK at C:\Users\ugcan\AppData\Local\Android\sdk • Platform android-30, build-tools 30.0.0 • Java binary at: C:\Program Files\Android\Android Studio1\jre\bin\java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe [√] Visual Studio - develop for Windows (Visual Studio Enterprise 2019 16.6.2) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise • Visual Studio Enterprise 2019 version 16.6.30204.135 • Windows 10 SDK version 10.0.18362.0 [√] Android Studio (version 4.0) • Android Studio at C:\Program Files\Android\Android Studio1 • Flutter plugin version 46.0.2 • Dart plugin version 193.7361 • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) [√] VS Code, 64-bit edition (version 1.52.1) • VS Code at C:\Program Files\Microsoft VS Code • Flutter extension version 3.18.1 [√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.18363.1256] • Chrome (web) • chrome • web-javascript • Google Chrome 87.0.4280.141 • Edge (web) • edge • web-javascript • Microsoft Edge 86.0.622.38 • No issues found! ```
TahaTesser commented 3 years ago

Hi @ugurcan Thanks for filing the issue, switching from landscape to portrait does affect windows app orientation.

code sample ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); SystemChrome.setPreferredOrientations( [ DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ], ); runApp( MyApp(), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { void initState() { super.initState(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( MediaQuery.of(context).orientation.toString(), style: Theme.of(context).textTheme.headline4, ), ], ), ), ); } } ```
flutter doctor -v ```bash [✓] Flutter (Channel master, 1.26.0-18.0.pre.73, on Microsoft Windows [Version 10.0.19042.746], locale en-US) • Flutter version 1.26.0-18.0.pre.73 at C:\Users\Taha\Code\flutter_master • Framework revision 791edc230d (9 hours ago), 2021-01-28 21:54:03 -0800 • Engine revision 4ba79a34a0 • Dart version 2.12.0 (build 2.12.0-272.0.dev) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at C:\Users\Taha\Code\SDK • Platform android-30, build-tools 30.0.3 • ANDROID_HOME = C:\Users\Taha\Code\SDK • Java binary at: C:\Users\Taha\Code\android-studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) • All Android licenses accepted. [✓] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [✓] Visual Studio - develop for Windows (Visual Studio Community 2019 16.8.3) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community • Visual Studio Community 2019 version 16.8.30804.86 • Windows 10 SDK version 10.0.18362.0 [✓] Android Studio (version 4.1.0) • Android Studio at C:\Users\Taha\Code\android-studio • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) [✓] VS Code (version 1.52.1) • VS Code at C:\Users\Taha\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.18.1 [✓] Connected device (4 available) • Android SDK built for x86 (mobile) • emulator-5554 • android-x86 • Android 10 (API 29) (emulator) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19042.746] • Chrome (web) • chrome • web-javascript • Google Chrome 88.0.4324.104 • Edge (web) • edge • web-javascript • Microsoft Edge 88.0.705.53 • No issues found! ```

Thank you

stuartmorgan commented 3 years ago

it seems "SystemChrome.setPreferredOrientations" doesn't work.

Can you elaborate on what you expect this to do on desktop? Desktop devices don't have orientations the way mobile devices do; instead, the window is freely resizable (unless you are enforcing constraints on your window resizing using native code).

you can see orieantation is changing according to window size

Per the docs, the orientation is simply telling you if the window is wider than it is tall, or the reverse. If you're resizing the window such that the inequality flips, changing is the correct behavior.

pedromassango commented 3 years ago

Sounds like a invalid issue!

(This needs waiting for customer response label)!

ugurcan commented 3 years ago

You ignore Windows tablets. The Flutter application we are currently developing is for Windows tablets and we want to run them as "Windows Desktop" application. Because of Covid 19, I could not find the chance to test the application in the field, but I got this result when I tried it on my own computer. I would appreciate it if you could specify another method that I can test other than resize.

stuartmorgan commented 3 years ago

You ignore Windows tablets.

I was responding to the issue you filed, which described resizing a window.

The Flutter application we are currently developing is for Windows tablets and we want to run them as "Windows Desktop" application.

Tablets are not currently well supported by the alpha, FYI. There's no specific touch support, and the virtual keyboard is unlikely to show for text fields, for instance. But if you test on tablet and find issues that aren't already filed, please do file them.

I would appreciate it if you could specify another method that I can test other than resize.

I'm still not clear on what you expect to happen on a desktop (not tablet) when you call SystemChrome.setPreferredOrientations; you didn't respond to that question. I don't know what behavior you are expecting when following the steps you describe.

If you want to request that the Windows embedding handle SystemChrome.setPreferredOrientations specifically when running on a tablet that's a reasonable request, but it's not at all what your initial report was describing.

ugurcan commented 3 years ago

Hi @stuartmorgan,

"If you want to request that the Windows embedding handle SystemChrome.setPreferredOrientations specifically when running on a tablet that's a reasonable request, but it's not at all what your initial report was describing."

Yes we asked to use "SystemChrome.setPreferredOrientations" for lock to orientations. We are preffering only landscape usage for our applications on windows tablets. I had thought about that you understand to talking about to lock orientations when we are talking about to "SystemChrome.setPreferredOrientations".

stuartmorgan commented 3 years ago

I understand what you expect it to do on a tablet. I don't understand what you expect it to do on a desktop with a resizeable window, which is what your initial report describes.

I had thought about that you understand to talking about to lock orientations when we are talking about to "SystemChrome.setPreferredOrientations".

I do understand that. What I'm asking is what you think "lock orientations" means in the context of an application running in a resizeable window on desktop.

ugurcan commented 3 years ago

If the window width is greater than the window height, the orientation is displayed horizontally.

If the window height is greater than the window width, the orientation will be vertical.

You can see it from the attached pictures. This was the orientation test in our windows desktop application.

First of all, understanding the orientation behavior in test applications. Apply to our project if it works. That was the plan.

portrait landscape

stuartmorgan commented 3 years ago

If the window width is greater than the window height, the orientation is displayed horizontally.

If the window height is greater than the window width, the orientation will be vertical.

This is exactly the documented behavior of orientation, so your comment establishes that it works correctly.

It doesn't respond to the question I've now asked three times though, which is what you expect SystemChrome.setPreferredOrientations to do on desktop. Given that I'm unable to get an answer to that question I'm going to close this as working as intended, since I expect it not to do anything, so absent an argument for a different expected behavior everything in the initial report (and the issue title) looks like correct behavior to me.

I've filed https://github.com/flutter/flutter/issues/75135 for tablets based on the latter discussion.

TahaTesser commented 3 years ago

@stuartmorgan @pedromassango I've tested this in portrait mode, this is not just for tablets but for vertical monitors or rotated monitors using rotate stands OR monitor arms which don't require switching to tablet mode.

Perhaps I should have been more clear in my reproduction Thank you

TahaTesser commented 3 years ago

@stuartmorgan Please let me know if you need a new proposal besides the tablet Thank

ugurcan commented 3 years ago

@stuartmorgan

From "SystemChrome.setPreferredOrientations" , I am expecting that locks to the orientation and do not rotate the existing view.

Because i am going to use it on a windows tablet with windows application.

From "SystemChrome.setPreferredOrientations" I am expecting that exactly working on android phone as is.

Set to the orientations, do not rotate it, keep existing view exactly as is.

TahaTesser commented 3 years ago

@ugurcan Exactly it still changes orientation in portrait, doesn't lock it in my reproduction, hence labeled the issue, If SystemChrome.setPreferredOrientations was working, it should have locked just like it does on mobile

ugurcan commented 3 years ago

@TahaTesser thank you.

TahaTesser commented 3 years ago

Preview for https://github.com/flutter/flutter/issues/74979#issuecomment-769860702, when simply switching orientation. No modes changes

Windows

ezgif com-gif-maker (1)

Android

ezgif com-gif-maker

stuartmorgan commented 3 years ago

for vertical monitors or rotated monitors

A window on a monitor does not control the orientation of a monitor. A device where every application takes the entire screen has very different behaviors than a device where applications are windowed. See the discussion of iPad multitasking in setPreferredOrientations docs, for instance.

I am expecting that locks to the orientation and do not rotate the existing view

Nothing has rotated; you resized a window. It sounds like you think that MediaQuery's orientation is a measure of rotation; it's not. It's purely a description of whether the content is wider that it is tall, or the reverse.

On a mobile device where the application is always full screen, rotation is the only way you can change that. On a windowed system, it is not.

From "SystemChrome.setPreferredOrientations" I am expecting that exactly working on android phone as is.

A window and a device are fundamentally different things. Applying a setting intended for a device with specific behaviors to a window doesn't make sense.

If I'm understanding what you are both describing here correctly, you want that setting to dissociate the Flutter content rendering from the rest of the OS and cause it to render at 90 degrees to the rest of the OS as the window is resized. That would be extremely bizarre behavior, and I can't see any scenario where a user would want that behavior.

If you want a desktop application to always be portrait, or always be landscape, you should restrict the window resizing to meet your requirements. Rotation is simply not how this works on a desktop OS.

TahaTesser commented 3 years ago

@stuartmorgan Wow, thanks, that's a far better explanation.

ugurcan commented 3 years ago

If we look at the situation from the perspective of the Windows application, I agree with what you say a hundred percent. I think it is the Windows tablet concept that makes the difference here. Because when you put the concept of tablet into the work, the application is no longer a windows application and becomes a mobile application. This complicates things a little bit. Because when the end user buys a new tablet for himself, the applications on it are considered as mobile apps, not windows apps. As the software team, we develop our developments over the Android Emulator and produce the Windows Desktop application as one of the outputs. Of course, when it comes to the tablet application, we are waiting for the results in the emulator on windows tablet. This is exactly where we left. It turns out that we will have to wait a little longer for you to complete your improvements. Thanks for your help.

stuartmorgan commented 3 years ago

If we look at the situation from the perspective of the Windows application

For future reference: if you file an issue that specifically says it's about Windows desktop, and talks about resizing windows, that is how they will be evaluated. If you want to file issues about tablet behavior, you should discuss tablet, not desktop, when filing the issue.

github-actions[bot] commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.