屏幕旋转
parent
4727e01e89
commit
3b95b8b58d
|
@ -1,13 +1,77 @@
|
||||||
import Flutter
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
import Flutter
|
||||||
|
|
||||||
@main
|
@main
|
||||||
@objc class AppDelegate: FlutterAppDelegate {
|
@objc class AppDelegate: FlutterAppDelegate {
|
||||||
|
// 动态方向掩码(默认竖屏)
|
||||||
|
static var orientationMask: UIInterfaceOrientationMask = .portrait
|
||||||
|
|
||||||
override func application(
|
override func application(
|
||||||
_ application: UIApplication,
|
_ application: UIApplication,
|
||||||
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
|
|
||||||
|
let controller = window?.rootViewController as! FlutterViewController
|
||||||
|
let channel = FlutterMethodChannel(name: "app.orientation",
|
||||||
|
binaryMessenger: controller.binaryMessenger)
|
||||||
|
|
||||||
|
channel.setMethodCallHandler { [weak self] call, result in
|
||||||
|
guard let self = self else { return }
|
||||||
|
|
||||||
|
if call.method == "setOrientation" {
|
||||||
|
guard let arg = call.arguments as? String else {
|
||||||
|
result(FlutterError(code: "BAD_ARGS", message: "need 'landscape' | 'portrait'", details: nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先更新允许的方向掩码
|
||||||
|
if arg == "landscape" {
|
||||||
|
AppDelegate.orientationMask = .landscape
|
||||||
|
} else if arg == "portrait" {
|
||||||
|
AppDelegate.orientationMask = .portrait
|
||||||
|
} else {
|
||||||
|
result(FlutterError(code: "BAD_ARGS", message: "unknown arg", details: nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 再请求实际旋转
|
||||||
|
if #available(iOS 16.0, *) {
|
||||||
|
// 通知顶层 VC:其 supportedInterfaceOrientations 需要刷新
|
||||||
|
self.window?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
|
||||||
|
|
||||||
|
if let scene = self.window?.windowScene {
|
||||||
|
let orientations: UIInterfaceOrientationMask =
|
||||||
|
(arg == "landscape") ? .landscape : .portrait
|
||||||
|
do {
|
||||||
|
try scene.requestGeometryUpdate(.iOS(interfaceOrientations: orientations))
|
||||||
|
} catch {
|
||||||
|
result(FlutterError(code: "GEOMETRY_UPDATE_FAILED",
|
||||||
|
message: error.localizedDescription, details: nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let target: UIInterfaceOrientation =
|
||||||
|
(arg == "landscape") ? .landscapeLeft : .portrait
|
||||||
|
UIDevice.current.setValue(target.rawValue, forKey: "orientation")
|
||||||
|
UIViewController.attemptRotationToDeviceOrientation()
|
||||||
|
}
|
||||||
|
|
||||||
|
result(true)
|
||||||
|
} else {
|
||||||
|
result(FlutterMethodNotImplemented)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
GeneratedPluginRegistrant.register(with: self)
|
GeneratedPluginRegistrant.register(with: self)
|
||||||
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 关键:把当前的掩码提供给系统
|
||||||
|
override func application(_ application: UIApplication,
|
||||||
|
supportedInterfaceOrientationsFor window: UIWindow?)
|
||||||
|
-> UIInterfaceOrientationMask {
|
||||||
|
return AppDelegate.orientationMask
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,17 +86,13 @@ class HttpManager {
|
||||||
}) async {
|
}) async {
|
||||||
Response resp;
|
Response resp;
|
||||||
final url = baseUrl + path;
|
final url = baseUrl + path;
|
||||||
final options = Options(
|
|
||||||
method: method.name.toUpperCase(),
|
|
||||||
contentType: headerType,
|
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case Method.get:
|
case Method.get:
|
||||||
final queryParameters = <String, dynamic>{};
|
final queryParameters = <String, dynamic>{};
|
||||||
if (params != null) queryParameters.addAll(params);
|
if (params != null && params.isNotEmpty) queryParameters.addAll(params);
|
||||||
if (data != null) queryParameters.addAll(data);
|
if (data != null && data.isNotEmpty) queryParameters.addAll(data);
|
||||||
resp = await _dio.get(
|
resp = await _dio.get(
|
||||||
url,
|
url,
|
||||||
queryParameters: queryParameters,
|
queryParameters: queryParameters,
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:package_info_plus/package_info_plus.dart';
|
import 'package:package_info_plus/package_info_plus.dart';
|
||||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||||
|
|
||||||
|
@ -396,4 +397,14 @@ class NoDataWidget {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
class NativeOrientation {
|
||||||
|
static const MethodChannel _channel = MethodChannel('app.orientation');
|
||||||
|
|
||||||
|
static Future<void> setLandscape() async {
|
||||||
|
await _channel.invokeMethod('setOrientation', 'landscape');
|
||||||
|
}
|
||||||
|
static Future<void> setPortrait() async {
|
||||||
|
await _channel.invokeMethod('setOrientation', 'portrait');
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue