开发集成环境
[✓] Flutter (Channel stable, v1.12.13+hotfix.9, on Mac OS X 10.14.6 18G103, locale zh-Hans-CN)
一、简述Flutter集成到Android原生项目
二、Android原生以AAR形式集成Flutter项目
三、Flutter与原生(Android/IOS)的消息通信
四、Flutter中如何使用原生控件/组件
五、Flutter状态管理Provider与Redux
六、Flutter升级及开发中遇到的问题汇总
此前记录的关于升级到Flutter V1.12与之前版本异同点
-
不需要在Application中初始化
去除
FlutterMain.startInitialization(this);
初始化方法。 io.flutter.facade包已移除,flutterView不再建议使用
-
原来的FlutterActivity包路径替换
由
io.flutter.app.FlutterActivity
替换成io.flutter.embedding.android.FlutterActivity
-
修改启动FlutterActivity方法
官方启动方式:
startActivity( FlutterActivity .withNewEngine() .initialRoute("/test") .build(currentActivity) );
稍微走进FlutterActivity源码你会发现:
@NonNull public static NewEngineIntentBuilder withNewEngine() { return new NewEngineIntentBuilder(FlutterActivity.class); } protected NewEngineIntentBuilder(@NonNull Class<? extends FlutterActivity> activityClass) { this.activityClass = activityClass; } @NonNull public Intent build(@NonNull Context context) { return new Intent(context, activityClass) .putExtra(EXTRA_INITIAL_ROUTE, initialRoute) .putExtra(EXTRA_BACKGROUND_MODE, backgroundMode) .putExtra(EXTRA_DESTROY_ENGINE_WITH_ACTIVITY, true); }
可以看出这个withNewEngine方法传的一直都是FlutterActivity.class,然后new一个NewEngineIntentBuilder,而NewEngineIntentBuilder的构造函数是protected,不是子类没法直接new。那只好继承NewEngineIntentBuilder这个类,重写它的构造函数。
改造如下:
public class MyFlutterActivity extends FlutterActivity { public static void openFlutter(Activity activity, String routerUrl){ Intent intent = MyFlutterActivity.withNewEngine() .initialRoute(routerUrl) .build(activity); //主要加入这句话实现透明Activity //intent.putExtra("background_mode","transparent"); activity.startActivity(intent); } public static AhsEngineIntentBuilder withNewEngine() { return new AhsEngineIntentBuilder(MyFlutterActivity.class); } public static AhsEngineIntentBuilder withNewEngine(Class<? extends FlutterActivity> activityClass) { return new AhsEngineIntentBuilder(activityClass); } public static class AhsEngineIntentBuilder extends NewEngineIntentBuilder { protected AhsEngineIntentBuilder(Class<? extends FlutterActivity> activityClass) { super(activityClass); } } }
-
启动FlutterActivity过渡的图片
为了启动了FlutteActivity后加载flutter过程中显示的图片,没设置一般是白屏或者黑屏。
如果你之前设置了android:name="io.flutter.app.android.SplashScreenUntilFirstFrame".
需要移除掉.添加如下代码:
<!-- 在所在的activity中加入 --> <meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="@drawable/placehold_background" />
-
flutter出现黑屏无法显示
需要在flutter的main函数中,runApp前调用如下方法即可:
void main() { WidgetsFlutterBinding.ensureInitialized(); runApp(MyApp()); }
-
MethodChannel的应用
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler()
改成:
MethodChannel(flutterEngine.dartExecutor, CHANNEL).setMethodCallHandler()