diff --git a/android/app/src/main/kotlin/com/example/waitress/MainActivity.kt b/android/app/src/main/kotlin/com/example/waitress/MainActivity.kt deleted file mode 100644 index 6af3a97..0000000 --- a/android/app/src/main/kotlin/com/example/waitress/MainActivity.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.tuihub.waiter - -import io.flutter.embedding.android.FlutterActivity - -class MainActivity: FlutterActivity() { -} diff --git a/android/app/src/main/kotlin/com/tuihub/waiter/MainActivity.kt b/android/app/src/main/kotlin/com/tuihub/waiter/MainActivity.kt new file mode 100644 index 0000000..40bc4ff --- /dev/null +++ b/android/app/src/main/kotlin/com/tuihub/waiter/MainActivity.kt @@ -0,0 +1,26 @@ +package com.tuihub.waiter + +import android.os.Bundle +import io.flutter.embedding.android.FlutterActivity +import io.flutter.embedding.engine.FlutterEngine +import io.flutter.plugin.common.MethodChannel + +class MainActivity: FlutterActivity() { + // Temporary fix for https://github.com/flutter/flutter/issues/117061 + override fun configureFlutterEngine(flutterEngine: FlutterEngine) { + super.configureFlutterEngine(flutterEngine) + + val binaryMessenger = flutterEngine.dartExecutor.binaryMessenger + + MethodChannel(binaryMessenger, "com.tuihub.waiter/app_retain").apply { + setMethodCallHandler { method, result -> + if (method.method == "sendToBackground") { + moveTaskToBack(true) + result.success(null) + } else { + result.notImplemented() + } + } + } + } +} diff --git a/lib/main_window.dart b/lib/main_window.dart index ffe75af..8036d9e 100644 --- a/lib/main_window.dart +++ b/lib/main_window.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'common/platform.dart'; import 'view/specialized/theme_mode_toggle.dart'; import 'view/specialized/title_bar.dart'; @@ -10,20 +12,50 @@ class MainWindow extends StatelessWidget { @override Widget build(BuildContext context) { - return Scaffold( - body: SafeArea( - maintainBottomViewPadding: true, - child: Column( - children: [ - const TitleBar( - actions: [ThemeModeToggle()], - ), - Expanded( - child: child, - ), - ], + return _AppRetainWidget( + child: Scaffold( + body: SafeArea( + maintainBottomViewPadding: true, + child: Column( + children: [ + const TitleBar( + actions: [ThemeModeToggle()], + ), + Expanded( + child: child, + ), + ], + ), ), ), ); } } + +// Temporary fix for https://github.com/flutter/flutter/issues/117061 +// From https://github.com/ppicas/flutter-android-background +class _AppRetainWidget extends StatelessWidget { + const _AppRetainWidget({required this.child}); + + final Widget child; + + @override + Widget build(BuildContext context) { + const channel = MethodChannel('com.tuihub.waiter/app_retain'); + return WillPopScope( + onWillPop: () async { + if (PlatformHelper.isAndroidApp()) { + if (Navigator.of(context).canPop()) { + return true; + } else { + await channel.invokeMethod('sendToBackground'); + return false; + } + } else { + return true; + } + }, + child: child, + ); + } +}