dialogs.dart 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import 'package:flutter/material.dart';
  2. import 'package:photos/ente_theme_data.dart';
  3. enum DialogUserChoice { firstChoice, secondChoice }
  4. enum ActionType {
  5. confirm,
  6. critical,
  7. }
  8. // if dialog is dismissed by tapping outside, this will return null
  9. Future<DialogUserChoice> showChoiceDialog<T>(
  10. BuildContext context,
  11. String title,
  12. String content, {
  13. String firstAction = 'Ok',
  14. Color firstActionColor,
  15. String secondAction = 'Cancel',
  16. Color secondActionColor,
  17. ActionType actionType = ActionType.confirm,
  18. }) {
  19. final AlertDialog alert = AlertDialog(
  20. title: Text(
  21. title,
  22. style: TextStyle(
  23. color: actionType == ActionType.critical
  24. ? Colors.red
  25. : Theme.of(context).colorScheme.primary,
  26. ),
  27. ),
  28. content: Text(
  29. content,
  30. style: const TextStyle(
  31. height: 1.4,
  32. ),
  33. ),
  34. actions: [
  35. TextButton(
  36. child: Text(
  37. firstAction,
  38. style: TextStyle(
  39. color: firstActionColor ??
  40. (actionType == ActionType.critical
  41. ? Colors.red
  42. : Theme.of(context).colorScheme.onSurface),
  43. ),
  44. ),
  45. onPressed: () {
  46. Navigator.of(context, rootNavigator: true)
  47. .pop(DialogUserChoice.firstChoice);
  48. },
  49. ),
  50. TextButton(
  51. child: Text(
  52. secondAction,
  53. style: TextStyle(
  54. color: secondActionColor ??
  55. Theme.of(context).colorScheme.greenAlternative,
  56. ),
  57. ),
  58. onPressed: () {
  59. Navigator.of(context, rootNavigator: true)
  60. .pop(DialogUserChoice.secondChoice);
  61. },
  62. ),
  63. ],
  64. );
  65. return showDialog(
  66. context: context,
  67. builder: (BuildContext context) {
  68. return alert;
  69. },
  70. barrierColor: Colors.black87,
  71. );
  72. }