Skip to content

Commit 99b2f8b

Browse files
author
deimos
committed
programmatically open menu
1 parent 9ab80f4 commit 99b2f8b

17 files changed

+536
-205
lines changed

.idea/workspace.xml

+21-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.vscode/launch.json

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "star_menu",
9+
"request": "launch",
10+
"type": "dart"
11+
},
12+
{
13+
"name": "main2",
14+
"request": "launch",
15+
"type": "dart",
16+
"cwd": "example",
17+
"args": ["-t", "lib/main2.dart"],
18+
"flutterMode": "debug",
19+
"program": "lib/main2.dart"
20+
},
21+
{
22+
"name": "star_menu (profile mode)",
23+
"request": "launch",
24+
"type": "dart",
25+
"flutterMode": "profile"
26+
},
27+
{
28+
"name": "star_menu (release mode)",
29+
"request": "launch",
30+
"type": "dart",
31+
"flutterMode": "release"
32+
},
33+
{
34+
"name": "example",
35+
"cwd": "example",
36+
"request": "launch",
37+
"type": "dart"
38+
},
39+
{
40+
"name": "example (profile mode)",
41+
"cwd": "example",
42+
"request": "launch",
43+
"type": "dart",
44+
"flutterMode": "profile"
45+
},
46+
{
47+
"name": "example (release mode)",
48+
"cwd": "example",
49+
"request": "launch",
50+
"type": "dart",
51+
"flutterMode": "release"
52+
}
53+
]
54+
}

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cmake.configureOnOpen": false
3+
}

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [3.1.0] - 26 Oct 2022
2+
* it's now possible to open a menu programmatically with [StarMenuController].
3+
* it's now possible to open a menu programmatically by passing [parentContext] to [StarMenu].
4+
* added [useTouchAsCenter] to [StarMenuParameters] to use the touch position as the menu center.
5+
16
## [3.0.0+2] - 23 Sep 2022
27
* added a new main.dart example
38
* added `onHoverScale` in `StarMenuParameters` to scale items when mouse hover (desktop only)

README.md

