Created with <3 with dartpad.dev.
Last active
January 23, 2024 19:47
-
-
Save ultraon/937d0e43c799b620c7cde256313ca020 to your computer and use it in GitHub Desktop.
How to use OverlayPortal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
/// Flutter code sample for [OverlayPortal]. | |
void main() => runApp(const OverlayPortalExampleApp()); | |
class OverlayPortalExampleApp extends StatelessWidget { | |
const OverlayPortalExampleApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: const Text('OverlayPortal Example')), | |
body: const Center(child: ClickableTooltipWidget()), | |
), | |
); | |
} | |
} | |
class ClickableTooltipWidget extends StatefulWidget { | |
const ClickableTooltipWidget({super.key}); | |
@override | |
State<StatefulWidget> createState() => ClickableTooltipWidgetState(); | |
} | |
class ClickableTooltipWidgetState extends State<ClickableTooltipWidget> { | |
final OverlayPortalController _tooltipController = OverlayPortalController(); | |
final LayerLink _layerLink = LayerLink(); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: Colors.green, | |
child: CompositedTransformTarget( | |
link: _layerLink, | |
child: TextButton( | |
onPressed: _tooltipController.show, | |
child: DefaultTextStyle( | |
style: DefaultTextStyle.of(context).style.copyWith(fontSize: 50), | |
child: OverlayPortal( | |
controller: _tooltipController, | |
overlayChildBuilder: (BuildContext context) { | |
return Container( | |
color: Colors.blue.withAlpha(80), | |
child: Row( | |
children: [ | |
CompositedTransformFollower( | |
showWhenUnlinked: false, | |
targetAnchor: Alignment.topCenter, | |
followerAnchor: Alignment.bottomCenter, | |
link: _layerLink, | |
child: ColoredBox( | |
color: Colors.amberAccent, | |
child: TextButton( | |
onPressed: _tooltipController.hide, | |
child: const Text('tooltip'), | |
), | |
), | |
), | |
], | |
), | |
); | |
}, | |
child: const Text('Press to show/hide tooltip'), | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment