Created with <3 with dartpad.dev.
Last active
January 24, 2024 17:06
-
-
Save davidhicks980/c82e3b2e0db2a739d597e4062f93da0c to your computer and use it in GitHub Desktop.
sparkling-flash-8427
This file contains 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'; | |
import 'package:flutter/services.dart'; | |
/// Flutter code sample for [MenuAnchor]. | |
void main() => runApp(const MenuApp()); | |
class MenuApp extends StatelessWidget { | |
const MenuApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData(useMaterial3: true), | |
home: const Scaffold(body: SafeArea(child: MyCascadingMenu())), | |
); | |
} | |
} | |
class MyCascadingMenu extends StatefulWidget { | |
const MyCascadingMenu({super.key}); | |
@override | |
State<MyCascadingMenu> createState() => _MyCascadingMenuState(); | |
} | |
class _MyCascadingMenuState extends State<MyCascadingMenu> { | |
late FocusNode? _buttonFocusNode = FocusNode(); | |
@override | |
Widget build(BuildContext context) { | |
return MenuAnchor( | |
menuChildren: <Widget>[ | |
MenuItemButton( | |
focusNode: _buttonFocusNode, | |
closeOnActivate: false, | |
child: Text("Set focus to null"), | |
onPressed: () => setState(() { | |
_buttonFocusNode = null; | |
})), | |
], | |
builder: | |
(BuildContext context, MenuController controller, Widget? child) { | |
return TextButton( | |
onPressed: () { | |
if (controller.isOpen) { | |
controller.close(); | |
} else { | |
controller.open(); | |
} | |
}, | |
child: const Text('OPEN MENU'), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment