Saya menghabiskan waktu berhari-hari untuk mencari tahu cara menambahkan Flutter UI ke Aplikasi Android saya yang ada. Tantangan terbesar adalah membuat MethodChannel bekerja dengan FlutterActivity dipanggil dari MainActivity. Saya tahu ini sedikit berbeda dari pertanyaan yang diajukan di sini, tetapi postingan ini dikembalikan ketika saya mencari 'Android FlutterActivity MethodChannel'. Setelah melalui banyak sumber tentang cara melakukan ini, saya akhirnya menemukan solusi saya di sini:
https://github.com/flutter/samples/tree/master/add_to_app/android_using_plugin/app/src/main/java/dev/flutter/ contoh / android menggunakanplugin
Awalnya, di Android Studio, dengan aplikasi yang ada dibuka, saya mengetuk File, New, New Module, Flutter Module. Saya menerima kesalahan dan harus melakukan langkah-langkah manual.
Tujuan saya adalah meluncurkan FlutterActivity (membuka main.dart di flutter_module) di MainActivity - onCreate, kemudian mengembangkan 'layar' yang meningkatkan sebanyak mungkin kode Flutter asli sebanyak mungkin, dengan panggilan Platform terbatas menggunakan MethodChannel. Saat saya mengembangkan kode Flutter pengganti, saya akan terus mengomentari Kode Android yang ada.
Inilah yang akhirnya berhasil untuk saya:
../App_Project/Android/Existing_Android_App/settings.gradle
include ':app'
setBinding(new Binding([gradle: this]))
evaluate(new File(settingsDir.parentFile, '../flutter_module/.android/include_flutter.groovy'))
include ':flutter_module’
project(':flutter_module’).projectDir = new File('../../flutter_module’)
rootProject.name=‘existing_android_app’
../App_Project/Android/Existing_Android_App/app/build.gradle
dependencies {
…
implementation project(':flutter')
}
../App_Project/Android/Existing_Android_App/app/src/main/AndroidManifest.xml
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize" />
../App_Project/Android/Existing_Android_App/app/src/main/java/com/existing_android_app/MainActivity.java
package com.existing_android_app;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class MainActivity extends AppCompatActivity {
final String ENGINE_ID = "1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FlutterEngine flutterEngine = new FlutterEngine(this);
flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
FlutterEngineCache.getInstance().put(ENGINE_ID, flutterEngine);
MethodChannel channel = new MethodChannel(flutterEngine.getDartExecutor(), "com.existing_android_app/myMethodChannel");
channel.setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
String url = call.argument("url");
if (call.method.equals("openBrowser")) {
openBrowser(url);
}
else {
result.notImplemented();
}
}
});
startActivity(FlutterActivity.withCachedEngine(ENGINE_ID).build(this));
}
void openBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
this.startActivity(intent);
}
}
../App_Project/flutter_module/lib/home_page.dart
class AppHomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<AppHomePage> {
static const platform = const MethodChannel(‘com.existing_android_app/myMethodChannel’);
Future<void> _openBrowser() async {
try {
final int result = await platform.invokeMethod('openBrowser', <String, String> { 'url': "http://bing.com” });
}
catch (e) {
print('***** _openBrowser error: ' + e.toString());
}
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: CustomAppBar(),
body: Column(
children: <Widget>[
RaisedButton(
label: Text('Search',
style: TextStyle(fontSize: 18.0),
),
onPressed: () { _openBrowser(); },
) // RaisedButton.icon
], // Widget
) // Column
) // Scaffold
); // SafeArea
}