- Flutter不仅仅可以作为单独的activity使用,也可以作为view或是fragment加入原生应用中,笔者在这里尝试过,把flutter作为view封装成rn的组件在rn框架中使用,也可进行数据传递。
- 集成 Flutter Module到现有项目中,使用以下命令,在项目文件夹同级别目录下.
flutter create -t module gkt_flutter
- 创建完成后 磁盘中会多出来一个Flutter文件夹,和原生项目同级目录
-
然后配置 Project下Setting.grade文件 然后 Sync 即可
图二
//加入下面配置
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile,
'gkt/gkt_flutter/.android/include_flutter.groovy'
))
-
上面的Sync之后,走下面命令,引入flutter,之所以加上 exclude group 是为了防止和项目中已经引入的库重复
图三
implementation(project(':flutter'),{
exclude group: 'com.android.support'
})
-
到这里flutter的moudle就集成到了android的原生view中了,下面看下原生代码。
图四 - 图四里面代码分为两部分,一部分是创建flutterview,需要三个参数,当前activivty对象,第二个活动的生命周期,第三个是传给flutter一个路由参数,window.defaultRouteName就可得到所传页面的routename。
- 创建flutter view并把view加入到布局中,有一秒到两秒多的时间会是白屏,在这段期间我们可以把闪屏页做成全屏dialog,一进入app显示闪屏页,然后监听flutterview第一帧,收到第一帧的时候把dialog关掉,同时把flutterview加入的容器设置为可见,可解决黑屏现象。
- 当应用切到后台多任务时,把不可见写到onPause解决从多任务状态回到当前activity黑屏问题。下面是里面的实现代码,有兴趣的可以尝试一下,有问题也请多交流。
- MainActivity.java
package com.gktapp;
import androidx.appcompat.app.AppCompatActivity;
import io.flutter.facade.Flutter;
import io.flutter.view.FlutterView;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import com.gktapp.dialog.PackageDialog;
public class MainActivity extends AppCompatActivity {
FrameLayout frameLayout = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final PackageDialog packageDialog = new PackageDialog(MainActivity.this);
packageDialog.show();
FlutterView flutterView = Flutter.createView(this,getLifecycle(),"login");
flutterView.setLayoutParams(new FrameLayout.LayoutParams(-1,-1));
frameLayout = findViewById(R.id.main_frame);
final FlutterView.FirstFrameListener[] listeners = new FlutterView.FirstFrameListener[1];
listeners[0] = new FlutterView.FirstFrameListener() {
@Override
public void onFirstFrame() {
frameLayout.setVisibility(View.VISIBLE);
packageDialog.dismiss();
Log.e("lookat","show");
}
};
flutterView.addFirstFrameListener(listeners[0]);
frameLayout.addView(flutterView);
}
@Override
protected void onPause() {
super.onPause();
if(frameLayout != null){
frameLayout.setVisibility(View.INVISIBLE);
}
}
}
- PackageDialog.java
package com.gktapp.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.ViewGroup;
import android.view.WindowManager;
import com.gktapp.R;
import androidx.annotation.NonNull;
public class PackageDialog extends Dialog {
public PackageDialog(@NonNull Context context) {
super(context, R.style.dialog);
setContentView(R.layout.activity_splash);
setOwnerActivity((Activity) context);
}
/**
* 设置宽度全屏,要设置在show的后面
*/
@Override
public void show() {
super.show();
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
//getWindow().getDecorView().setPadding(0, 0, 0, 0);
getWindow().setAttributes(layoutParams);
}
}
- styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@android:color/white</item>
<item name="colorPrimaryDark">@android:color/black</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@android:color/white</item>
<item name="colorPrimaryDark">@android:color/white</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>
</style>
<style name="dialog" parent="@android:style/Theme.Dialog">
<!-- 有无边框 -->
<item name="android:windowFrame">@null</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 背景透明 -->
<item name="android:windowBackground">@color/colorWhite</item>
<!-- 模糊 -->
<item name="android:backgroundDimEnabled">true</item>
<!--无标题 -->
<item name="android:windowNoTitle">true</item>
</style>
</resources>
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="@+id/main_frame">
</FrameLayout>
</LinearLayout>
- main.dart
-
最后看下效果图
图六
网友评论