Skip to content

Instantly share code, notes, and snippets.

@HansMuller
Last active June 2, 2024 05:09
Show Gist options
  • Save HansMuller/59feb99eb51a1e6b65ca57fdfdc199a4 to your computer and use it in GitHub Desktop.
Save HansMuller/59feb99eb51a1e6b65ca57fdfdc199a4 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
class MenuDemo extends StatelessWidget {
static const slug = 'menu';
Widget menuItem(IconData? icon, int itemNumber) {
return PopupMenuItem<Object>(
child: Row(
children: [
if (icon != null) ...[Icon(icon), const SizedBox(width: 16)],
Expanded(
child: Text('item $itemNumber'),
),
],
),
);
}
Widget menuButtonAndDescription(
String description, [
IconData? icon1,
IconData? icon2,
IconData? icon3,
]) {
return Padding(
padding: const EdgeInsets.all(32),
child: Column(
children: [
PopupMenuButton<void>(
tooltip: description,
itemBuilder: (context) {
return <PopupMenuEntry<Object>>[
menuItem(icon1, 1) as PopupMenuEntry<Object>,
menuItem(icon2, 2) as PopupMenuEntry<Object>,
menuItem(icon3, 3) as PopupMenuEntry<Object>,
];
},
),
Text(description),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Menu')),
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
menuButtonAndDescription('Menu without icons'),
menuButtonAndDescription(
'Menu with icons',
Icons.bookmark,
Icons.lock,
Icons.check_circle,
),
],
),
),
),
);
}
}
class Home extends StatelessWidget {
const Home({ super.key });
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
SemanticsBinding.instance.ensureSemantics();
Navigator.push(context, MaterialPageRoute<void>(
builder: (BuildContext context) => MenuDemo(),
));
},
child: const Text('Show Menus'),
),
),
);
}
}
void main() {
runApp(const MaterialApp(home: Home()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment