dialog.dart 3.1 KB

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