基础库
 
 
 
 
 

122 lines
3.1 KiB

  1. import 'dart:async';
  2. import 'package:zhiying_comm/zhiying_comm.dart';
  3. import 'package:flutter/material.dart';
  4. class VcodeWidget extends StatefulWidget {
  5. // 按钮文字
  6. final String btnText;
  7. // 按钮等待文字
  8. final String btnAwaitText;
  9. // 按钮文字的颜色
  10. final String btnTextColor;
  11. // 按钮的背景颜色
  12. final String color;
  13. // 禁用文字的颜色
  14. final String disabledTextColor;
  15. // 禁用按钮的背景颜色
  16. final String disabledColor;
  17. // 等待时长
  18. final int awaitTime;
  19. // 高
  20. final double height;
  21. final double width;
  22. final double border;
  23. final OnClickListener onCallBack;
  24. const VcodeWidget({
  25. this.btnText,
  26. this.btnAwaitText,
  27. this.btnTextColor,
  28. this.color,
  29. this.disabledTextColor,
  30. this.disabledColor,
  31. this.awaitTime,
  32. this.onCallBack,
  33. this.border = 8,
  34. this.height = 30,
  35. this.width = 82});
  36. @override
  37. _VcodeWidgetState createState() => _VcodeWidgetState();
  38. }
  39. class _VcodeWidgetState extends State<VcodeWidget> {
  40. /// 是否可以获取验证码
  41. bool _canGetVcode = true;
  42. /// 倒计时
  43. Timer _timer;
  44. int _countdownTime = 0;
  45. /// 获取验证码
  46. void _getVcodeOnClick() {
  47. // print('获取验证码');
  48. if(widget.onCallBack.onVcodeClick()) {
  49. int count = widget?.awaitTime ?? 60;
  50. startCountdownTimer(count);
  51. }
  52. }
  53. /// 开始倒计时
  54. void startCountdownTimer(int countTime) {
  55. const oneSec = const Duration(seconds: 1);
  56. setState(() {
  57. _canGetVcode = false;
  58. _countdownTime = countTime;
  59. });
  60. var callback = (timer) => {
  61. setState(() {
  62. if (_countdownTime < 1) {
  63. _canGetVcode = true;
  64. _timer.cancel();
  65. _countdownTime = countTime; // 重置倒计时
  66. } else {
  67. _countdownTime = _countdownTime - 1;
  68. _canGetVcode = false;
  69. }
  70. })
  71. };
  72. _timer = Timer.periodic(oneSec, callback);
  73. }
  74. @override
  75. void dispose() {
  76. _timer?.cancel();
  77. super.dispose();
  78. }
  79. @override
  80. Widget build(BuildContext context) {
  81. return Material(
  82. child: Container(
  83. alignment: Alignment.center,
  84. height: widget?.height ?? 30,
  85. width: widget?.width ?? 82,
  86. margin: const EdgeInsets.only(right: 6),
  87. child: RaisedButton(
  88. onPressed: _canGetVcode ? ()=> _getVcodeOnClick() : null,
  89. padding: EdgeInsets.zero,
  90. child: Text(_canGetVcode ? ( widget?.btnText ?? '获取验证码') : '${_countdownTime}${widget?.btnAwaitText ?? '秒'}',
  91. style: TextStyle(color: HexColor.fromHex( widget?.btnTextColor ?? '#FFFFFF'), fontSize: 11)),
  92. disabledTextColor: HexColor.fromHex( widget?.disabledTextColor ?? '#FFFFFF'),
  93. disabledColor: HexColor.fromHex( widget?.disabledColor ?? '#DDDDDD'),
  94. color: HexColor.fromHex( widget?.color ?? '#FF4343'),
  95. textColor: HexColor.fromHex( widget?.btnTextColor ?? '#FFFFFF'),
  96. elevation: 4,
  97. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(widget?.border ?? 8)),
  98. ),
  99. ),
  100. );
  101. }
  102. }
  103. abstract class OnClickListener{
  104. bool onVcodeClick();
  105. }