Created
June 25, 2026 18:33
-
-
Save loic-sharma/ea3cc4898684190fe44a6976d6fd2016 to your computer and use it in GitHub Desktop.
Transform follower sample
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'; | |
| void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: FollowerPortalExample())))); | |
| class FollowerPortalExample extends StatefulWidget { | |
| const FollowerPortalExample({super.key}); | |
| @override | |
| State<FollowerPortalExample> createState() => _FollowerPortalExampleState(); | |
| } | |
| class _FollowerPortalExampleState extends State<FollowerPortalExample> { | |
| // 1. Create the LayerLink and the OverlayPortalController | |
| final LayerLink _layerLink = LayerLink(); | |
| final OverlayPortalController _tooltipController = OverlayPortalController(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| // 2. Wrap your target widget structure in an OverlayPortal | |
| OverlayPortal( | |
| controller: _tooltipController, | |
| // This builds the floating child when controller.toggle() or show() is called | |
| overlayChildBuilder: (BuildContext context) { | |
| return CompositedTransformFollower( | |
| link: _layerLink, | |
| showWhenUnlinked: false, | |
| // Aligns the follower's top-left to the target's bottom-left | |
| targetAnchor: Alignment.bottomLeft, | |
| child: Align( | |
| alignment: Alignment.topLeft, | |
| child: Material( | |
| elevation: 4.0, | |
| child: Container( | |
| width: 200, // Or dynamically passed down | |
| color: Colors.amber[100], | |
| child: ListView( | |
| padding: EdgeInsets.zero, | |
| shrinkWrap: true, | |
| children: const [ | |
| ListTile(title: Text('Option 1')), | |
| ListTile(title: Text('Option 2')), | |
| ], | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| }, | |
| // 3. The target widget itself acts as the anchor child | |
| child: CompositedTransformTarget( | |
| link: _layerLink, | |
| child: ElevatedButton( | |
| onPressed: () { | |
| // Declaratively toggle the overlay visibility | |
| _tooltipController.toggle(); | |
| }, | |
| child: const Text('Toggle Floating Menu'), | |
| ), | |
| ), | |
| ), | |
| ], | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment