Skip to content

Commit

Permalink
fix(and): move to background on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Dec 12, 2023
1 parent 3e2636d commit 51be8b2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.

This file was deleted.

26 changes: 26 additions & 0 deletions android/app/src/main/kotlin/com/tuihub/waiter/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}
}
56 changes: 44 additions & 12 deletions lib/main_window.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
);
}
}

0 comments on commit 51be8b2

Please sign in to comment.