Skip to content

Commit 48669e8

Browse files
committed
Merge branch 'development'
0.7.4
2 parents e62a83e + c3ccd8a commit 48669e8

14 files changed

+231
-32
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ on:
88
- main
99
- development
1010
pull_request:
11-
types:
12-
- ready_for_review
13-
- synchronize
14-
- opened
11+
types: [synchronize, opened, reopened]
1512

1613
env:
1714
flutter_version: 3.10.0

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## 0.7.4 - 2023-10-30
8+
### Added
9+
- Add FocusNode parameter which is passed to underlying Focus widget
10+
- Added input as a type for an action. This type of action will give the user a text field and accept the processed text, passing it to a callback
11+
712
## 0.7.3 - 2023-05-12
813
### Changed
914
- Upgrade to Flutter 3.10.0
@@ -91,4 +96,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9196

9297
## 0.1.0 - 2021-11-03
9398
### Added
94-
- initial release
99+
- initial release

example/lib/main.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ class _MyHomePageState extends State<MyHomePage> {
9999
),
100100
],
101101
),
102+
CommandPaletteAction.input(
103+
id: "new-user",
104+
label: "New User",
105+
shortcut: ["ctrl", "shift", "n"],
106+
leading: Icon(Icons.add),
107+
onConfirmInput: (value) {
108+
ScaffoldMessenger.of(context).showSnackBar(
109+
SnackBar(content: Text('Created user: $value')));
110+
},
111+
),
102112
CommandPaletteAction.nested(
103113
id: 1, // or numbers (or really anything...)
104114
label: "Set User",

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ packages:
4747
path: ".."
4848
relative: true
4949
source: path
50-
version: "0.7.3"
50+
version: "0.7.4"
5151
fake_async:
5252
dependency: transitive
5353
description:

lib/src/command_palette.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class CommandPalette extends InheritedWidget {
2424
/// functional configuration
2525
final CommandPaletteConfig config;
2626

27+
final FocusNode? focusNode;
28+
2729
late final _CommandPaletteToggler _toggler;
2830

2931
late final CommandPaletteController _controller;
@@ -33,6 +35,7 @@ class CommandPalette extends InheritedWidget {
3335
CommandPaletteConfig? config,
3436
required this.actions,
3537
required Widget child,
38+
this.focusNode,
3639
}) : config = config ?? CommandPaletteConfig(),
3740
super(
3841
key: key,
@@ -45,6 +48,7 @@ class CommandPalette extends InheritedWidget {
4548
),
4649
config: config ?? CommandPaletteConfig(),
4750
toggler: _CommandPaletteToggler(false),
51+
focusNode: focusNode,
4852
child: child,
4953
),
5054
) {
@@ -109,14 +113,15 @@ class _CommandPaletteInner extends StatefulWidget {
109113
final CommandPaletteConfig config;
110114
final _CommandPaletteToggler toggler;
111115
final CommandPaletteController controller;
112-
116+
final FocusNode? focusNode;
113117
const _CommandPaletteInner({
114118
Key? key,
115119
required this.child,
116120
required this.actions,
117121
required this.config,
118122
required this.toggler,
119123
required this.controller,
124+
required this.focusNode,
120125
}) : super(key: key);
121126

122127
@override
@@ -239,6 +244,7 @@ class _CommandPaletteInnerState extends State<_CommandPaletteInner> {
239244
)
240245
},
241246
child: Focus(
247+
focusNode: widget.focusNode,
242248
autofocus: true,
243249
child: widget.child,
244250
),

lib/src/controller/command_palette_controller.dart

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ class CommandPaletteController extends ChangeNotifier {
8585

8686
/// Listens to [textEditingController] and is called whenever it changes.
8787
void _onTextControllerChange() {
88+
if (currentlySelectedAction?.actionType == CommandPaletteActionType.input) {
89+
return;
90+
}
91+
8892
if (_enteredQuery != textEditingController.text) {
8993
_enteredQuery = textEditingController.text;
9094
_actionsNeedRefiltered = true;
@@ -96,7 +100,8 @@ class CommandPaletteController extends ChangeNotifier {
96100
CommandPaletteAction? get currentlySelectedAction => _currentlySelectedAction;
97101
set currentlySelectedAction(CommandPaletteAction? newAction) {
98102
assert(newAction == null ||
99-
newAction.actionType == CommandPaletteActionType.nested);
103+
newAction.actionType == CommandPaletteActionType.nested ||
104+
newAction.actionType == CommandPaletteActionType.input);
100105
_currentlySelectedAction = newAction;
101106
_actionsNeedRefiltered = true;
102107
textEditingController.clear();
@@ -118,6 +123,9 @@ class CommandPaletteController extends ChangeNotifier {
118123
if (currentlySelectedAction?.actionType ==
119124
CommandPaletteActionType.nested) {
120125
filteredActions = currentlySelectedAction!.childrenActions!;
126+
} else if (currentlySelectedAction?.actionType ==
127+
CommandPaletteActionType.input) {
128+
filteredActions = [];
121129
} else {
122130
filteredActions = actions;
123131
}
@@ -193,14 +201,26 @@ class CommandPaletteController extends ChangeNotifier {
193201

194202
// nested items we set this item as the selected which in turn
195203
// will display its children.
196-
else if (action.actionType == CommandPaletteActionType.nested) {
204+
else {
197205
currentlySelectedAction = action;
198206
}
199207
}
200208

209+
void handleActionInput(BuildContext context) {
210+
_currentlySelectedAction?.onConfirmInput!(textEditingController.text);
211+
if (Navigator.of(context).canPop()) {
212+
Navigator.of(context).pop();
213+
}
214+
}
215+
201216
/// performs the action which is currently selected by [highlightedAction]
202217
void performHighlightedAction(BuildContext context) {
203-
handleAction(context, action: _filteredActionsCache[highlightedAction]);
218+
if (_currentlySelectedAction?.actionType ==
219+
CommandPaletteActionType.input) {
220+
handleActionInput(context);
221+
} else {
222+
handleAction(context, action: _filteredActionsCache[highlightedAction]);
223+
}
204224
}
205225
}
206226

lib/src/models/command_palette_action.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ enum CommandPaletteActionType {
88

99
/// Upon being selected a nested action will change the state of the command
1010
/// palette so that it only shows its children
11-
nested
11+
nested,
12+
13+
/// Upon being selected the user will be given a text field that they can enter text into and submit.
14+
/// The entered text will then be passed to the relevant callback
15+
input
1216
}
1317

1418
/// Action that is presented in the command palette. These are the things the
@@ -25,6 +29,10 @@ class CommandPaletteAction {
2529
/// Specifies what type of action this is
2630
final CommandPaletteActionType actionType;
2731

32+
/// Required when [actionType] set to [CommandPaletteActionType.input]. This
33+
/// function is called when the action is confirmed
34+
ValueChanged<String>? onConfirmInput;
35+
2836
/// Required when [actionType] set to [CommandPaletteActionType.single]. This
2937
/// function is called when the action is selected
3038
VoidCallback? onSelect;
@@ -68,13 +76,16 @@ class CommandPaletteAction {
6876
required this.actionType,
6977
this.onSelect,
7078
this.childrenActions,
79+
this.onConfirmInput,
7180
this.shortcut,
7281
this.id,
7382
this.leading,
7483
}) : assert((actionType == CommandPaletteActionType.single &&
7584
onSelect != null) ||
7685
(actionType == CommandPaletteActionType.nested &&
77-
(childrenActions?.isNotEmpty ?? false))) {
86+
(childrenActions?.isNotEmpty ?? false)) ||
87+
(actionType == CommandPaletteActionType.input &&
88+
(onConfirmInput != null))) {
7889
// give all our children "us" as a parent.
7990
if (actionType == CommandPaletteActionType.nested) {
8091
for (final child in childrenActions!) {
@@ -106,6 +117,15 @@ class CommandPaletteAction {
106117
}
107118
}
108119

120+
CommandPaletteAction.input({
121+
required this.label,
122+
this.description,
123+
required this.onConfirmInput,
124+
this.shortcut,
125+
this.id,
126+
this.leading,
127+
}) : actionType = CommandPaletteActionType.input;
128+
109129
@override
110130
bool operator ==(Object other) {
111131
if (identical(this, other)) return true;

lib/src/models/command_palette_config.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class CommandPaletteConfig {
9191
/// keyboard
9292
///
9393
/// The current instructions are:
94-
/// * enter/return: to select
94+
/// * enter/return: to select, (or if an input action) to confirm
9595
/// * up/down arrow: to navigate
9696
/// * escape: to close
9797
///

lib/src/models/matched_command_palette_action.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class MatchedCommandPaletteAction extends CommandPaletteAction {
1313
childrenActions: action.childrenActions,
1414
description: action.description,
1515
onSelect: action.onSelect,
16+
onConfirmInput: action.onConfirmInput,
1617
shortcut: action.shortcut,
1718
id: action.id,
1819
leading: action.leading,

lib/src/widgets/command_palette_instructions.dart

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:command_palette/src/controller/command_palette_controller.dart';
2+
import 'package:command_palette/src/models/command_palette_action.dart';
23
import 'package:command_palette/src/widgets/keyboard_key_icon.dart';
34
import 'package:flutter/material.dart';
45

@@ -34,21 +35,27 @@ class CommandPaletteInstructions extends StatelessWidget {
3435
color: color,
3536
),
3637
],
37-
instruction: "to select",
38-
),
39-
_KeyboardInstruction(
40-
icons: [
41-
KeyboardKeyIcon(
42-
icon: Icons.arrow_upward,
43-
color: color,
44-
),
45-
KeyboardKeyIcon(
46-
icon: Icons.arrow_downward,
47-
color: color,
48-
),
49-
],
50-
instruction: "to navigate",
38+
instruction:
39+
(controller.currentlySelectedAction?.actionType ==
40+
CommandPaletteActionType.input)
41+
? "to confirm"
42+
: "to select",
5143
),
44+
if (controller.currentlySelectedAction?.actionType !=
45+
CommandPaletteActionType.input)
46+
_KeyboardInstruction(
47+
icons: [
48+
KeyboardKeyIcon(
49+
icon: Icons.arrow_upward,
50+
color: color,
51+
),
52+
KeyboardKeyIcon(
53+
icon: Icons.arrow_downward,
54+
color: color,
55+
),
56+
],
57+
instruction: "to navigate",
58+
),
5259
if (controller.currentlySelectedAction != null)
5360
_KeyboardInstruction(
5461
icons: [

0 commit comments

Comments
 (0)