本文針對初學者, 講述用flutter自帶方法做簡單邏輯跳轉(zhuǎn)處理.
非初學者或想更方便實現(xiàn)功能的驶忌,可了解插件flutter_boost及其它.
-
Flutter跳轉(zhuǎn)到原生iOS頁面
1. flutter頁面中:
class _MyHomePageState extends State<MyHomePage> {
//平臺通道––––跳轉(zhuǎn)到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('跳轉(zhuǎn)到iOS頁面')),
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
//跳轉(zhuǎn)到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 //設(shè)置代理 ,配置導航欄的顯示與否
window?.makeKeyAndVisible()
let jumpIosChannel = FlutterMethodChannel(name: "samples.flutter.jumpto.iOS",binaryMessenger: controller.binaryMessenger)
//處理-----跳轉(zhuǎn)到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) //跳轉(zhuǎn)頁面
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
//跳轉(zhuǎn)到iOS頁面
private func jumpToIosPageMethod(result: FlutterResult) {
let vc: UIViewController = JumpTestViewController()
vc.navigationItem.title = "原生頁面"
self.navigationController?.pushViewController(vc, animated: true)
result("跳轉(zhuǎn)")
}
//實現(xiàn)UINavigationControllerDelegate代理
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
//如果是Flutter頁面笑跛,導航欄就隱藏
navigationController.navigationBar.isHidden = viewController.isKind(of: FlutterViewController.self)
}
}
:之所以讓AppDelegate繼承于UINavigationControllerDelegate付魔,并實現(xiàn)navigationController:willShow方法聊品,
几苍,所以實現(xiàn)代理方法對導航欄的顯示做了判斷翻屈。
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. 效果展示
iOS.gif
-
Flutter跳轉(zhuǎn)到原生Android頁面
1. flutter頁面中:
class _MyHomePageState extends State<MyHomePage> {
//平臺通道––––跳轉(zhuǎn)到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('跳轉(zhuǎn)到Android頁面')),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
//跳轉(zhuǎn)到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)
//跳轉(zhuǎn)到原生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('跳轉(zhuǎn)');
}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文件夾下創(chuàng)建一個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>
3. 效果展示
android.gif