原生里面集成flutter module
创建flutter模块:
flutter create -t module --org com.example flutter_module
查看android项目的app的build.gradle文字中是否有如下代码:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
然后在android项目的跟目录setting.gradle中配置:
在app的build.gradle 里面加上如下代码dependencies { }里面:
implementation project(':flutter')
在project的build.gradle 里面加上如下代码:
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
}
}
从原生页面调到flutter 页面.
AndroidManifest.xml 这个文件里面 增加一个<activity 给Flutter用.
在application节点内添加代码:
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:theme="@style/Theme.AppCompat.Light"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
/>
两种跳转方式
一:Activity跳转
public class MainActivity extends AppCompatActivity {
public FlutterEngine flutterEngine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//-----这一块代码都是加引擎缓存用的.---------
// Instantiate a FlutterEngine.
flutterEngine = new FlutterEngine(this);
// Start executing Dart code to pre-warm the FlutterEngine.
flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
// Cache the FlutterEngine to be used by FlutterActivity.
FlutterEngineCache.getInstance().put("engine_f_m", flutterEngine);
//-------------------------------------
setContentView(R.layout.activity_main);
findViewById(R.id.buttonT).setOnClickListener(v->{
// startActivity(FlutterActivity.createDefaultIntent(this));
startActivity(FlutterActivity.withCachedEngine("engine_f_m").build(this));
});
}
}
二:fragment跳转
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="600dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/buttonF"/>
public class MainActivity extends AppCompatActivity {
public FlutterEngine flutterEngine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//-----这一块代码都是加引擎缓存用的.---------
// Instantiate a FlutterEngine.
flutterEngine = new FlutterEngine(this);
// Start executing Dart code to pre-warm the FlutterEngine.
flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
// Cache the FlutterEngine to be used by FlutterActivity.
FlutterEngineCache.getInstance().put("engine_f_m", flutterEngine);
//-------------------------------------
//跳Activity
findViewById(R.id.buttonT).setOnClickListener(v->{
// startActivity(FlutterActivity.createDefaultIntent(this));//这种没有加缓存引擎,不推荐使用
startActivity(FlutterActivity.withCachedEngine("engine_f_m").build(this));
});
//跳Fragment
findViewById(R.id.buttonF).setOnClickListener(v->{
getSupportFragmentManager().beginTransaction().add(
R.id.fragment_container,
//FlutterFragment.createDefault()//这种没有加缓存引擎,不推荐使用
FlutterFragment.withCachedEngine("engine_f_m").build()
).commit();
});
}