diff options
| author | Akshay Nair <phenax5@gmail.com> | 2020-06-04 10:57:41 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2020-06-04 10:57:41 +0530 |
| commit | 173cb600667e730c337a436c9407d5b268491e9c (patch) | |
| tree | f5a9c93e1dafddc08a87b9943ea97b942229d69f | |
| parent | 41f04f9e2770761166b67fee9a2eb757b4aa1f33 (diff) | |
| download | daft-launcher-173cb600667e730c337a436c9407d5b268491e9c.tar.gz daft-launcher-173cb600667e730c337a436c9407d5b268491e9c.zip | |
Refactors context menu into its own component
| -rw-r--r-- | lib/components/AppContextMenu.dart | 71 | ||||
| -rw-r--r-- | lib/main.dart | 42 | ||||
| -rw-r--r-- | lib/pages/Apps.dart | 24 |
3 files changed, 76 insertions, 61 deletions
diff --git a/lib/components/AppContextMenu.dart b/lib/components/AppContextMenu.dart new file mode 100644 index 0000000..4dcc93a --- /dev/null +++ b/lib/components/AppContextMenu.dart @@ -0,0 +1,71 @@ +import 'package:flutter/material.dart'; +import 'package:device_apps/device_apps.dart'; +import 'package:android_intent/android_intent.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import '../data/favorites.dart'; + +class Option extends StatelessWidget { + void Function() onTap; + Widget child; + + Option({ this.child, this.onTap }): super(); + + @override + build(BuildContext ctx) { + return Column( + children: [ + ListTile( + contentPadding: const EdgeInsets.symmetric(vertical: 0.0, horizontal: 16.0), + title: child, + onTap: () { + onTap(); + Navigator.pop(ctx); + }, + ), + Divider(height: 1.0), + ], + ); + } +} + +class AppContextMenu extends StatelessWidget { + final Application app; + final bool isFavorite; + + AppContextMenu({ this.app, this.isFavorite }): super(); + + @override + Widget build(BuildContext ctx) { + ThemeData theme = Theme.of(ctx); + + return Dialog( + child: Container( + decoration: BoxDecoration( + color: theme.backgroundColor, + ), + height: 170.0, + child: ListView( + children: [ + Option(child: Text(isFavorite ? 'Remove from favorites' : 'Add to favorites'), onTap: () async { + await isFavorite ? removeFromFavorites(app) : addToFavorites(app); + }), + Option(child: Text('App Settings'), onTap: () async { + await AndroidIntent( + action: 'action_application_details_settings', + data: 'package:${app.packageName}', + ).launch(); + }), + Option(child: Text('Uninstall'), onTap: () async { + // FIXME: Doesn't work + await AndroidIntent( + action: 'action_delete', + data: 'package:${app.packageName}', + ).launch(); + }), + ], + ), + ), + ); + } +} diff --git a/lib/main.dart b/lib/main.dart index 2f5c415..c3bf787 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,11 +2,12 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:device_apps/device_apps.dart'; import 'package:android_intent/android_intent.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'pages/Home.dart'; import 'pages/Apps.dart'; +import 'components/AppContextMenu.dart'; + import 'data/config.dart'; import 'data/favorites.dart'; import 'helpers/StreamState.dart'; @@ -38,47 +39,14 @@ class MyAppState extends StreamState<MyApp> { initStateValue(dateTime); } - Widget buildOptionsMenu(BuildContext ctx, Application app) { - ThemeData theme = Theme.of(ctx); - - var isFavorite = -1 != favoriteApps.value.indexWhere((a) => a.packageName == app.packageName); - - return Dialog( - child: Container( - decoration: BoxDecoration( - color: theme.backgroundColor, - ), - height: 170.0, - child: ListView( - children: [ - Option(child: Text(isFavorite ? 'Remove from favorites' : 'Add to favorites'), onTap: () async { - await isFavorite ? removeFromFavorites(app) : addToFavorites(app); - }), - Option(child: Text('App Settings'), onTap: () async { - await AndroidIntent( - action: 'action_application_details_settings', - data: 'package:${app.packageName}', - ).launch(); - }), - Option(child: Text('Uninstall'), onTap: () async { - // FIXME: Doesn't work - await AndroidIntent( - action: 'action_delete', - data: 'package:${app.packageName}', - ).launch(); - }), - ], - ), - ), - ); - } - void openApp(Application app) { DeviceApps.openApp(app.packageName); } void openOptionsMenu(BuildContext ctx, Application app) { - showDialog(context: ctx, builder: (BuildContext ctx) => buildOptionsMenu(ctx, app)); + var isFavorite = -1 != favoriteApps.value.indexWhere((a) => a.packageName == app.packageName); + + showDialog(context: ctx, builder: (BuildContext ctx) => AppContextMenu(app: app, isFavorite: isFavorite)); } ThemeData getLightTheme() { diff --git a/lib/pages/Apps.dart b/lib/pages/Apps.dart index 8c61814..613b0e3 100644 --- a/lib/pages/Apps.dart +++ b/lib/pages/Apps.dart @@ -6,30 +6,6 @@ import '../components/SearchableAppList.dart'; import '../data/favorites.dart'; -class Option extends StatelessWidget { - void Function() onTap; - Widget child; - - Option({ this.child, this.onTap }): super(); - - @override - build(BuildContext ctx) { - return Column( - children: [ - ListTile( - contentPadding: const EdgeInsets.symmetric(vertical: 0.0, horizontal: 16.0), - title: child, - onTap: () { - onTap(); - Navigator.pop(ctx); - }, - ), - Divider(height: 1.0), - ], - ); - } -} - class AppsView extends StatelessWidget { void Function(Application) openApp; void Function(BuildContext, Application) openOptionsMenu; |
