move_to_bg 1.0.1
move_to_bg: ^1.0.1 copied to clipboard
A Flutter plugin to programmatically move your application to the background.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:move_to_bg/move_to_bg.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _moveToBgPlugin = MoveToBg();
Future<void> _moveToBackground() async {
try {
await _moveToBgPlugin.moveTaskToBack();
} on PlatformException catch (e) {
debugPrint('Failed to move to background: ${e.message}');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Move to Background Example'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.minimize,
size: 64,
color: Colors.blue,
),
const SizedBox(height: 24),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 32),
child: Text(
'Tap the button below to move this app to the background.',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16),
),
),
const SizedBox(height: 16),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 32),
child: Text(
'On Android: The app will move to the background but stay in your recent apps.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 12,
color: Colors.grey[600],
),
),
),
const SizedBox(height: 8),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 32),
child: Text(
'On iOS: This uses a private API and may not work on all devices.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 12,
color: Colors.grey[600],
),
),
),
const SizedBox(height: 32),
ElevatedButton.icon(
onPressed: _moveToBackground,
icon: const Icon(Icons.arrow_back),
label: const Text('Move to Background'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 12,
),
),
),
],
),
),
),
);
}
}