home_view.dart 7.4 KB

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