home_view.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import 'dart:io';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. import 'package:naiyouwl/app/component/sys_app_bar.dart';
  6. import 'package:tray_manager/tray_manager.dart';
  7. import 'package:window_manager/window_manager.dart';
  8. import '../../../component/connection_status.dart';
  9. import '../../../component/connection_widget.dart';
  10. import '../../../global_controller/GlobalController.dart';
  11. import '../../../service/clash_service.dart';
  12. import '../controllers/home_controller.dart';
  13. class HomeView extends GetView<HomeController> {
  14. const HomeView({Key? key}) : super(key: key);
  15. @override
  16. Widget build(BuildContext context) {
  17. return Container(
  18. decoration: const BoxDecoration(
  19. image: DecorationImage(
  20. image: AssetImage("assets/images/main/main.png"),
  21. fit: BoxFit.fill,
  22. ),
  23. ),
  24. child: Scaffold(
  25. backgroundColor: Colors.transparent,
  26. appBar: SysAppBar(title: Text("首页"), actions: [
  27. Row(
  28. children: [
  29. IconButton(
  30. icon: const Icon(Icons.refresh),
  31. onPressed: () {
  32. controller.fetchNode();
  33. },
  34. ),
  35. const Text('刷新节点'), // 这里是您的标题
  36. ],
  37. ),
  38. ],),
  39. body: Obx(() {
  40. if (controller.errorMsg.isNotEmpty) {
  41. WidgetsBinding.instance.addPostFrameCallback((_) {
  42. ScaffoldMessenger.of(context).showSnackBar(
  43. SnackBar(content: Text(controller.errorMsg.value))
  44. );
  45. });
  46. }
  47. return controller.isLoading.value ? const Center(
  48. child: CircularProgressIndicator()) : Column(
  49. children: [
  50. // 错误消息展示
  51. UserStatusWidget(
  52. isActive: controller.GetEnable(),
  53. isLoading: controller.isLoading.value,
  54. username: controller.GetUserName(),
  55. expiryDate: controller.GetExpiredAt(),
  56. userTraffic: controller.GetTraffic(),
  57. onRefresh: () async {
  58. // 这里插入刷新操作代码
  59. //await Future.delayed(Duration(seconds: 2));
  60. if (controller.isLoading.value != true) {
  61. controller.fetchUserinfo();
  62. }
  63. },),
  64. Padding(
  65. padding: const EdgeInsets.fromLTRB(20, 20, 0, 0),
  66. child: Row(
  67. children: controller.imageMap.entries.expand((entry) {
  68. return [
  69. GestureDetector(
  70. onTap: () => controller.onImageTap(entry.key),
  71. child: Image(
  72. image: AssetImage(entry.value),
  73. width: 60,
  74. height: 60,
  75. ),
  76. ),
  77. //Spacer(), // 平均分配间隔
  78. const SizedBox(width: 30),
  79. ];
  80. }).toList()
  81. ..removeLast(), // 删除最后一个Spacer
  82. )
  83. ),
  84. const SizedBox(height: 35,),
  85. const Padding(
  86. padding: EdgeInsets.fromLTRB(30, 0, 30, 0),
  87. child: Row(
  88. children: [
  89. Text("http://127.0.0.1:5336"),
  90. Spacer(),
  91. Text("socks://127.0.0.1:5337"),
  92. ],
  93. ),
  94. ),
  95. Obx(() {
  96. return ConnectionWidget(
  97. status: controller.connectStatus.value, onTap: () {
  98. controller.SetSysProxy();
  99. },);
  100. }),
  101. Padding(
  102. padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
  103. child: SizedBox(
  104. width: 200,
  105. height: 40,
  106. child: ElevatedButton(
  107. onPressed: () {
  108. controller.RouteNode();
  109. },
  110. style: ElevatedButton.styleFrom(
  111. primary: Colors.white, // 设置背景颜色为白色
  112. onPrimary: Colors.black, // 设置文字颜色为黑色,确保在白色背景上可见
  113. shape: RoundedRectangleBorder(
  114. borderRadius: BorderRadius.circular(20), // 设置圆角
  115. ),
  116. ),
  117. child: Text(controller.GetNode()),
  118. ),
  119. ),
  120. ),
  121. ],
  122. );
  123. })
  124. ),
  125. );
  126. }
  127. }
  128. class UserStatusWidget extends StatefulWidget {
  129. final Future<void> Function() onRefresh; // 新增的回调
  130. final bool isActive;
  131. final bool isLoading; // 新增的变量
  132. final String username;
  133. final String userTraffic;
  134. final String expiryDate;
  135. const UserStatusWidget(
  136. {Key? key, required this.isActive, required this.isLoading, required this.username, required this.userTraffic, required this.expiryDate, required this.onRefresh})
  137. : super(key: key);
  138. @override
  139. _UserStatusWidgetState createState() => _UserStatusWidgetState();
  140. }
  141. class _UserStatusWidgetState extends State<UserStatusWidget> {
  142. @override
  143. Widget build(BuildContext context) {
  144. String imagePath = widget.isActive
  145. ? "assets/images/main/userstatus.png"
  146. : "assets/images/main/userstatusoff.png";
  147. return Align(
  148. alignment: Alignment.topCenter,
  149. child: Padding(
  150. padding: const EdgeInsets.fromLTRB(10.0, 0, 10.0, 10.0),
  151. child:
  152. Row(
  153. children: [
  154. const SizedBox(
  155. width: 80,
  156. height: 20,
  157. ),
  158. Column(
  159. crossAxisAlignment: CrossAxisAlignment.start, // 这将使子组件从左边开始对齐
  160. children: [
  161. Row(
  162. children: [
  163. Text(widget.username),
  164. const SizedBox(width: 10,),
  165. Image(
  166. image: AssetImage(imagePath),
  167. width: 80,
  168. height: 30,
  169. ),
  170. const SizedBox(width: 10,),
  171. IconButton(
  172. icon: widget.isLoading
  173. ? const CircularProgressIndicator()
  174. : Image.asset("assets/images/main/refresh.png"),
  175. onPressed: () {
  176. // 刷新操作
  177. if (!widget.isLoading) {
  178. widget.onRefresh();
  179. }
  180. },
  181. )
  182. ],
  183. ),
  184. Text(
  185. widget.expiryDate,
  186. style: const TextStyle(
  187. fontSize: 8.0, // 设置字体大小为20像素
  188. ),
  189. ),
  190. Text(
  191. widget.userTraffic,
  192. style: const TextStyle(
  193. fontSize: 8.0, // 设置字体大小为20像素
  194. ),
  195. )
  196. ],
  197. ),
  198. ],
  199. ),
  200. // const SizedBox(height: 10,), // 可调整间隔
  201. ),
  202. );
  203. }
  204. }