+130-45
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Easy and Fast Way to build Context Menus
1313
![GitHub stars](https://img.shields.io/github/stars/alnitak/flutter_star_menu?style=flat-square)
1414
![GitHub license](https://img.shields.io/github/license/alnitak/flutter_star_menu?style=flat-square)
1515

16+
![Image](https://github.com/alnitak/flutter_star_menu/blob/master/images/dont_buy_me_coffe.png)
17+
Instead, a star would be wonderful! ;)
18+
1619
A simple way to attach a popup menu to any widget with any widget as menu entries.
1720
Menu entry widgets can bind a sub-menu with different shapes.
1821
Multiple ways to fine-tune animation and position.
@@ -39,7 +42,7 @@ Using the package is pretty simple:
3942
- install the package with `flutter pub add star_menu`
4043
- feed the `items` parameter with the menu entry widgets list
4144
- set the `params` with `StarMenuParameters`
42-
- set the `child` with a widget you like to press to open the menu
45+
- set the `child` with a widget you wish to press to open the menu
4346

4447
```dart
4548
StarMenu(
@@ -100,36 +103,108 @@ FloatingActionButton(
100103
onPressed: () {print('FloatingActionButton tapped');},
101104
child: Icon(Icons.looks_one),
102105
).addStarMenu(
103-
context,
104-
entries,
106+
items,
105107
StarMenuParameters(),
108+
controller,
106109
onItemTapped: (index, controller) {}),
107110
```
111+
<br/>
112+
<br/>
113+
114+
## ***Programmatically open menu***
115+
There are two ways to programmatically open a menu, for example when pressing a button:
116+
117+
- **if you already have a StarMenu in the tree, add [StarMenuController] to it:**
118+
```dart
119+
StarMenuController starMenuController = StarMenuController();
120+
121+
StarMenu(
122+
params: StarMenuParameters(),
123+
controller: starMenuController,
124+
items: entries,
125+
child: ...,
126+
)
127+
```
128+
then in your button:
129+
```dart
130+
ElevatedButton(
131+
onPressed: () => starMenuController.openMenu!(),
132+
child: ...,
133+
),
134+
```
108135

109-
### StarMenuParameters
136+
- **if you want to dinamically create a StartMenu for another widget, assign a GlobalKey to your widget:**
137+
```dart
138+
GlobalKey containerKey = GlobalKey();
139+
...
140+
Container(
141+
key: containerKey,
142+
width: 40,
143+
height: 40,
144+
),
145+
```
146+
then in your button call *StarMenuOverlay.displayStarMenu* with the widget context:
147+
```dart
148+
ElevatedButton(
149+
onPressed: () {
150+
StarMenuOverlay.displayStarMenu(
151+
containerKey.currentContext!,
152+
StarMenu(
153+
params: StarMenuParameters(),
154+
items: entries,
155+
parentContext: containerKey.currentContext,
156+
),
157+
);
158+
},
159+
child: ...
160+
),
161+
```
110162

163+
<br/>
164+
<br/>
165+
166+
### ***StarMenu***
167+
Only [items] or [lazyItems] can be passed, not both.
168+
|Name|Type|Defaults|Description|
169+
|:-------|:----------|:----------|:-----------|
170+
|***params***|class|StarMenuParameters|See below.|
171+
|***items***|List<Widget>?|-|Widget items entry list.|
172+
|***lazyItems***|Future<List<Widget>> Function()?|-|Function to build dynamically items list whenever the menu open occurs.|
173+
|***child***|Widget|-|Widget that triggers the opening of the menu. Only [child] or [parentContext] is allowed|
174+
|***parentContext***|BuildContext|-|Widget that triggers the opening of the menu. Only [child] or [parentContext] is allowed|
175+
|***controller***|StarMenuController|-|context of the Widget where the menu will be opened. Only [child] or [parentContext] is allowed|
176+
|***onStateChanged***|Function(MenuState state)?|-|Return current menu state.|
177+
178+
<br/>
179+
<br/>
180+
181+
### ***StarMenuParameters***
111182
Class to define all the parameters for the shape, animation and menu behavior.
112183

113184
|Name|Type|Defaults|Description|
114185
|:-------|:----------|:----------|:-----------|
115-
|*shape*|enum|MenuShape.circle|Menu shape kind. Could be [MenuShape.circle], [MenuShape.linear], [MenuShape.grid].|
116-
|*boundaryBackground*|class|-|See below.|
117-
|*linearShapeParams*|class|-|See below.|
118-
|*circleShapeParams*|class|-|See below.|
119-
|*gridShapeParams*|class|-|See below.|
120-
|*backgroundParams*|class|-|See below.|
121-
|*openDurationMs*|int|400|Open animation duration ms.|
122-
|*closeDurationMs*|int|150|Close animation duration ms.|
123-
|*rotateItemsAnimationAngle*|double|0.0|Starting rotation angle of the items that will reach 0 DEG when animation ends.|
124-
|*startItemScaleAnimation*|double|0.3|Starting scale of the items that will reach 1 when animation ends.|
125-
|*centerOffset*|Offset|Offset.zero|Shift offset of menu center from the center of parent widget.|
126-
|*useScreenCenter*|bool|false|Use the screen center instead of parent widget center.|
127-
|*checkItemsScreenBoundaries*|bool|false|Checks if the whole menu boundaries exceed screen edges, if so set it in place to be all visible.|
128-
|*checkMenuScreenBoundaries*|bool|true|Checks if items exceed screen edges, if so set them in place to be visible.|
129-
|*animationCurve*|Curve|Curves.fastOutSlowIn|Animation curve kind to use.|
130-
|*useLongPress*|bool|false|Use long press instead of a tap to open the menu.|
131-
|*longPressDuration*|Duration|500 ms|The timing to trigger long press.|
132-
|*onHoverScale*|double|1.0|Scale item when mouse is hover (desktop only)|
186+
|***shape***|enum|MenuShape.circle|Menu shape kind. Could be [MenuShape.circle], [MenuShape.linear], [MenuShape.grid].|
187+
|***boundaryBackground***|class|-|See below.|
188+
|***linearShapeParams***|class|-|See below.|
189+
|***circleShapeParams***|class|-|See below.|
190+
|***gridShapeParams***|class|-|See below.|
191+
|***backgroundParams***|class|-|See below.|
192+
|***openDurationMs***|int|400|Open animation duration ms.|
193+
|***closeDurationMs***|int|150|Close animation duration ms.|
194+
|***rotateItemsAnimationAngle***|double|0.0|Starting rotation angle of the items that will reach 0 DEG when animation ends.|
195+
|***startItemScaleAnimation***|double|0.3|Starting scale of the items that will reach 1 when animation ends.|
196+
|***centerOffset***|Offset|Offset.zero|Shift offset of menu center from the center of parent widget.|
197+
|***useScreenCenter***|bool|false|Use the screen center instead of parent widget center.|
198+
|***useTouchAsCenter***|bool|false|Use the touch coordinate as the menu center.|
199+
|***checkItemsScreenBoundaries***|bool|false|Checks if the whole menu boundaries exceed screen edges, if so set it in place to be all visible.|
200+
|***checkMenuScreenBoundaries***|bool|true|Checks if items exceed screen edges, if so set them in place to be visible.|
201+
|***animationCurve***|Curve|Curves.fastOutSlowIn|Animation curve kind to use.|
202+
|***useLongPress***|bool|false|Use long press instead of a tap to open the menu.|
203+
|***longPressDuration***|Duration|500 ms|The timing to trigger long press.|
204+
|***onHoverScale***|double|1.0|Scale item when mouse is hover (desktop only)|
205+
206+
<br/>
207+
<br/>
133208

134209
There are some ***StarMenuParameters*** factory presets with which you can set *StarMenu.params*
135210

@@ -154,54 +229,64 @@ There are some ***StarMenuParameters*** factory presets with which you can set *
154229

155230

156231
---
232+
<br/>
233+
<br/>
157234

158-
### BoundaryBackground
235+
### ***BoundaryBackground***
159236

160237
|Name|Type|Defaults|Description|
161238
|:-------|:----------|:----------|:-----------|
162-
|*color*|Color|0x80000000|color of the boundary background.|
163-
|*padding*|EdgeInsets|EdgeInsets.all(8.0)|Padding of the boundary background.|
164-
|*decoration*|Decoration|BorderRadius.circular(8)| background Container widget decoration.|
239+
|***color***|Color|0x80000000|color of the boundary background.|
240+
|***padding***|EdgeInsets|EdgeInsets.all(8.0)|Padding of the boundary background.|
241+
|***decoration***|Decoration|BorderRadius.circular(8)| background Container widget decoration.|
165242

166243
---
244+
<br/>
245+
<br/>
167246

168-
### LinearShapeParams
247+
### ***LinearShapeParams***
169248

170249
|Name|Type|Defaults|Description|
171250
|:-------|:----------|:----------|:-----------|
172-
|*angle*|double|90.0|Degree angle. Anticlockwise with 0° on 3 o'clock.|
173-
|*space*|double|0.0|Space between items.|
174-
|*alignment*|LinearAlignment|center| *left*, *center*, *right*, *top*, *bottom*. Useful when the linear shape is vertical or horizontal.|
251+
|***angle***|double|90.0|Degree angle. Anticlockwise with 0° on 3 o'clock.|
252+
|***space***|double|0.0|Space between items.|
253+
|***alignment***|LinearAlignment|center| *left*, *center*, *right*, *top*, *bottom*. Useful when the linear shape is vertical or horizontal.|
175254

176255
---
256+
<br/>
257+
<br/>
177258

178-
### CircleShapeParams
259+
### ***CircleShapeParams***
179260

180261
|Name|Type|Defaults|Description|
181262
|:-------|:----------|:----------|:-----------|
182-
|*radiusX*|double|100.0|Horizontal radius.|
183-
|*radiusY*|double|100.0|Vertical radius.|
184-
|*startAngle*|double|0.0|Starting angle for the 1st item. Anticlockwise with 0° on the right.|
185-
|*endAngle*|double|360.0|Ending angle for the 1st item. Anticlockwise with 0° on the right.|
263+
|***radiusX***|double|100.0|Horizontal radius.|
264+
|***radiusY***|double|100.0|Vertical radius.|
265+
|***startAngle***|double|0.0|Starting angle for the 1st item. Anticlockwise with 0° on the right.|
266+
|***endAngle***|double|360.0|Ending angle for the 1st item. Anticlockwise with 0° on the right.|
186267

187268
---
269+
<br/>
270+
<br/>
188271

189-
### GridShapeParams
272+
### ***GridShapeParams***
190273

191274
|Name|Type|Defaults|Description|
192275
|:-------|:----------|:----------|:-----------|
193-
|*columns*|int|3|Number of columns.|
194-
|*columnsSpaceH*|int|0|Horizontal space between items.|
195-
|*columnsSpaceV*|int|0|Vertical space between items.|
276+
|***columns***|int|3|Number of columns.|
277+
|***columnsSpaceH***|int|0|Horizontal space between items.|
278+
|***columnsSpaceV***|int|0|Vertical space between items.|
196279

197280
---
281+
<br/>
282+
<br/>
198283

199-
### BackgroundParams
284+
### ***BackgroundParams***
200285

201286
|Name|Type|Defaults|Description|
202287
|:-------|:----------|:----------|:-----------|
203-
|*animatedBlur*|bool|false|Animate background blur from 0.0 to sigma if true.|
204-
|*sigmaX*|double|0.0|Horizontal blur.|
205-
|*sigmaY*|double|0.0|Vertical blur.|
206-
|*animatedBackgroundColor*|bool|false|Animate [backgroundColor] from transparent if true.|
207-
|*backgroundColor*|Color|#80000000|Background color.|
288+
|***animatedBlur***|bool|false|Animate background blur from 0.0 to sigma if true.|
289+
|***sigmaX***|double|0.0|Horizontal blur.|
290+
|***sigmaY***|double|0.0|Vertical blur.|
291+
|***animatedBackgroundColor***|bool|false|If true animate from transparent to [backgroundColor].|
292+
|***backgroundColor***|Color|Colors.transparent|Background color.|

0 commit comments

Comments
 (0)