dialogs.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 ? Colors.red : Colors.white,
  23. ),
  24. ),
  25. content: Text(
  26. content,
  27. style: TextStyle(
  28. height: 1.4,
  29. ),
  30. ),
  31. actions: [
  32. TextButton(
  33. child: Text(
  34. firstAction,
  35. style: TextStyle(
  36. color: firstActionColor ??
  37. (actionType == ActionType.critical ? Colors.red : Colors.white),
  38. ),
  39. ),
  40. onPressed: () {
  41. Navigator.of(context, rootNavigator: true)
  42. .pop(DialogUserChoice.firstChoice);
  43. },
  44. ),
  45. TextButton(
  46. child: Text(
  47. secondAction,
  48. style: TextStyle(
  49. color: secondActionColor ?? Theme.of(context).buttonColor,
  50. ),
  51. ),
  52. onPressed: () {
  53. Navigator.of(context, rootNavigator: true)
  54. .pop(DialogUserChoice.secondChoice);
  55. },
  56. ),
  57. ],
  58. );
  59. return showDialog(
  60. context: context,
  61. builder: (BuildContext context) {
  62. return alert;
  63. },
  64. barrierColor: Colors.black87,
  65. );
  66. }