本文针对初学者, 讲述用flutter自带方法做简单逻辑跳转处理.
非初学者或想更方便实现功能的,可了解插件flutter_boost及其它.
-
Flutter跳转到原生iOS页面
1. flutter页面中:
class _MyHomePageState extends State<MyHomePage> {
//平台通道––––跳转到iOS页面
static const platform = const MethodChannel('samples.flutter.jumpto.iOS');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextButton(onPressed: _jumpToIosMethod, child: Text('跳转到iOS页面')),
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
//跳转到iOS页面
Future<Null> _jumpToIosMethod() async {
final String result = await platform.invokeMethod('jumpToIosPage');
print('result===$result');
}
}
2. iOS的 AppDelegate.swift :
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, UINavigationControllerDelegate{
var navigationController: UINavigationController?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
self.navigationController = UINavigationController.init(rootViewController: controller)
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = self.navigationController
self.navigationController?.delegate=self //设置代理 ,配置导航栏的显示与否
window?.makeKeyAndVisible()
let jumpIosChannel = FlutterMethodChannel(name: "samples.flutter.jumpto.iOS",binaryMessenger: controller.binaryMessenger)
//处理-----跳转到iOS页面
jumpIosChannel.setMethodCallHandler({
[weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
// Note: this method is invoked on the UI thread.
guard call.method == "jumpToIosPage" else {
result(FlutterMethodNotImplemented)
return
}
self?.jumpToIosPageMethod(result: result) //跳转页面
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
//跳转到iOS页面
private func jumpToIosPageMethod(result: FlutterResult) {
let vc: UIViewController = JumpTestViewController()
vc.navigationItem.title = "原生页面"
self.navigationController?.pushViewController(vc, animated: true)
result("跳转")
}
//实现UINavigationControllerDelegate代理
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
//如果是Flutter页面,导航栏就隐藏
navigationController.navigationBar.isHidden = viewController.isKind(of: FlutterViewController.self)
}
}
:之所以让AppDelegate继承于UINavigationControllerDelegate,并实现navigationController:willShow方法,,所以实现代理方法对导航栏的显示做了判断。
JumpTestViewController.swift 为:
import Foundation
class JumpTestViewController: UIViewController {
lazy var testLabel: UILabel = {
let label = UILabel()
label.frame.size = CGSize(width: 300, height: 50)
label.backgroundColor = UIColor.blue
label.textColor = UIColor.white
label.text = "原生界面"
label.textAlignment = .center
label.center.x = self.view.bounds.width/2
label.center.y = self.view.bounds.height/2
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.view.addSubview(testLabel)
}
}
3. 效果展示
-
Flutter跳转到原生Android页面
1. flutter页面中:
class _MyHomePageState extends State<MyHomePage> {
//平台通道––––跳转到Android页面
static const platform = const MethodChannel('samples.flutter.jumpto.android');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextButton(onPressed: _jumpToAndroidMethod, child: Text('跳转到Android页面')),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
//跳转到Android页面
Future<Null> _jumpToAndroidMethod() async {
final String result = await platform.invokeMethod('jumpToAndroidPage');
print('result===$result');
}
}
2. Android的 MainActivity.kt :
package com.example.flutter_jumpto_native
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build
import android.util.Log
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
//跳转到原生Android页面
JumpChannel(flutterEngine.dartExecutor.binaryMessenger,this)
}
}
JumpChannel.kt :
package com.example.flutter_jumpto_native
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build
import android.util.Log
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
class JumpChannel(flutterEngine: BinaryMessenger, activity: FlutterActivity): MethodChannel.MethodCallHandler {
private val batteryChannelName = "samples.flutter.jumpto.android"
private var channel: MethodChannel
private var mActivity: FlutterActivity
init {
channel = MethodChannel(flutterEngine, batteryChannelName)
channel.setMethodCallHandler(this)
mActivity = activity;
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
if (call.method == "jumpToAndroidPage") {
var intent = Intent(mActivity,SecondActivity::class.java)
mActivity.startActivity(intent)
result.success('跳转');
}else if(call.method == "别的method"){
//处理samples.flutter.jumpto.android下别的method方法
} else {
result.notImplemented()
}
}
}
SecondActivity.kt :
package com.example.flutter_jumpto_native
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.FragmentActivity
class SecondActivity: FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.e("YM", "第二个页面的渲染");
setContentView(R.layout.activity_second);
}
}
在AndroidManifest.xml的application中注册SecondActivity:
<activity android:name=".SecondActivity"/>
在res文件夹下创建一个layout文件夹,并添加activity_second.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="18sp"
android:text="安卓原生界面" />
</RelativeLayout>