delayed_trigger_button.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // ignore_for_file: must_be_immutable
  2. import 'dart:async';
  3. import 'package:fis_ui/index.dart';
  4. import 'package:fis_ui/interface/interactive_container.dart';
  5. import 'package:flutter/material.dart';
  6. /// 延迟触发按钮
  7. class FDelayedTiriggerButton extends FStatefulWidget {
  8. FDelayedTiriggerButton({
  9. Key? key,
  10. required this.child,
  11. required this.onPressed,
  12. required this.businessParent,
  13. required this.name,
  14. this.style,
  15. this.countdownSeconds,
  16. VoidCallback? onLongPress,
  17. ValueChanged<bool>? onHover,
  18. ValueChanged<bool>? onFocusChange,
  19. FocusNode? focusNode,
  20. bool autofocus = false,
  21. Clip clipBehavior = Clip.none,
  22. });
  23. FInteractiveContainer businessParent;
  24. String? name;
  25. double? countdownSeconds = 5;
  26. VoidCallback? onPressed;
  27. FWidget child;
  28. ButtonStyle? style;
  29. @override
  30. FState<FDelayedTiriggerButton> createState() => _FDelayElevatedButtonState();
  31. }
  32. class _FDelayElevatedButtonState extends FState<FDelayedTiriggerButton> {
  33. late double _countdownSeconds = widget.countdownSeconds ?? 5;
  34. Timer? _timer;
  35. @override
  36. void initState() {
  37. super.initState();
  38. startCountdown();
  39. }
  40. void startCountdown() {
  41. _timer = Timer.periodic(Duration(milliseconds: 100), (timer) {
  42. setState(() {
  43. if (_countdownSeconds > 0) {
  44. _countdownSeconds -= 0.1;
  45. } else {
  46. _timer?.cancel();
  47. }
  48. });
  49. });
  50. }
  51. @override
  52. void dispose() {
  53. _timer?.cancel();
  54. super.dispose();
  55. }
  56. @override
  57. FWidget build(BuildContext context) {
  58. return FContainer(
  59. child: FStack(
  60. children: [
  61. FElevatedButton(
  62. businessParent: widget.businessParent,
  63. name: 'FDelayedTiriggerButton',
  64. onPressed: _countdownSeconds > 0 ? null : widget.onPressed,
  65. child: widget.child,
  66. style: widget.style?.copyWith(
  67. textStyle: MaterialStateProperty.all(
  68. TextStyle(
  69. color: _countdownSeconds > 0 ? Colors.black : Colors.white,
  70. fontSize: 16,
  71. ),
  72. ),
  73. ),
  74. ),
  75. FPositioned(
  76. right: 4,
  77. top: 4,
  78. child: FContainer(
  79. width: 20,
  80. height: 20,
  81. child: FStack(
  82. children: [
  83. FCircularProgressIndicator(
  84. ///当前指示的进度 0.0 -1.0
  85. value: _countdownSeconds / (widget.countdownSeconds ?? 5),
  86. strokeWidth: 2.0,
  87. ),
  88. if (_countdownSeconds >= 0)
  89. FContainer(
  90. alignment: Alignment.topCenter,
  91. child: FRichText(
  92. text: TextSpan(
  93. text: (_countdownSeconds.toInt() + 1).toString(),
  94. style: TextStyle(
  95. color: Colors.black,
  96. fontSize: 14,
  97. height: 1.4,
  98. ),
  99. ),
  100. ),
  101. ),
  102. ],
  103. ),
  104. ),
  105. ),
  106. ],
  107. ),
  108. );
  109. }
  110. }