dialogs.dart 1.7 KB

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