Skip to content

Commit

Permalink
icon size fix, arrow nav and orientation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalraja committed Mar 6, 2024
1 parent 063cd04 commit c7a6d64
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
58 changes: 56 additions & 2 deletions lib/app_drawer_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:math';

import 'package:collection/collection.dart' show IterableExtension;
import 'package:flutter/material.dart';
Expand All @@ -21,7 +22,12 @@ class AppDrawerState extends State<AppDrawerPage> with WidgetsBindingObserver {
final DBProvider _db = DBProvider();
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
new GlobalKey<RefreshIndicatorState>();
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();
final FocusNode _focusNode = FocusNode(onKey: (node, event) {
return KeyEventResult.handled;
},);

List<int> appLoc = [-1, 0];
List<AppItem>? _apps;
List<CategoryItem>? _categories;
CategoryItem? _selectedCategory;
Expand All @@ -31,6 +37,7 @@ class AppDrawerState extends State<AppDrawerPage> with WidgetsBindingObserver {
Completer<Null>? _isLoading;
Completer<Null>? _isCategorizing;
AppSort? _appSort;
AppItem? selectedApp;

bool isSelecting = false;
int? shortcutSupport = -1;
Expand Down Expand Up @@ -61,6 +68,8 @@ class AppDrawerState extends State<AppDrawerPage> with WidgetsBindingObserver {
setCategory(CategoryItem? category) async {
setState(() {
_selectedCategory = category;
appLoc[0] = -1;
appLoc[1] = 0;
});

debugPrint(
Expand Down Expand Up @@ -221,6 +230,7 @@ class AppDrawerState extends State<AppDrawerPage> with WidgetsBindingObserver {
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_focusNode.dispose();
_db.close();
super.dispose();
}
Expand Down Expand Up @@ -255,6 +265,47 @@ class AppDrawerState extends State<AppDrawerPage> with WidgetsBindingObserver {
return _selectedCategory != null ? _selectedCategory!.name : widget.title;
}

void _handleKeyEvent(RawKeyEvent event) {
if (_scaffoldKey.currentState != null && event.runtimeType.toString() == "RawKeyDownEvent") {
var orient = MediaQuery.of(context).orientation;
var maxX = orient == Orientation.portrait ? 4 : 9;
var maxY = (_apps!.length / maxX).floor() - 1;
var state = _scaffoldKey.currentState!;
var key = event.logicalKey.keyLabel;
if (key == "Arrow Left") {
if (appLoc[0] <= 0) {
appLoc[0] = 0;
state.openDrawer();
} else {
appLoc[0]--;
}
} else if (key == "Arrow Right") {
if (appLoc[0] >= maxX) {
appLoc[0] = maxX;
state.openEndDrawer();
} else {
appLoc[0]++;
}
} else if (key == "Arrow Down") {
if (appLoc[0] == -1)
appLoc[0] = 0;
if (appLoc[1] < maxY - 1)
appLoc[1]++;
} else if (key == "Arrow Up") {
if (appLoc[1] > 0)
appLoc[1]--;
} else if (key == "Enter" && selectedApp != null) {
selectedApp!.openApp();
selectedApp = null;
appLoc[0] = -1;
appLoc[1] = 0;
}
setState(() {
appLoc = appLoc;
});
}
}

void _showPrivacyAcceptDialog() {
showDialog(
context: context,
Expand Down Expand Up @@ -311,6 +362,7 @@ class AppDrawerState extends State<AppDrawerPage> with WidgetsBindingObserver {
return new Future(() => allow);
},
child: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text(appBarTitle()!),
actions: <Widget>[
Expand Down Expand Up @@ -448,12 +500,14 @@ class AppDrawerState extends State<AppDrawerPage> with WidgetsBindingObserver {
),
))
: null,
body: RefreshIndicator(
body: RawKeyboardListener(
onKey: _handleKeyEvent,
child: RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _loadApps,
child: AppGrid(
apps: filteredApps(),
)),
)), focusNode: _focusNode,autofocus: true,),
floatingActionButton: Container(
padding: MediaQuery.of(context).viewInsets.bottom > 0
? EdgeInsets.only(bottom: 42.0)
Expand Down
13 changes: 10 additions & 3 deletions lib/app_grid.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:applauncher/app_model.dart';
import 'package:applauncher/app_icon.dart';
Expand All @@ -22,17 +24,22 @@ class AppGrid extends StatelessWidget {

@override
Widget build(BuildContext context) {
return GridView.builder(
return OrientationBuilder(builder: (context, orientation) {
var xCount = 5 * (orientation == Orientation.portrait ? 1 : 2);
return GridView.builder(
primary: true,
itemCount: apps!.length,
padding: EdgeInsets.all(getDouble('padding', 10.0)!),
shrinkWrap: false,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: getDouble('horizontal_space', 2.5)!,
crossAxisCount: getInt('item_count', 5)!,
crossAxisCount: getInt('item_count', xCount)!,
childAspectRatio: getDouble('aspect_ratio', .8)!),
itemBuilder: (BuildContext context, int index) {
return AppIcon(appItem: apps![index]);
var x = index / xCount;
x = x - x.truncate();
return AppIcon(appItem: apps![index], coord: Point((x * xCount).round(), (index / xCount).floor()),);
});
});
}
}
17 changes: 12 additions & 5 deletions lib/app_icon.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:android_intent/android_intent.dart';
import 'package:applauncher/app_model.dart';
import 'package:applauncher/store.dart';

class AppIcon extends StatelessWidget with Store {
AppIcon({this.appItem});
AppIcon({this.appItem, this.coord});

final AppItem? appItem;
final Point? coord;

// Used for displaying an actual app and it's image and handle the onTap event
@override
Widget build(BuildContext context) {
var isDarkMode = Preference.getBool(settingsDarkMode);
var selected = coord != null && appDrawerState!.appLoc[0] == coord!.x && appDrawerState!.appLoc[1] == coord!.y;
if (selected) {
appDrawerState!.selectedApp = appItem;
}
return GestureDetector(
onTap: () {
if (appDrawerState!.isSelecting) {
Expand Down Expand Up @@ -84,12 +91,12 @@ class AppIcon extends StatelessWidget with Store {
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Container(
decoration: appDrawerState!.isSelecting && appItem!.selected
Expanded(child:Container(
decoration: appDrawerState!.isSelecting && appItem!.selected || selected
? BoxDecoration(color: Colors.blueGrey)
: null,
padding: EdgeInsets.all(10.0),
child: Image.memory(appItem!.icon!, fit: BoxFit.fitHeight)),
padding: EdgeInsets.all(10.0),
child: Image.memory(appItem!.icon!, fit: BoxFit.fill))),
Text(
appItem!.name!,
style: TextStyle(
Expand Down

0 comments on commit c7a6d64

Please sign in to comment.