#ios #objective-c #swift #flutter
#iOS #objective-c #swift #флаттер
Вопрос:
Я открыл собственный UIViewController раскадровки, используя вызов fluttermethod Channel из раздела Flutter. Теперь я хочу перейти к другому виджету / просмотру, используя вызов метода из раздела iOS (swift). В части вызова метода и маршрутизации нет проблем, кроме одной. Новое представление, которое я перешел из раздела flutter с помощью invokeMethod из кода iOS, покрывается текущим представлением iOS. То есть новый виджет навигации flutter находится под iOS UIViewController, который я открыл из метода flutter «await platform.invokeMethod(‘Printy’);». Я хочу перемещаться по текущему iOS UIViewController.
AppDelegate.swift
@objc class AppDelegate: FlutterAppDelegate {
var navigationController: UINavigationController?
var CHANNEL: FlutterMethodChannel?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller = window.rootViewController as! FlutterViewController
CHANNEL = FlutterMethodChannel(name: "com.flutter.epic/epic", binaryMessenger: controller as! FlutterBinaryMessenger)
CHANNEL?.setMethodCallHandler { [unowned self] (methodCall, result) in
if methodCall.method == "Printy" {
self.newViewController(result)
}
}
GeneratedPluginRegistrant.register(with: self)
navigationController = UINavigationController(rootViewController: controller)
navigationController?.isNavigationBarHidden = true
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func newViewController(_ result: @escaping FlutterResult) {
let storyboard : UIStoryboard? = UIStoryboard.init(name: "Main", bundle: nil);
let window: UIWindow = ((UIApplication.shared.delegate?.window)!)!
let paytmTransactionController = storyboard!.instantiateViewController(withIdentifier: "PaytmTransactionController") as! PaytmTransactionController
paytmTransactionController.result = result
self.navigationController?.pushViewController(paytmTransactionController, animated: true)
}
}
PaytmTransactionController.swift
class PaytmTransactionController: UIViewController {
var result: FlutterResult?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func close(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
@IBAction func showFlutterView(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
(UIApplication.shared.delegate as? AppDelegate)!.CHANNEL!.invokeMethod("didRecieveTranscript", arguments: nil)
}
}
Flutter -> App.dart
class AppState extends State<App> {
final GlobalKey<NavigatorState> navigatorKey =
new GlobalKey<NavigatorState>();
@override
void initState() {
super.initState();
final channelName = 'com.flutter.epic/epic';
final methodChannel = MethodChannel(channelName);
methodChannel.setMethodCallHandler(this._didRecieveTranscript);
}
Future<void> _didRecieveTranscript(MethodCall call) async {
final String utterance = call.arguments;
switch (call.method) {
case "didRecieveTranscript":
openMyProject(utterance);
break;
case "didRecieveNativeViewFactory":
didRecieveNativeViewFactory(utterance);
break;
}
}
Future<void> didRecieveNativeViewFactory(utterance) async {
navigatorKey.currentState
.pushNamed("/project", arguments: ScreenArguments());
}
Future<void> openMyProject(utterance) async {
navigatorKey.currentState
.pushNamed("/project", arguments: ScreenArguments());
}
@override
Widget build(BuildContext context) {
// TODO: implement build
if (this._locale == null) {
return CircularProgressIndicator();
} else {
return Provider(
create: (context) => ApiService.create(),
child: ZoningProvider(
child: GlobalProvider(
child: DashboardProvider(
child: AuthProvider(
child: MaterialApp(
title: GlobalVariable.appTitle,
theme: ThemeData(
//fontFamily: FontClass.defaultRegular,
),
onGenerateRoute: RouterClass().router,
navigatorKey: navigatorKey,
supportedLocales: [
Locale('en', 'US'),
Locale('zh', 'Chinese'),
],
localizationsDelegates: [
AppLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
],
localeResolutionCallback: (locale, supportedLocales) {
for (var supportedLocale in supportedLocales) {
if (supportedLocale.languageCode == locale.languageCode amp;amp;
supportedLocale.countryCode == locale.countryCode) {
return supportedLocale;
}
}
return supportedLocales.first;
},
locale: _locale,
),
),
),
),
),
);
}
}
}
Ответ №1:
После изучения двух дней я нашел решение. Это просто, просто вызовите «SystemNavigator.pop()», чтобы открыть маршрут iOS со стороны Flutter.