Skip to content

Instantly share code, notes, and snippets.

@manthri-mohan-sai
Created October 14, 2021 10:42
Show Gist options
  • Save manthri-mohan-sai/653ee961795ce196691ca21c4189af9d to your computer and use it in GitHub Desktop.
Save manthri-mohan-sai/653ee961795ce196691ca21c4189af9d to your computer and use it in GitHub Desktop.
PopupMenuButton -> show dialog on popupMenuItem tap
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
void showDialog(BuildContext context) {
WidgetsBinding?.instance?.addPostFrameCallback((_) {
showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: const Icon(CupertinoIcons.info_circle),
content: const Text(
'Hello User, Welcome',
textAlign: TextAlign.center,
),
actions: [
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () => Navigator.pop(context),
child: const Text('Thanks'),
),
],
);
});
});
}
@override
Widget build(BuildContext context) {
return PopupMenuButton(
itemBuilder: (context) {
return [
PopupMenuItem(
child: const Text('Item 0'),
onTap: () {
WidgetsBinding?.instance?.addPostFrameCallback((_) {
showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: const Icon(CupertinoIcons.info_circle),
content: const Text(
'Hello User, Welcome',
textAlign: TextAlign.center,
),
actions: [
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () => Navigator.pop(context),
child: const Text('Thanks'),
),
],
);
});
});
}),
PopupMenuItem(child: const Text('Item 1')),
PopupMenuItem(child: const Text('Item 2')),
PopupMenuItem(child: const Text('Item 3')),
];
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment