I spend a lot of time on YouTube every day. This is very alarming for me, and this is why I want to build a Flutter app where the time phone usage will be calculated, and after 2 hours of using the phone in a day, my YouTube will be locked for the whole day, and it will be unlocked the next day.
Looking for experts' suggestions.
Is it a realistic and feasible idea? If yes, then how may I implement?
Please share your experience and plan.
Yes, this is feasible, but the implementation differs significantly between Android and iOS due to platform restrictions.
Android ✅ Fully possible iOS ⚠️ Heavily restricted (Apple's sandboxing prevents most of this)
For a practical app, target Android first.
Flutter itself can't control other apps directly. You need native Android APIs bridged via Flutter's MethodChannel. Here's the architecture:
Use Android's UsageStatsManager API:
// Android (Kotlin) - Native side
val usageStatsManager = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val stats = usageStatsManager.queryUsageStats(
UsageStatsManager.INTERVAL_DAILY, startTime, endTime
)
// Filter for YouTube: "com.google.android.youtube"
Requires permission: android.permission.PACKAGE_USAGE_STATS (user must grant manually in Settings).
Option A: Accessibility Service (Recommended)
Your app runs a background AccessibilityService
When it detects YouTube is in the foreground, it immediately launches your lock screen on top
This is how apps like Digital Wellbeing, ActionDash, and StayFree work
class AppBlockerService : AccessibilityService() {
override fun onAccessibilityEvent(event: AccessibilityEvent) {
if (event.packageName == "com.google.android.youtube") {
if (isYouTubeLocked()) {
launchLockScreen() // Bring your Flutter app to front
}
}
}
}
Option B: Android Device Policy (MDM) — More powerful but complex
Requires enrolling as a Device Admin or using DevicePolicyManager
Can hard-block apps entirely
Overkill for personal use
// Flutter (Dart) side
class UsageService {
static const _channel = MethodChannel('com.yourapp/usage');
Future<int> getYouTubeUsageMinutes() async {
final int minutes = await _channel.invokeMethod('getYouTubeUsage');
return minutes;
}
Future<void> setYouTubeLocked(bool locked) async {
await _channel.invokeMethod('setLocked', {'locked': locked});
}
}
// Use shared_preferences to store lock state
final prefs = await SharedPreferences.getInstance();
final today = DateTime.now().toIso8601String().substring(0, 10); // "2026-04-05"
final lockedDate = prefs.getString('locked_date');
if (lockedDate != today) {
// New day — unlock YouTube
prefs.remove('locked_date');
}
PurposePackageNative bridgeflutter/services.dart (MethodChannel)Local storageshared_preferencesBackground tasksflutter_background_serviceNotificationsflutter_local_notificationsPermissionspermission_handler
Phase 1 — Usage Detection
Set up UsageStatsManager on Android native side
Bridge it to Flutter via MethodChannel
Display today's phone usage in your UI
Phase 2 — Lock Logic
Build an AccessibilityService in Kotlin
Monitor foreground app every second
If YouTube + locked → push your overlay/lock screen
Phase 3 — Flutter UI
Dashboard showing daily usage
Lock screen overlay (a full-screen Flutter route)
Daily reset logic at midnight
Phase 4 — Polish
Background service that survives app close
Prevent easy bypasses (disable uninstall during lock, PIN to change settings)
Digital Wellbeing (built into Android) — same concept
StayFree (open source portions available)
AppBlock — uses identical AccessibilityService approach
The user must manually grant Accessibility + Usage Stats permissions — Android security requirement, no way around it
A determined person can bypass it by uninstalling the app or using YouTube in a browser
iOS version would be limited to Screen Time API (read-only) — you can't programmatically lock apps on iOS without MDM enrollment
Even if you built the app, Google playstore / iOS app store would not approve the app. I can see a high chance of rejection
This is a well-trodden path — Digital Wellbeing apps use exactly this architecture. You're essentially building a lite version of that.
Muhamed Riyas M