我正在开发需要呈现本机 UI 的颤振应用程序,为了实现这一点,我用来在 iOS 和iOSMethodChannel
之间建立连接,我能够将数据从颤振视图传递到本机并导航本机 UI,一旦本机 UI 被关闭,然后发送消息回扑。从本机端收到消息后,我正在努力导航颤振屏幕。flutter
Native
让我更详细地解释一下:我创建了一个通道对象,然后从颤振调用本机函数:
Future<Null> _openNewPage() async {
final response = await channel.invokeMethod("openPage", ["Hi From Flutter"]);
}
中的 iOS 代码didFinishLaunchingWithOptions
:
let flutterController : FlutterViewController = window?.rootViewController as! FlutterViewController
let channel: FlutterMethodChannel = FlutterMethodChannel(name: "CHANNEL", binaryMessenger: flutterController.binaryMessenger)
channel.setMethodCallHandler { [unowned self] (methodCall, result) in
guard let arg = (methodCall.arguments as! [String]).first else { return }
switch methodCall.method {
case "openPage":
self.openSecondPage(result: result)
default:
debugPrint(methodCall.method)
result(methodCall.method)
}
}
self.openSecondPage
方法具有导航本机 iOS UI 的代码,并且工作正常。
一旦该 UI 被关闭,结果消息就会通过参数传递给颤振。这个方法在这里处理结果 Future<dynamic> _handleMethod(MethodCall call) async {}
。
在这种方法中,我将颤振活动推为:
Navigator.of(context).push(MaterialPageRoute(builder: (context) => PreviewActivity(videoFile: path)));
但这不起作用,任何人都可以指导我。
更新:
将数据传递给颤振视图的本机代码:
let flutterVC = UIApplication.shared.keyWindow?.rootViewController as? FlutterViewController
let channel = FlutterMethodChannel(name: "CHANNEL", binaryMessenger: flutterVC as! FlutterBinaryMessenger)
channel.invokeMethod("sendFromNative", arguments: path)
在颤振方面,我将代码实现为:
DashBoardParentScreen({Key key, this.guestUserType}) : super(key: key){
channel.setMethodCallHandler(_handleMethod);
}
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case 'sendFromNative':
String text = call.arguments as String;
print("PathFromNative:: $text");
pushPreviewScreen(text);
}
}
pushPreviewScreen(String path){
print("calledFunction:: $path");
Navigator.of(context).push(MaterialPageRoute(builder: (context) => PreviewActivity(videoFile: path)));
}
一旦本机方法向颤动端发送消息,那么我需要显示其他预览屏幕。