Android跳转Flutter指定界面,笔者目前所知道的有两种方式:
1、设置路由:initialRoute,在Flutter默认入口main中使用window.defaultRouteName 获取值,然后根据该值跳转界面
Android:
CustomActivity.withNewEngine(CustomActivity::class.java).initialRoute("/my_route").build(this@HomPageActivity)
Flutter:
window.defaultRouteName
void main() {
runApp(_createWidget(window.defaultRouteName));
}
Widget _createWidget(String routeName){
print("routeName= "+routeName);
if("/my_route" == routeName){
return Login();
}
return HomePage();
}
2、既然Flutter有默认入口,我们试想是否可以通过修改入口来达到跳转指定界面需求,答案是肯定的
参照源码,可发现默认入口为“main”
public static class DartEntrypoint {
@NonNull
public static DartEntrypoint createDefault() {
return new DartEntrypoint(
FlutterMain.findAppBundlePath(),
"main"
);
}
开始在Android端修改自己的入口为“association”:
var engine = FlutterEngine(this)
//自定义入口
var entrypoint = DartExecutor.DartEntrypoint(FlutterMain.findAppBundlePath(), "association")
engine.dartExecutor.executeDartEntrypoint(
entrypoint
)
然后在Flutter默认入口文件中添加association,如:
void main() {//默认入口
runApp(_createWidget(window.defaultRouteName));
}
void association(){//新入口
WidgetsFlutterBinding.ensureInitialized();
runApp(Association());
}
手写不易,若对你有所帮助,请给个点赞鼓励
网友评论