dialogs.dart 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.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<T> showChoiceDialog<T>(
  10. BuildContext context,
  11. String title,
  12. String content, {
  13. String firstAction = 'ok',
  14. String secondAction = 'cancel',
  15. ActionType actionType = ActionType.confirm,
  16. }) {
  17. AlertDialog alert = AlertDialog(
  18. title: Text(
  19. title,
  20. style: TextStyle(
  21. color: actionType == ActionType.critical ? Colors.red : Colors.white,
  22. ),
  23. ),
  24. content: Text(
  25. content,
  26. style: TextStyle(
  27. height: 1.4,
  28. ),
  29. ),
  30. actions: [
  31. TextButton(
  32. child: Text(
  33. firstAction,
  34. style: TextStyle(
  35. color:
  36. actionType == ActionType.critical ? Colors.red : Colors.white,
  37. ),
  38. ),
  39. onPressed: () {
  40. Navigator.of(context, rootNavigator: true)
  41. .pop(DialogUserChoice.firstChoice);
  42. },
  43. ),
  44. TextButton(
  45. child: Text(
  46. secondAction,
  47. style: TextStyle(
  48. color: Theme.of(context).buttonColor,
  49. ),
  50. ),
  51. onPressed: () {
  52. Navigator.of(context, rootNavigator: true)
  53. .pop(DialogUserChoice.secondChoice);
  54. },
  55. ),
  56. ],
  57. );
  58. return showDialog(
  59. context: context,
  60. builder: (BuildContext context) {
  61. return alert;
  62. },
  63. barrierColor: Colors.black87,
  64. );
  65. }