Add flutter-mimize.md
This commit is contained in:
parent
72244022bc
commit
a45a6fb706
|
|
@ -0,0 +1,275 @@
|
|||
Bagus! Dengan **Riverpod**, Anda tetap bisa menggunakan `WillPopScope` atau `PopScope` (versi terbaru) dengan cara yang sama. Tidak ada perbedaan signifikan, karena ini murni masalah widget Flutter, bukan state management.
|
||||
|
||||
Namun, ada cara yang lebih **clean** dan **reusable** menggunakan Riverpod, yaitu dengan membuat **Provider** untuk menangani logika minimize.
|
||||
|
||||
---
|
||||
|
||||
## Cara Terbaik dengan Riverpod + PopScope (Flutter 3.16+)
|
||||
|
||||
### 1. Buat Provider untuk Minimize Logic
|
||||
|
||||
```dart
|
||||
// providers/minimize_provider.dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
final minimizeProvider = Provider<void>((ref) {
|
||||
// Kosongkan dulu, kita akan panggil langsung dari fungsi
|
||||
});
|
||||
|
||||
// Buat fungsi global atau static
|
||||
void minimizeApp() {
|
||||
// Untuk Android
|
||||
const platform = MethodChannel('com.example.app/minimize');
|
||||
platform.invokeMethod('minimize');
|
||||
|
||||
// Atau jika pakai android_intent_plus:
|
||||
// const AndroidIntent intent = AndroidIntent(
|
||||
// action: 'android.intent.action.MAIN',
|
||||
// category: ['android.intent.category.HOME'],
|
||||
// );
|
||||
// intent.launch();
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. Implementasi di Halaman Utama dengan Riverpod
|
||||
|
||||
```dart
|
||||
// home_page.dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class HomePage extends ConsumerWidget {
|
||||
const HomePage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
// Gunakan PopScope (pengganti WillPopScope di Flutter 3.16+)
|
||||
return PopScope(
|
||||
canPop: false, // Mencegah pop default
|
||||
onPopInvoked: (bool didPop) async {
|
||||
if (!didPop) {
|
||||
// Panggil fungsi minimize
|
||||
await minimizeApp();
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Halaman Utama'),
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
),
|
||||
body: const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.home, size: 64),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'Tekan tombol Back untuk minimize',
|
||||
style: TextStyle(fontSize: 18),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'(Aplikasi masuk background)',
|
||||
style: TextStyle(fontSize: 14, color: Colors.grey),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Fungsi minimize dengan MethodChannel
|
||||
Future<void> minimizeApp() async {
|
||||
try {
|
||||
const platform = MethodChannel('com.example.app/minimize');
|
||||
await platform.invokeMethod('minimizeApp');
|
||||
} catch (e) {
|
||||
debugPrint('Error minimizing app: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. Implementasi Native Android (Kotlin)
|
||||
|
||||
File: `android/app/src/main/kotlin/com/example/yourapp/MainActivity.kt`
|
||||
|
||||
```kotlin
|
||||
package com.example.yourapp
|
||||
|
||||
import android.os.Bundle
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
|
||||
class MainActivity: FlutterActivity() {
|
||||
private val CHANNEL = "com.example.app/minimize"
|
||||
|
||||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
|
||||
.setMethodCallHandler { call, result ->
|
||||
if (call.method == "minimizeApp") {
|
||||
// Kunci: pindahkan task ke background
|
||||
moveTaskToBack(true)
|
||||
result.success(null)
|
||||
} else {
|
||||
result.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 4. Alternatif: Menggunakan `android_intent_plus` (Tanpa Native Code)
|
||||
|
||||
Jika Anda tidak ingin mengedit file native, gunakan package `android_intent_plus`:
|
||||
|
||||
```yaml
|
||||
dependencies:
|
||||
android_intent_plus: ^5.0.0
|
||||
```
|
||||
|
||||
```dart
|
||||
// home_page.dart
|
||||
import 'package:android_intent_plus/android_intent.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
class HomePage extends ConsumerWidget {
|
||||
const HomePage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
onPopInvoked: (bool didPop) async {
|
||||
if (!didPop) {
|
||||
// Minimize ke home screen
|
||||
const AndroidIntent intent = AndroidIntent(
|
||||
action: 'android.intent.action.MAIN',
|
||||
category: <String>['android.intent.category.HOME'],
|
||||
);
|
||||
await intent.launch();
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(title: const Text('Halaman Utama')),
|
||||
body: const Center(
|
||||
child: Text('Tekan Back untuk minimize'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. Best Practice: State Management dengan Riverpod
|
||||
|
||||
Jika Anda ingin menangani status minimize di provider (misalnya untuk logging atau analytics):
|
||||
|
||||
```dart
|
||||
// providers/app_provider.dart
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
final appStateProvider = StateProvider<bool>((ref) => false);
|
||||
|
||||
final minimizeControllerProvider = Provider((ref) {
|
||||
return MinimizeController(ref);
|
||||
});
|
||||
|
||||
class MinimizeController {
|
||||
final Ref ref;
|
||||
|
||||
MinimizeController(this.ref);
|
||||
|
||||
Future<void> minimize() async {
|
||||
// Update state jika perlu
|
||||
ref.read(appStateProvider.notifier).state = true;
|
||||
|
||||
// Panggil fungsi minimize native
|
||||
const platform = MethodChannel('com.example.app/minimize');
|
||||
await platform.invokeMethod('minimizeApp');
|
||||
|
||||
// Reset state setelah minimize
|
||||
ref.read(appStateProvider.notifier).state = false;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Penggunaan di halaman:**
|
||||
```dart
|
||||
class HomePage extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final minimizeController = ref.watch(minimizeControllerProvider);
|
||||
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
onPopInvoked: (bool didPop) async {
|
||||
if (!didPop) {
|
||||
await minimizeController.minimize();
|
||||
}
|
||||
},
|
||||
child: Scaffold(...),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### ⚠️ Perbedaan `WillPopScope` vs `PopScope`
|
||||
|
||||
| Fitur | WillPopScope (Lama) | PopScope (Baru, Flutter 3.16+) |
|
||||
|-------|-------------------|-------------------------------|
|
||||
| Parameter | `onWillPop: () async => bool` | `canPop: bool` + `onPopInvoked: (bool didPop) => void` |
|
||||
| Rekomendasi | Masih bisa dipakai | **Direkomendasikan** untuk project baru |
|
||||
|
||||
---
|
||||
|
||||
### Untuk iOS (Catatan)
|
||||
|
||||
Di iOS, Anda tidak bisa meminimalkan aplikasi secara programatis karena batasan Apple. Solusi untuk iOS:
|
||||
|
||||
```dart
|
||||
import 'dart:io';
|
||||
|
||||
// Di dalam PopScope:
|
||||
onPopInvoked: (bool didPop) async {
|
||||
if (!didPop) {
|
||||
if (Platform.isAndroid) {
|
||||
await minimizeApp(); // Panggil minimize untuk Android
|
||||
} else {
|
||||
// Untuk iOS: tetap di halaman atau beri feedback
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Swipe ke atas untuk keluar aplikasi')),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Kesimpulan
|
||||
|
||||
Dengan Riverpod, Anda tetap menggunakan `PopScope` seperti biasa. Yang membedakan adalah:
|
||||
- Logika minimize bisa ditaruh di **Provider** jika ingin reusable
|
||||
- Tetap menggunakan **MethodChannel** atau `android_intent_plus` untuk native code
|
||||
- `PopScope` dengan `canPop: false` mencegah navigasi ke belakang
|
||||
|
||||
**Rekomendasi:** Gunakan `PopScope` + MethodChannel (Cara 3) untuk hasil paling akurat (aplikasi masuk background, bukan ke home screen).
|
||||
Loading…
Reference in New Issue