home_page.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_modular/flutter_modular.dart';
  4. enum ImageType {
  5. CUSTOMER,
  6. PROMOTION,
  7. TUTORIAL,
  8. RENEWAL,
  9. }
  10. class HomePage extends StatefulWidget {
  11. const HomePage({super.key});
  12. @override
  13. State<HomePage> createState() => _HomePageState();
  14. }
  15. class _HomePageState extends State<HomePage> {
  16. final Map<ImageType, String> imageMap = {
  17. ImageType.CUSTOMER: "images/main/customer.png",
  18. ImageType.PROMOTION: "images/main/promotion.png",
  19. ImageType.TUTORIAL: "images/main/tutorial.png",
  20. ImageType.RENEWAL: "images/main/renewal.png",
  21. };
  22. void onImageTap(ImageType type) {
  23. print("${imageMap[type]} tapped as ${type.toString().split('.').last}");
  24. }
  25. ConnectionStatus _status = ConnectionStatus.disconnected;
  26. void _simulateStatusChange() {
  27. setState(() {
  28. switch (_status) {
  29. case ConnectionStatus.disconnected:
  30. _status = ConnectionStatus.connecting;
  31. break;
  32. case ConnectionStatus.connecting:
  33. _status = ConnectionStatus.stopped;
  34. break;
  35. case ConnectionStatus.stopped:
  36. _status = ConnectionStatus.disconnected;
  37. break;
  38. }
  39. });
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. // TODO: implement build
  44. return Scaffold(
  45. body: Container(
  46. decoration: const BoxDecoration(
  47. image: DecorationImage(
  48. image: AssetImage("images/main/main.png"),
  49. fit: BoxFit.fill,
  50. )
  51. ),
  52. child: Column(
  53. children: [
  54. const UserStatusWidget(isActive: true),
  55. Padding(
  56. padding: const EdgeInsets.fromLTRB(20, 25, 0, 0),
  57. child: Row(
  58. children: imageMap.entries.expand((entry) {
  59. return [
  60. GestureDetector(
  61. onTap: () => onImageTap(entry.key),
  62. child: Image(
  63. image: AssetImage(entry.value),
  64. width: 60,
  65. height: 60,
  66. ),
  67. ),
  68. //Spacer(), // 平均分配间隔
  69. SizedBox(width: 30),
  70. ];
  71. }).toList()..removeLast(), // 删除最后一个Spacer
  72. )
  73. ),
  74. const SizedBox(height: 30,),
  75. const Padding(
  76. padding: EdgeInsets.fromLTRB(30, 0 , 30, 0),
  77. child: Row(
  78. children: [
  79. Text("http://127.0.0.1:5336"),
  80. Spacer(),
  81. Text("socks://127.0.0.1:5337"),
  82. ],
  83. ),
  84. ),
  85. ConnectionWidget(status: ConnectionStatus.disconnected,onStatusChange: (con) {
  86. },)
  87. ],
  88. )
  89. ),
  90. );
  91. }
  92. }
  93. class UserStatusWidget extends StatefulWidget {
  94. final bool isActive;
  95. const UserStatusWidget({Key? key, required this.isActive}) : super(key: key);
  96. @override
  97. _UserStatusWidgetState createState() => _UserStatusWidgetState();
  98. }
  99. class _UserStatusWidgetState extends State<UserStatusWidget> {
  100. @override
  101. Widget build(BuildContext context) {
  102. String imagePath = widget.isActive ? "images/main/userstatus.png" : "images/main/userstatusoff.png";
  103. return Align(
  104. alignment: Alignment.topCenter,
  105. child: Padding(
  106. padding: const EdgeInsets.fromLTRB(10.0, 40.0, 10.0, 10.0),
  107. child:
  108. Row(
  109. children: [
  110. const SizedBox(
  111. width: 80,
  112. height: 20,
  113. ),
  114. Column(
  115. crossAxisAlignment: CrossAxisAlignment.start, // 这将使子组件从左边开始对齐
  116. children: [
  117. Row(
  118. children: [
  119. const Text("用户名: xxxxx"),
  120. const SizedBox(width: 10,),
  121. Image(
  122. image: AssetImage(imagePath),
  123. width: 100,
  124. height: 30,
  125. ),
  126. const SizedBox(width: 10,),
  127. IconButton(
  128. icon: Image.asset("images/main/refresh.png"),
  129. onPressed: () {
  130. // 刷新操作
  131. },
  132. )
  133. ],
  134. ),
  135. const Text(
  136. '到期时间: xxx-xx-xx',
  137. style: TextStyle(
  138. fontSize: 8.0, // 设置字体大小为20像素
  139. ),
  140. ),
  141. const Text(
  142. '用户流量: 5G',
  143. style: TextStyle(
  144. fontSize: 8.0, // 设置字体大小为20像素
  145. ),
  146. )
  147. ],
  148. ),
  149. ],
  150. ),
  151. // const SizedBox(height: 10,), // 可调整间隔
  152. ),
  153. );
  154. }
  155. }
  156. enum ConnectionStatus { disconnected, connecting, stopped }
  157. class ConnectionWidget extends StatefulWidget {
  158. final ConnectionStatus status;
  159. final Function(ConnectionStatus) onStatusChange;
  160. ConnectionWidget({required this.status, required this.onStatusChange});
  161. @override
  162. _ConnectionWidgetState createState() => _ConnectionWidgetState();
  163. }
  164. class _ConnectionWidgetState extends State<ConnectionWidget> {
  165. ConnectionStatus _currentStatus = ConnectionStatus.disconnected;
  166. late AssetImage currentImage;
  167. @override
  168. void initState() {
  169. super.initState();
  170. _updateImage();
  171. }
  172. void _updateStatus() {
  173. setState(() {
  174. switch (_currentStatus) {
  175. case ConnectionStatus.disconnected:
  176. _currentStatus = ConnectionStatus.connecting;
  177. Future.delayed(const Duration(seconds: 5), () {
  178. if (mounted) { // 确保Widget仍然在Widget树中
  179. setState(() {
  180. _currentStatus = ConnectionStatus.stopped;
  181. _updateImage();
  182. });
  183. }
  184. });
  185. break;
  186. case ConnectionStatus.connecting:
  187. // 在"connecting"状态时加入延迟
  188. //_currentStatus = ConnectionStatus.stopped;
  189. break;
  190. case ConnectionStatus.stopped:
  191. _currentStatus = ConnectionStatus.disconnected;
  192. break;
  193. }
  194. _updateImage();
  195. });
  196. }
  197. @override
  198. void didUpdateWidget(ConnectionWidget oldWidget) {
  199. super.didUpdateWidget(oldWidget);
  200. _updateImage();
  201. }
  202. void _updateImage() {
  203. switch (_currentStatus) {
  204. case ConnectionStatus.disconnected:
  205. currentImage = const AssetImage('images/main/disconnected.gif');
  206. break;
  207. case ConnectionStatus.connecting:
  208. currentImage = const AssetImage('images/main/connecting.gif');
  209. break;
  210. case ConnectionStatus.stopped:
  211. currentImage = const AssetImage('images/main/stopped.gif');
  212. break;
  213. }
  214. }
  215. @override
  216. Widget build(BuildContext context) {
  217. return GestureDetector(
  218. onTap: () {
  219. _updateStatus();
  220. // Add your action on tap here
  221. // ScaffoldMessenger.of(context).showSnackBar(
  222. // SnackBar(content: Text('Status: ${widget.status.toString()}')),
  223. // );
  224. },
  225. child: Image(
  226. image: currentImage,
  227. width: 250,
  228. height: 250,
  229. ),
  230. );
  231. }
  232. }