创建flutter模块
在项目目录下执行
$ cd some/path/
$ flutter create -t module --org com.example my_flutter
flutter模块项目使用androidx
构建脚本
// MyApp/settings.gradle
include ':app' // assumed existing content
setBinding(new Binding([gradle: this])) // new
evaluate(new File( // new
settingsDir.parentFile, // new
'my_flutter/.android/include_flutter.groovy' // new
))
引入flutter模块
// MyApp/app/build.gradle
dependencies {
implementation project(':flutter')
}
activity中引入flutter组件
public void addFlutterView() {
View flutterView = Flutter.createView(
MainActivity.this,
getLifecycle(),
"route3"
);
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
((ViewGroup) findViewById(R.id.layout)).addView(flutterView, layout);
}
public void addFlutterFragment() {
log("addFlutterFragment " + new Date().getTime());
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.layout2, Flutter.createFragment("route1"));
tx.commit();
}
//route 在dart代码中接收window.defaultRouteName 根据不同显示不同的ui
//dart代码
void main() => runApp(getRootView());
Widget getRootView() {
if ("route2" == window.defaultRouteName) {
return MyApp2();
} else {
return MyApp();
}
}
class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
}
//activty中使用路由
override fun createFlutterView(context: Context?): FlutterView {
val flutterView = FlutterView(this, null, this.createFlutterNativeView())
flutterView.setInitialRoute("route1")
this.setContentView(flutterView)
return flutterView
}
}
加速
public class MixApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Flutter.startInitialization(this);
}
}
//或直接使用FlutterApplication
debug模式会慢些,release打包后加载很快的。
网友评论