Skip to content

Instantly share code, notes, and snippets.

@davidhicks980
Last active January 24, 2024 17:06
Show Gist options
  • Save davidhicks980/c82e3b2e0db2a739d597e4062f93da0c to your computer and use it in GitHub Desktop.
Save davidhicks980/c82e3b2e0db2a739d597e4062f93da0c to your computer and use it in GitHub Desktop.
sparkling-flash-8427

sparkling-flash-8427

Created with <3 with dartpad.dev.

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