Skip to content

Commit

Permalink
Add Flyout.navigatorKey (Fixes #538)
Browse files Browse the repository at this point in the history
A key to the navigator used to show the flyout. Usually provided when the Flyout if off the scope of a [Navigator]. If null, [Navigator.of] is used
  • Loading branch information
bdlukaa committed Dec 23, 2022
1 parent fc77248 commit a1e9560
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- `FlyoutListTile` can be used outside of a flyout ([#650](https://github.com/bdlukaa/fluent_ui/issues/650))
- Add uk localization ([#647](https://github.com/bdlukaa/fluent_ui/pull/647))
- Add `Flyout.navigatorKey` ([#538](https://github.com/bdlukaa/fluent_ui/issues/538))

## 4.1.2

Expand Down
2 changes: 1 addition & 1 deletion lib/fluent_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ export 'src/styles/focus.dart';
export 'src/utils/horizontal_scroll_view.dart';
export 'src/utils/label.dart';

export 'src/utils/popup.dart';
export 'src/utils/popup.dart' show showMenu, ContentManager, ContentSizeInfo;
10 changes: 10 additions & 0 deletions lib/src/controls/surfaces/flyout/flyout.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:fluent_ui/fluent_ui.dart';
import 'package:fluent_ui/src/utils/popup.dart';
import 'package:flutter/foundation.dart';

export 'controller.dart';
Expand Down Expand Up @@ -96,6 +97,7 @@ class Flyout extends StatefulWidget {
this.longHoverDuration = kDefaultLongHoverDuration,
this.onOpen,
this.onClose,
this.navigatorKey,
}) : super(key: key);

/// The child that will be attached to the flyout.
Expand Down Expand Up @@ -148,6 +150,13 @@ class Flyout extends StatefulWidget {
/// Called when the flyout is closed, either by [controller] or by the user
final VoidCallback? onClose;

/// A key to the navigator used to show the flyout.
///
/// Usually provided when the Flyout if off the scope of a [Navigator].
///
/// If null, [Navigator.of] is used
final NavigatorKey? navigatorKey;

@override
_FlyoutState createState() => _FlyoutState();

Expand Down Expand Up @@ -250,6 +259,7 @@ class _FlyoutState extends State<Flyout> {
horizontalOffset: widget.horizontalOffset,
placement: widget.placement,
position: widget.position,
navigatorKey: widget.navigatorKey,
child: widget.child,
);

Expand Down
8 changes: 7 additions & 1 deletion lib/src/utils/popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Future<T?> showMenu<T>({
));
}

typedef NavigatorKey = GlobalKey<NavigatorState>;

class PopUp<T> extends StatefulWidget {
const PopUp({
Key? key,
Expand All @@ -94,6 +96,7 @@ class PopUp<T> extends StatefulWidget {
this.horizontalOffset = 0,
this.placement = FlyoutPlacement.center,
this.position = FlyoutPosition.above,
this.navigatorKey,
}) : super(key: key);

final Widget child;
Expand All @@ -104,6 +107,8 @@ class PopUp<T> extends StatefulWidget {
final FlyoutPlacement placement;
final FlyoutPosition position;

final NavigatorKey? navigatorKey;

@override
PopUpState<T> createState() => PopUpState<T>();
}
Expand All @@ -113,7 +118,8 @@ class PopUpState<T> extends State<PopUp<T>> {

Future<void> openPopup() {
assert(_dropdownRoute == null, 'You can NOT open a popup twice');
final NavigatorState navigator = Navigator.of(context);
final NavigatorState navigator =
widget.navigatorKey?.currentState ?? Navigator.of(context);
final RenderBox itemBox = context.findRenderObject()! as RenderBox;
Offset leftTarget = itemBox.localToGlobal(
itemBox.size.centerLeft(Offset.zero),
Expand Down

0 comments on commit a1e9560

Please sign in to comment.