Skip to content

Commit

Permalink
Flyout and tooltip position (#780)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa authored Mar 24, 2023
2 parents 3b783eb + cd1d161 commit 2bb4d60
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

- `TabView` lazy loading ([#751](https://github.com/bdlukaa/fluent_ui/issues/751))
- Added Bangla localization ([#786](https://github.com/bdlukaa/fluent_ui/pull/786))
- Correctly position the flyouts on a multi navigator context ([#780](https://github.com/bdlukaa/fluent_ui/pull/780))

## 4.4.1

Expand Down
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -766,10 +766,10 @@ class _LinkPaneItemAction extends PaneItem {
}
}

final _rootNavigatorKey = GlobalKey<NavigatorState>();
final rootNavigatorKey = GlobalKey<NavigatorState>();
final _shellNavigatorKey = GlobalKey<NavigatorState>();
final router = GoRouter(
navigatorKey: _rootNavigatorKey,
navigatorKey: rootNavigatorKey,
routes: [
ShellRoute(
navigatorKey: _shellNavigatorKey,
Expand Down
28 changes: 26 additions & 2 deletions example/lib/screens/popups/flyout.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:example/main.dart';
import 'package:example/theme.dart';
import 'package:example/widgets/card_highlight.dart';
import 'package:example/widgets/page.dart';
Expand Down Expand Up @@ -121,6 +122,7 @@ class _Flyout2ScreenState extends State<Flyout2Screen> with PageMixin {
barrierDismissible: barrierDismissible,
dismissOnPointerMoveAway: dismissOnPointerMoveAway,
dismissWithEsc: dismissWithEsc,
navigatorKey: rootNavigatorKey.currentState,
builder: (context) {
return FlyoutContent(
child: Column(
Expand Down Expand Up @@ -181,6 +183,7 @@ class _Flyout2ScreenState extends State<Flyout2Screen> with PageMixin {
barrierDismissible: barrierDismissible,
dismissOnPointerMoveAway: dismissOnPointerMoveAway,
dismissWithEsc: dismissWithEsc,
navigatorKey: rootNavigatorKey.currentState,
builder: (context) {
return MenuFlyout(
items: [
Expand Down Expand Up @@ -262,12 +265,23 @@ class _Flyout2ScreenState extends State<Flyout2Screen> with PageMixin {
),
CardHighlight(
codeSnippet: '''final contextController = FlyoutController();
final contextAttachKey = GlobalKey();
return GestureDetector(
onSecondaryTapUp: (d) {
// This calculates the position of the flyout according to the parent navigator
final targetContext = contextAttachKey.currentContext;
if (targetContext == null) return;
final box = targetContext.findRenderObject() as RenderBox;
final position = box.localToGlobal(
d.localPosition,
ancestor: Navigator.of(context).context.findRenderObject(),
);
contextController.showFlyout(
barrierColor: Colors.black.withOpacity(0.1),
position: d.globalPosition,
position: position,
builder: (context) {
return FlyoutContent(
child: SizedBox(
Expand Down Expand Up @@ -307,16 +321,26 @@ return GestureDetector(
);
},
child: FlyoutTarget(
key: contextAttachKey,
controller: contextController,
child: const FlutterLogo(size: 400.0),
),
);
''',
child: GestureDetector(
onSecondaryTapUp: (d) {
final targetContext = contextAttachKey.currentContext;
if (targetContext == null) return;

final box = targetContext.findRenderObject() as RenderBox;
final position = box.localToGlobal(
d.localPosition,
ancestor: Navigator.of(context).context.findRenderObject(),
);

contextController.showFlyout(
barrierColor: Colors.black.withOpacity(0.1),
position: d.globalPosition,
position: position,
builder: (context) {
return FlyoutContent(
child: SizedBox(
Expand Down
16 changes: 10 additions & 6 deletions lib/src/controls/flyouts/flyout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,16 @@ class FlyoutController with ChangeNotifier {

final targetBox = context.findRenderObject() as RenderBox;
targetSize = targetBox.size;
targetOffset =
targetBox.localToGlobal(Offset.zero, ancestor: navigatorBox) +
Offset(0, targetSize.height);
targetRect =
targetBox.localToGlobal(Offset.zero, ancestor: navigatorBox) &
targetSize;
targetOffset = targetBox.localToGlobal(
Offset.zero,
ancestor: navigatorBox,
) +
Offset(0, targetSize.height);
targetRect = targetBox.localToGlobal(
Offset.zero,
ancestor: navigatorBox,
) &
targetSize;
}

_open = true;
Expand Down
11 changes: 8 additions & 3 deletions lib/src/controls/flyouts/menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,15 @@ class _MenuFlyoutSubItemState extends State<_MenuFlyoutSubItem>
}

void show(MenuInfoProviderState menuInfo) {
final itemBox = context.findRenderObject() as RenderBox;
final itemRect = itemBox.localToGlobal(Offset.zero) & itemBox.size;

final parent = Flyout.of(context);

final itemBox = context.findRenderObject() as RenderBox;
final itemRect = itemBox.localToGlobal(
Offset.zero,
ancestor: parent.widget.root?.context.findRenderObject(),
) &
itemBox.size;

menuInfo.add(
CustomSingleChildLayout(
delegate: _SubItemPositionDelegate(
Expand All @@ -457,6 +461,7 @@ class _MenuFlyoutSubItemState extends State<_MenuFlyoutSubItem>
additionalOffset: parent.additionalOffset,
margin: parent.margin,
transitionDuration: parent.transitionDuration,
root: parent.widget.root,
builder: (context) {
return FadeTransition(
opacity: transitionController,
Expand Down

0 comments on commit 2bb4d60

Please # to comment.