dialog.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. Future<T> _showAlert<T>({BuildContext context, Widget child}) => showDialog<T>(
  4. context: context,
  5. barrierDismissible: false,
  6. builder: (BuildContext context) => child,
  7. );
  8. Future<bool> showAlert(BuildContext context,
  9. {String title,
  10. String negativeText = "Cancel",
  11. String positiveText = "Confirm",
  12. bool onlyPositive = false}) =>
  13. _showAlert<bool>(
  14. context: context,
  15. child: CupertinoAlertDialog(
  16. title: Text(title),
  17. actions: _buildAlertActions(
  18. context, onlyPositive, negativeText, positiveText),
  19. ),
  20. );
  21. List<Widget> _buildAlertActions(BuildContext context, bool onlyPositive,
  22. String negativeText, String positiveText) {
  23. if (onlyPositive) {
  24. return [
  25. CupertinoDialogAction(
  26. child: Text(
  27. positiveText,
  28. style: TextStyle(fontSize: 18.0),
  29. ),
  30. isDefaultAction: true,
  31. onPressed: () {
  32. Navigator.pop(context, true);
  33. },
  34. ),
  35. ];
  36. } else {
  37. return [
  38. CupertinoDialogAction(
  39. child: Text(
  40. negativeText,
  41. style: TextStyle(color: Color(0xFF71747E), fontSize: 18.0),
  42. ),
  43. isDestructiveAction: true,
  44. onPressed: () {
  45. Navigator.pop(context, false);
  46. },
  47. ),
  48. CupertinoDialogAction(
  49. child: Text(
  50. positiveText,
  51. style: TextStyle(fontSize: 18.0),
  52. ),
  53. isDefaultAction: true,
  54. onPressed: () {
  55. Navigator.pop(context, true);
  56. },
  57. ),
  58. ];
  59. }
  60. }
  61. Future _showLoadingDialog(BuildContext c, LoadingDialog loading,
  62. {bool cancelable = true}) =>
  63. showDialog(
  64. context: c,
  65. barrierDismissible: cancelable,
  66. builder: (BuildContext c) => loading);
  67. /// 加载框
  68. class LoadingDialog extends CupertinoAlertDialog {
  69. BuildContext parentContext;
  70. BuildContext currentContext;
  71. bool showing;
  72. show(BuildContext context) {
  73. parentContext = context;
  74. showing = true;
  75. _showLoadingDialog(context, this).then((_){
  76. showing=false;
  77. });
  78. }
  79. hide() {
  80. if(showing) {
  81. Navigator.removeRoute(parentContext, ModalRoute.of(currentContext));
  82. }
  83. }
  84. @override
  85. Widget build(BuildContext context) {
  86. currentContext= context;
  87. return WillPopScope(
  88. onWillPop: () => Future.value(true),
  89. child: LayoutBuilder(
  90. builder: (BuildContext context, BoxConstraints constraints) {
  91. return Center(
  92. child: Container(
  93. width: 120,
  94. height: 120,
  95. child: CupertinoPopupSurface(
  96. child: Semantics(
  97. namesRoute: true,
  98. scopesRoute: true,
  99. explicitChildNodes: true,
  100. child: const Center(
  101. child: CupertinoActivityIndicator(),
  102. ),
  103. ),
  104. ),
  105. ),
  106. );
  107. },
  108. ),
  109. );
  110. }
  111. }