前言
只有Android手機(jī)上才會(huì)有返回APP的操作简肴,所以以下所說的是針對(duì)Flutter的Android平臺(tái)上開發(fā)晃听,現(xiàn)在flutter的版本已經(jīng)升級(jí)到1.22.0以上了,很多舊方法都不能用了,網(wǎng)絡(luò)上有很多案例都是針對(duì)舊的flutter版本能扒,所以我這邊就做過新版本的該功能佣渴,方便大家參考一下
一、編寫回到桌面方法的插件
這是新舊flutter編寫返回到桌面的最大區(qū)別初斑,以前可以在MainActivity直接編寫能用的插件辛润,但是現(xiàn)在不行了,很多方法都沒有了见秤,因此砂竖,我們需要新啟一個(gè)插件 app_util_plugin,編寫網(wǎng)上有很多教程鹃答,這里就不在贅述了
在這里插入圖片描述
插件模塊
在這里插入圖片描述
核心代碼乎澄,返回桌面的方法navigateToSystemHome
package com.app.app_util_plugin;
import android.content.Context;
import android.content.Intent;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/** AppUtilPlugin */
public class AppUtilPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;
private Context mContext;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
mContext = flutterPluginBinding.getApplicationContext();
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "app_util_plugin");
channel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else if (call.method.equals("checkUpdateApp")) {
} else if (call.method.equals("navigateToSystemHome")) {//返回系統(tǒng)首頁
Intent intent = new Intent();// 創(chuàng)建Intent對(duì)象
intent.setAction(Intent.ACTION_MAIN);// 設(shè)置Intent動(dòng)作
intent.addCategory(Intent.CATEGORY_HOME);// 設(shè)置Intent種類
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//標(biāo)記
mContext.startActivity(intent);
} else {
result.notImplemented();
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
二、編寫通過MethodChannel調(diào)用插件中方法
編寫插件調(diào)用方法
import 'dart:async';
import 'package:flutter/services.dart';
class AppUtilPlugin {
static const MethodChannel _channel =
const MethodChannel('app_util_plugin');
//返回系統(tǒng)首頁
static void navigateToSystemHome() async {
await _channel.invokeMethod('navigateToSystemHome');
}
}
三测摔、使用返回桌面的方法
在main.dart監(jiān)聽系統(tǒng)返回鍵置济,當(dāng)時(shí)調(diào)用系統(tǒng)返回鍵時(shí),將它攔截下來锋八,再調(diào)用插件中的返回桌面的方法浙于,這樣就可以不用退出APP,就可以回到桌面了
MaterialApp(
home: WillPopScope(
onWillPop: () async {
AppUtilPlugin.navigateToSystemHome();//返回到系統(tǒng)首頁
return false;
},
child: HomeMainPage(),
),
navigatorKey: Constant.navKey,
navigatorObservers: [MyApp.routeObserver],
onGenerateRoute: onGenerateRoute,
)
總結(jié)
整個(gè)過程很簡(jiǎn)單挟纱,希望可以幫到flutter 的初學(xué)者們