用Android studio打开刚才创建的功能,我们刚创建好的时候不会有.android与.ios这两个目录的,用Android studio打开构建一下就出来了(也可以用flutter make-host-app-editable命令来创建出来),.android与.ios中都存在一个Flutter目录,这个很重要,我试过只有通过flutter create -t module XXX命令生成出来的才有这个Flutter的文件夹,直接创建出来的没有,Flutter这个是我们flutter的库项目,Android用来生成aar,iOS用来生成framework
2.接下来就是依赖了,添加flutter的依赖
这里要注意层级关系,要和flutter:一个层级
flutter_boost: ^0.0.415
添加完依赖后,在flutter中初始化flutter_boost,配置路由
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:fluttr_test/firstpage.dart';
import 'package:fluttr_test/second.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
@override
void initState() {
// TODO: implement initState
super.initState();
FlutterBoost.singleton.registerPageBuilders({
'first':(pageName,params,_){//格式为路由名称:(pageName,params,_)pageName是路由名称,params为原生传递过来的参数
return FirstPage();//返回一个widget
},
'second':(pageNmae,params,_){
return Second();
},
});
FlutterBoost.handleOnStartPage();//查询当前的顶层页面,并加载它
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'oa',
builder: FlutterBoost.init(),
home: Container(),
);
}
}
这里切记flutter一定要生成出.android与.ios两个文件
setBinding(new Binding(gradle:this))
evaluate(new File('../flutter_test/.android/include_flutter.groovy'))
implementation project(path:':flutter')
implementation project(":flutter_boost")
FlutterMain.startInitialization(this);
FlutterBoostPlugin.init(new IPlatform() {
@Override
public Application getApplication() {
return FunnyApplication.this;
}
/**
* 获取应用入口的Activity,这个Activity在应用交互期间应该是一直在栈底的
* @return
*/
@Override
public Activity getMainActivity() {
if (MainActivity.sRef != null) {
return MainActivity.sRef.get();
}
return null;
}
@Override
public boolean isDebug() {
return true;
}
/**
* 如果flutter想打开一个本地页面,将会回调这个方法,页面参数将会拼接在url中
*
* 例如:sample://nativePage?aaa=bbb
*
* 参数就是类似 aaa=bbb 这样的键值对
*
* @param context
* @param url
* @param requestCode
* @return
*/
@Override
public boolean startActivity(Context context, String url, int requestCode) {
Debuger.log("startActivity url="+url);
return PageRouter.openPageByUrl(context,url,requestCode);
}
@Override
public Map getSettings() {
return null;
}
});
package com.example.funny.flutter;
import com.taobao.idlefish.flutterboost.containers.BoostFlutterActivity;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class FlutterPageActivity extends BoostFlutterActivity {
@Override
public void onRegisterPlugins(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
/**
* 该方法返回当前Activity在Flutter层对应的name,
* 混合栈将会在flutter层根据这个名字,在注册的Route表中查找对应的Widget
*
* 在flutter层有注册函数:
* FlutterBoost.singleton.registerPageBuilders({
* 'first': (pageName, params, _) => FirstRouteWidget(),
* 'second': (pageName, params, _) => SecondRouteWidget(),
* ...
* });
*
* 该方法中返回的就是注册的key:first , second
*
* @return
*/
@Override
public String getContainerName() {
return "first";
}
/**
* 该方法返回的参数将会传递给上层的flutter对应的Widget
*
* 在flutter层有注册函数:
* FlutterBoost.singleton.registerPageBuilders({
* 'first': (pageName, params, _) => FirstRouteWidget(),
* 'second': (pageName, params, _) => SecondRouteWidget(),
* ...
* });
*
* 该方法返回的参数就会封装成上面的params
*
* @return
*/
@Override
public Map getContainerParams() {
Map<String,String> params = new HashMap<>();
params.put("aaa","bbb");
return params;
}
}
FlutterBoost.singleton.openPage("oa://createNew", {
"query": {"userid":"123"}
});
public class PageRouter {
public static final String CREATE_NEW = "oa://createNew";
public static boolean openPageByUrl(Context context, String url) {
return openPageByUrl(context, url, 0);
}
public static boolean openPageByUrl(Context context, String url, int requestCode) {
try {
if(url.startsWith(CREATE_NEW)){
context.startActivity(new Intent(context,CreateFunnyActivity.class));
return true;
} else {
return false;
}
} catch (Throwable t) {
return false;
}
}
}
public class FlutterPageFragment extends BoostFlutterFragment
package com.example.funny.flutter;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import java.util.Map;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.platform.PlatformView;
public class MyFlutterView implements PlatformView {
private TextView view;
public MyFlutterView(Context context, BinaryMessenger messenger, int id, Map<String, Object> map) {
TextView textView = new TextView(context);
textView.setText("这是AndroidView");
this.view = textView;//我们返回一个text
}
@Override
public View getView() {
return view;
}
@Override
public void dispose() {
}
}
public class MyFlutterFactory extends PlatformViewFactory {
private BinaryMessenger message;
public MyFlutterFactory(BinaryMessenger messenger) {
super(StandardMessageCodec.INSTANCE);
this.message = messenger;
}
@Override
public PlatformView create(Context context, int i, Object o) {
Map<String, Object> map = (Map<String, Object>) o;
return new MyFlutterView(context, message, i, map);
}
}
public class FlutterPageActivity extends BoostFlutterActivity {
@Override
public void onRegisterPlugins(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
final String key = FlutterPageActivity.class.getCanonicalName();
if (registry.hasPlugin(key)) return;
PluginRegistry.Registrar registrar = registry.registrarFor(key);
registrar.platformViewRegistry().registerViewFactory("plugins.nightfarmer.top/myTestView", new MyFlutterFactory(registrar.messenger()));
}
child:Platform.isAndroid?AndroidView(viewType: 'plugins.nightfarmer.top/myTestView'):Text("这还是Flutter 的文字",style: TextStyle(color: Colors.white),)