connection_button.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import 'package:flutter/material.dart' hide ConnectionState;
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'dart:math' as math;
  4. import 'package:get/get.dart';
  5. import 'package:nomo/app/widgets/ix_image.dart';
  6. import '../../../constants/assets.dart';
  7. import '../../../../config/theme/theme_extensions/theme_extension.dart';
  8. import '../../../constants/enums.dart';
  9. class ConnectionButton extends StatefulWidget {
  10. final ConnectionState state;
  11. final VoidCallback? onTap;
  12. const ConnectionButton({super.key, required this.state, this.onTap});
  13. @override
  14. State<ConnectionButton> createState() => _ConnectionButtonState();
  15. }
  16. class _ConnectionButtonState extends State<ConnectionButton>
  17. with TickerProviderStateMixin {
  18. bool _isAtTop = false; // 控制按钮位置,false=底部,true=顶部
  19. List<Color>? _previousGradientColor; // 保存前一个渐变色用于动画过渡
  20. late AnimationController _rotationController; // 旋转动画控制器
  21. @override
  22. void initState() {
  23. super.initState();
  24. // 初始化旋转动画控制器
  25. _rotationController = AnimationController(
  26. duration: const Duration(seconds: 2),
  27. vsync: this,
  28. );
  29. }
  30. @override
  31. void dispose() {
  32. _rotationController.dispose();
  33. super.dispose();
  34. }
  35. void _onTap() {
  36. if (widget.onTap != null) {
  37. widget.onTap!();
  38. }
  39. }
  40. // 比较两个颜色列表是否相等
  41. bool _colorsEqual(List<Color> a, List<Color> b) {
  42. if (a.length != b.length) return false;
  43. for (int i = 0; i < a.length; i++) {
  44. if (a[i].value != b[i].value) return false;
  45. }
  46. return true;
  47. }
  48. Widget _buildImageWithAnimation(String imgPath) {
  49. // 如果是connecting状态,添加旋转动画
  50. if (widget.state == ConnectionState.connecting) {
  51. // 启动旋转动画
  52. if (!_rotationController.isAnimating) {
  53. _rotationController.repeat();
  54. }
  55. return AnimatedBuilder(
  56. animation: _rotationController,
  57. builder: (context, child) {
  58. return Transform.rotate(
  59. angle: _rotationController.value * 2 * math.pi,
  60. child: IXImage(
  61. source: imgPath,
  62. sourceType: ImageSourceType.asset,
  63. width: 30.w,
  64. height: 30.w,
  65. ),
  66. );
  67. },
  68. );
  69. } else {
  70. // 其他状态停止旋转动画
  71. if (_rotationController.isAnimating) {
  72. _rotationController.stop();
  73. }
  74. return IXImage(
  75. source: imgPath,
  76. sourceType: ImageSourceType.asset,
  77. width: 30.w,
  78. height: 30.w,
  79. );
  80. }
  81. }
  82. @override
  83. Widget build(BuildContext context) {
  84. return GestureDetector(onTap: _onTap, child: _buildMainButton());
  85. }
  86. Widget _buildMainButton() {
  87. // 根据状态和主题获取颜色
  88. List<Color> gradientColor;
  89. String imgPath;
  90. String statusImgPath;
  91. String text;
  92. Color textColor;
  93. switch (widget.state) {
  94. case ConnectionState.disconnected:
  95. gradientColor = [
  96. Get.reactiveTheme.highlightColor,
  97. Get.reactiveTheme.hintColor,
  98. ];
  99. imgPath = Assets.switchStatusDisconnected;
  100. statusImgPath = Assets.disconnected;
  101. text = "Disconnected";
  102. textColor = Get.reactiveTheme.hintColor;
  103. if (_isAtTop) {
  104. setState(() {
  105. _isAtTop = false;
  106. });
  107. }
  108. break;
  109. case ConnectionState.connecting:
  110. gradientColor = [
  111. Get.reactiveTheme.highlightColor,
  112. Get.reactiveTheme.hintColor,
  113. ];
  114. imgPath = Assets.switchStatusConnecting;
  115. statusImgPath = Assets.connecting;
  116. text = "Connecting";
  117. textColor = Get.reactiveTheme.hintColor;
  118. // 切换位置
  119. setState(() {
  120. _isAtTop = !_isAtTop;
  121. });
  122. break;
  123. case ConnectionState.connected:
  124. gradientColor = [
  125. Get.reactiveTheme.shadowColor,
  126. Get.reactiveTheme.primaryColor,
  127. ];
  128. imgPath = Assets.switchStatusConnected;
  129. statusImgPath = Assets.connected;
  130. text = "Connected";
  131. textColor = Get.reactiveTheme.textTheme.bodyLarge!.color!;
  132. if (!_isAtTop) {
  133. setState(() {
  134. _isAtTop = true;
  135. });
  136. }
  137. break;
  138. case ConnectionState.error:
  139. gradientColor = [
  140. Get.reactiveTheme.highlightColor,
  141. Get.reactiveTheme.hintColor,
  142. ];
  143. imgPath = Assets.switchStatusDisconnected;
  144. statusImgPath = Assets.error;
  145. text = "Error";
  146. textColor = Get.reactiveTheme.hintColor;
  147. break;
  148. }
  149. // 保存前一个渐变色用于动画
  150. final previousColor = _previousGradientColor ?? gradientColor;
  151. // 只在颜色真正改变时更新
  152. if (_previousGradientColor == null ||
  153. _previousGradientColor!.length != gradientColor.length ||
  154. !_colorsEqual(_previousGradientColor!, gradientColor)) {
  155. // 延迟更新,避免在 build 中调用 setState
  156. WidgetsBinding.instance.addPostFrameCallback((_) {
  157. if (mounted) {
  158. _previousGradientColor = gradientColor;
  159. }
  160. });
  161. }
  162. return Column(
  163. children: [
  164. TweenAnimationBuilder<List<Color>>(
  165. duration: const Duration(milliseconds: 300),
  166. curve: Curves.easeInOut,
  167. tween: ColorListTween(begin: previousColor, end: gradientColor),
  168. builder: (context, colors, child) {
  169. return Container(
  170. width: 72.w,
  171. height: 132.w,
  172. padding: EdgeInsets.all(6.w),
  173. decoration: BoxDecoration(
  174. borderRadius: BorderRadius.circular(20.r),
  175. gradient: LinearGradient(
  176. begin: Alignment.topCenter,
  177. end: Alignment.bottomCenter,
  178. colors: colors,
  179. ),
  180. ),
  181. child: child,
  182. );
  183. },
  184. child: AnimatedAlign(
  185. duration: const Duration(milliseconds: 300),
  186. curve: Curves.fastOutSlowIn,
  187. alignment: _isAtTop ? Alignment.topCenter : Alignment.bottomCenter,
  188. child: Container(
  189. width: 60.w,
  190. height: 60.w,
  191. alignment: Alignment.center,
  192. decoration: BoxDecoration(
  193. color: Colors.white,
  194. borderRadius: BorderRadius.circular(14.r),
  195. ),
  196. child: AnimatedSwitcher(
  197. duration: const Duration(milliseconds: 300),
  198. transitionBuilder: (Widget child, Animation<double> animation) {
  199. return FadeTransition(
  200. opacity: animation,
  201. child: ScaleTransition(
  202. scale: Tween<double>(
  203. begin: 0.85,
  204. end: 1.0,
  205. ).animate(animation),
  206. child: child,
  207. ),
  208. );
  209. },
  210. child: _buildImageWithAnimation(imgPath),
  211. ),
  212. ),
  213. ),
  214. ),
  215. 20.verticalSpaceFromWidth,
  216. Row(
  217. mainAxisAlignment: MainAxisAlignment.center,
  218. children: [
  219. IXImage(
  220. source: statusImgPath,
  221. sourceType: ImageSourceType.asset,
  222. width: 14.w,
  223. height: 14.w,
  224. ),
  225. 4.horizontalSpace,
  226. Text(
  227. text,
  228. style: TextStyle(
  229. fontSize: 14.sp,
  230. fontWeight: FontWeight.w500,
  231. color: textColor,
  232. ),
  233. ),
  234. ],
  235. ),
  236. ],
  237. );
  238. }
  239. }
  240. // 自定义颜色列表插值器,用于渐变色动画
  241. class ColorListTween extends Tween<List<Color>> {
  242. ColorListTween({super.begin, super.end});
  243. @override
  244. List<Color> lerp(double t) {
  245. if (begin == null || end == null) {
  246. return end ?? begin ?? [];
  247. }
  248. final length = math.max(begin!.length, end!.length);
  249. return List.generate(length, (index) {
  250. final beginColor = index < begin!.length ? begin![index] : begin!.last;
  251. final endColor = index < end!.length ? end![index] : end!.last;
  252. return Color.lerp(beginColor, endColor, t) ?? endColor;
  253. });
  254. }
  255. }