您好,登錄后才能下訂單哦!
FlutterSDK升級后,我們發現不能按照原來的方式集成到已有Android項目中了,因為沒有了Flutter這個類,通過閱讀源碼我找到了新的使用方式。
一、創建Flutter Module
在已有Android工程中集成flutter,可以使用AndroidStudio的new Flutter Module實現,方便快捷。
找到FlutterModule,一連串的next操作即可
創建完成后我們的項目中會多一個叫做flutter的module
并且在項目的setting.gradle文件中會自動添加如下幾行代碼,
setBinding(new Binding([gradle:this])) evaluate( new File( settingsDir, 'flutter_module/.android/include_flutter.groovy' ) ) include ':flutter_module'
最后我們只需在app的build.gradle中引用這個flutter模塊即可
implementation project(path: ':flutter')
二、使用Flutter
兩種使用方式,FlutterView和FlutterFragment。
首先在Application中初始化
public class MVApplication extends Application { private static MVApplication instance; public static MVApplication getInstance(){ return instance; } @Override public void onCreate() { super.onCreate(); FlutterMain.Settings settings=new FlutterMain.Settings(); settings.setLogTag("MyFlutter"); FlutterMain.startInitialization(this,settings); String[] args = {"info", "data"}; FlutterMain.ensureInitializationComplete(this,args); instance=this; } }
1、使用FlutterFragment
新版本的FlutterSDK不再支持已經沒有Flutter類,不再支持諸如Flutter.createView()、Flutter.createFragment()等用法,通過閱讀其源碼我們可以找到FlutterFragment的新用法,代碼如下:
public class MyFlutterActivity extends FragmentActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.flutter_layout); //FlutterFragment fragment=new FlutterFragment(); FlutterFragment fragment=FlutterFragment.withNewEngine().initialRoute("home").build(); getSupportFragmentManager().beginTransaction().add(R.id.flutter_container, fragment).commit(); } }
代碼中FlutterFragment的創建也可以直接使用默認的構造方法,這樣加載main.dart中的默認路由,即''/'',給出的示例代碼中我們通過調用FlutterFragment.withNewEngine().initialRoute("home").build()創建了新的NewEngineFragmentBuilder來構建指定路由的FlutterFragment。
2、使用FlutterView
FlutterView的創建同樣不能使用老版本的方法了,新版本的使用方法代碼如下:
public class MyFlutterActivity extends FragmentActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.flutter_layout); FrameLayout frameLayout=findViewById(R.id.flutter_container); ProgressBar progress=findViewById(R.id.progress); //創建FlutterView FlutterView flutterView=new FlutterView(this); //創建FlutterView首幀渲染完成監聽 flutterView.addFirstFrameListener(new FlutterView.FirstFrameListener() { @Override public void onFirstFrame() { //隱藏進度條,顯示FlutterView progress.setVisibility(View.GONE); frameLayout.setVisibility(View.VISIBLE); } }); //創建dart代碼執行器 DartExecutor executor=flutterView.getDartExecutor(); //執行main.dart中的main函數 executor.executeDartEntrypoint( new DartExecutor.DartEntrypoint(FlutterMain.findAppBundlePath(), "main")); //將FlutterView添加到布局中 ViewGroup.LayoutParams layoutParams=new LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); frameLayout.addView(flutterView); } }
布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/flutter_container" android:visibility="invisible" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> <ProgressBar android:id="@+id/progress" android:layout_width="45dp" android:layout_height="45dp" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:visibility="visible" > </ProgressBar> </RelativeLayout>
效果如下圖:
到此這篇關于新版Flutter集成到已有Android項目的實現的文章就介紹到這了,更多相關Flutter集成到已有Android項目內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。