connection_widget.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'connection_status.dart';
  4. class ConnectionWidget extends StatefulWidget {
  5. final ConnectionStatus status;
  6. final Function() onTap; // 只通知外部发生了点击事件
  7. const ConnectionWidget({
  8. Key? key,
  9. required this.status,
  10. required this.onTap,
  11. }) : super(key: key);
  12. @override
  13. _ConnectionWidgetState createState() => _ConnectionWidgetState();
  14. }
  15. class _ConnectionWidgetState extends State<ConnectionWidget> {
  16. late String currentImage;
  17. @override
  18. void initState() {
  19. super.initState();
  20. _updateImage();
  21. }
  22. @override
  23. void didUpdateWidget(ConnectionWidget oldWidget) {
  24. super.didUpdateWidget(oldWidget);
  25. if (oldWidget.status != widget.status) {
  26. _updateImage();
  27. }
  28. }
  29. void _updateImage() {
  30. switch (widget.status) {
  31. case ConnectionStatus.disconnected:
  32. currentImage = 'assets/images/main/disconnected.gif';
  33. break;
  34. case ConnectionStatus.connecting:
  35. currentImage = 'assets/images/main/connecting.gif';
  36. break;
  37. case ConnectionStatus.stopped:
  38. currentImage = 'assets/images/main/stopped.gif';
  39. break;
  40. }
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. return Stack(
  45. alignment: Alignment.center,
  46. children: <Widget>[
  47. Image.asset(
  48. currentImage,
  49. fit: BoxFit.cover,
  50. width: 250,
  51. height: 250,
  52. ),
  53. ClipOval(
  54. child: Material(
  55. color: Colors.transparent,
  56. child: GestureDetector(
  57. onTap: widget.onTap, // 使用外部传入的点击函数
  58. child: Container(
  59. width: 100,
  60. height: 100,
  61. decoration: const BoxDecoration(
  62. shape: BoxShape.circle,
  63. color: Colors.transparent,
  64. ),
  65. ),
  66. ),
  67. ),
  68. ),
  69. ],
  70. );
  71. }
  72. }