page_widget.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'package:flutter/material.dart';
  2. import 'package:lszlgl/config/colors.dart';
  3. import 'package:lszlgl/widget/button.dart';
  4. enum PageState {
  5. loading,
  6. success,
  7. error,
  8. }
  9. class PageLoadingWidget extends StatelessWidget {
  10. final AlignmentGeometry? alignment;
  11. final bool isLoading;
  12. final String? title;
  13. final String? message;
  14. final String? btnText;
  15. final VoidCallback? onTap;
  16. final EdgeInsetsGeometry? padding;
  17. const PageLoadingWidget({
  18. super.key,
  19. this.alignment,
  20. this.isLoading = false,
  21. this.title,
  22. this.message,
  23. this.btnText,
  24. this.onTap,
  25. this.padding = const EdgeInsets.only(top: 150),
  26. });
  27. const PageLoadingWidget.loading({
  28. super.key,
  29. this.alignment,
  30. this.message,
  31. this.padding = const EdgeInsets.only(top: 150),
  32. }) : isLoading = true,
  33. title = null,
  34. onTap = null,
  35. btnText = null;
  36. const PageLoadingWidget.empty({
  37. super.key,
  38. this.alignment,
  39. this.message = '暂无数据',
  40. this.padding = const EdgeInsets.only(top: 150),
  41. }) : isLoading = false,
  42. title = null,
  43. onTap = null,
  44. btnText = null;
  45. const PageLoadingWidget.error({
  46. super.key,
  47. this.alignment,
  48. this.title = '加载失败, 请重试',
  49. this.onTap,
  50. this.btnText = '点击重试',
  51. this.padding = const EdgeInsets.only(top: 150),
  52. }) : isLoading = false,
  53. message = null;
  54. @override
  55. Widget build(BuildContext context) {
  56. return Container(
  57. alignment: alignment ?? Alignment.center,
  58. padding: padding,
  59. child: Column(
  60. children: [
  61. buildLoading(),
  62. buildTitle(),
  63. buildMessage(),
  64. buildButton(),
  65. ],
  66. ),
  67. );
  68. }
  69. Widget buildLoading() {
  70. if (!isLoading) return const SizedBox.shrink();
  71. return const Padding(
  72. padding: EdgeInsets.only(bottom: 12),
  73. child: CircularProgressIndicator(strokeWidth: 3),
  74. );
  75. }
  76. Widget buildTitle() {
  77. if (title == null) return const SizedBox.shrink();
  78. return Padding(
  79. padding: const EdgeInsets.only(bottom: 12),
  80. child: Text(
  81. title!,
  82. style: const TextStyle(fontSize: 16, color: MyColor.c_333333),
  83. ),
  84. );
  85. }
  86. Widget buildMessage() {
  87. if (message == null) return const SizedBox.shrink();
  88. return Padding(
  89. padding: const EdgeInsets.only(bottom: 12),
  90. child: Text(
  91. message!,
  92. style: const TextStyle(fontSize: 16, color: MyColor.c_666666),
  93. ),
  94. );
  95. }
  96. Widget buildButton() {
  97. if (btnText == null) return const SizedBox.shrink();
  98. return MyButton(
  99. btnText!,
  100. onTap: onTap,
  101. alignment: null,
  102. );
  103. }
  104. }