项目工程如何混编Flutter呢?
Flutter页面跳转原生页面又是怎么实现的呢?
flutter要想跳转到原生页面:
class MainPageState extends State<MyHomePage> {
//–跳转到iOS页面
static const platform2 = const MethodChannel('samples.flutter.jumpto.iOS');
//–跳转到Android页面
static const platform3 = 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>[
SizedBox(height: 20,),
TextButton(onPressed: jumpToIosMethod, child: Text('跳转到iOS页面')),
TextButton(onPressed: jumpToAndroidMethod, child: Text('跳转到Android页面')),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
//跳转到iOS页面
Future<Null> jumpToIosMethod() async {
final String result = await platform2.invokeMethod('jumpToIosPage');
print('result==$result');
}
//跳转到Android页面
Future<Null> jumpToAndroidMethod() async {
final String result = await platform3.invokeMethod('jumpToAndroidPage');
print('result==$result');
}
import Flutter
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 = "原生Page"
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)
}
}
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.kt文件里JumpChannel类方法
JumpChannel(flutterEngine.dartExecutor.binaryMessenger,this)
}
}
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
companion object {
private const val TAG = "JumpChannel"
}
init {
Log.d(TAG, "init")
channel = MethodChannel(flutterEngine, batteryChannelName)
channel.setMethodCallHandler(this)
mActivity = activity;
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
Log.d(TAG, "onMethodCall: " + call.method)
if (call.method == "jumpToAndroidPage") {
var intent = Intent(mActivity,SecondActivity::class.java)
mActivity.startActivity(intent)
result.success(TAG);
}else if(call.method == "别的method"){
//处理samples.flutter.jumpto.android下别的method方法
} else {
result.notImplemented()
}
}
}
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);
}
}
<activity android:name=".SecondActivity"/>
<?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="match_parent"
android:layout_height="wrap_content"
android:text="安卓-原生界面" />
</RelativeLayout>
import UIKit
import Flutter
import FlutterPluginRegistrant
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
lazy var flutterEngine = FlutterEngine(name: "my flutter engine")
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
flutterEngine.run()
GeneratedPluginRegistrant.register(with: self.flutterEngine)
return true
}
}
import UIKit
import Flutter
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.brown
// Do any additional setup after loading the view.
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//搜索到了很多中获取控制器跳转的方法,感觉这种获取 控制器跳转最为流畅 (自我感觉)
let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
present(flutterViewController, animated: true, completion: nil)
}
}