home_view.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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:window_manager/window_manager.dart';
  7. import '../../../component/connection_status.dart';
  8. import '../../../component/connection_widget.dart';
  9. import '../../../global_controller/GlobalController.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("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(child: CircularProgressIndicator()) : Column(
  46. children: [
  47. // 错误消息展示
  48. UserStatusWidget(isActive: controller.GetEnable(),isLoading: controller.isLoading.value,username:controller.GetUserName(),expiryDate: controller.GetExpiredAt(),userTraffic:controller.GetTraffic(),onRefresh: () async {
  49. // 这里插入刷新操作代码
  50. //await Future.delayed(Duration(seconds: 2));
  51. if(controller.isLoading.value != true){
  52. controller.fetchUserinfo();
  53. }
  54. },),
  55. Padding(
  56. padding: const EdgeInsets.fromLTRB(20, 20, 0, 0),
  57. child: Row(
  58. children: controller.imageMap.entries.expand((entry) {
  59. return [
  60. GestureDetector(
  61. onTap: () => controller.onImageTap(entry.key),
  62. child: Image(
  63. image: AssetImage(entry.value),
  64. width: 60,
  65. height: 60,
  66. ),
  67. ),
  68. //Spacer(), // 平均分配间隔
  69. const SizedBox(width: 30),
  70. ];
  71. }).toList()
  72. ..removeLast(), // 删除最后一个Spacer
  73. )
  74. ),
  75. const SizedBox(height: 35,),
  76. const Padding(
  77. padding: EdgeInsets.fromLTRB(30, 0, 30, 0),
  78. child: Row(
  79. children: [
  80. Text("http://127.0.0.1:5336"),
  81. Spacer(),
  82. Text("socks://127.0.0.1:5337"),
  83. ],
  84. ),
  85. ),
  86. ConnectionWidget(
  87. status: ConnectionStatus.disconnected, onStatusChange: (con) {
  88. },),
  89. Padding(
  90. padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
  91. child: SizedBox(
  92. width: 200,
  93. height: 40,
  94. child: ElevatedButton(
  95. onPressed: () {
  96. controller.RouteNode();
  97. },
  98. style: ElevatedButton.styleFrom(
  99. primary: Colors.white, // 设置背景颜色为白色
  100. onPrimary: Colors.black, // 设置文字颜色为黑色,确保在白色背景上可见
  101. shape: RoundedRectangleBorder(
  102. borderRadius: BorderRadius.circular(20), // 设置圆角
  103. ),
  104. ),
  105. child: Text(controller.GetNode()),
  106. ),
  107. ),
  108. ),
  109. ],
  110. );
  111. })
  112. ),
  113. );
  114. }
  115. }
  116. class UserStatusWidget extends StatefulWidget {
  117. final Future<void> Function() onRefresh; // 新增的回调
  118. final bool isActive;
  119. final bool isLoading; // 新增的变量
  120. final String username;
  121. final String userTraffic;
  122. final String expiryDate;
  123. const UserStatusWidget({Key? key, required this.isActive, required this.isLoading,required this.username, required this.userTraffic, required this.expiryDate, required this.onRefresh}) : super(key: key);
  124. @override
  125. _UserStatusWidgetState createState() => _UserStatusWidgetState();
  126. }
  127. class _UserStatusWidgetState extends State<UserStatusWidget> {
  128. @override
  129. Widget build(BuildContext context) {
  130. String imagePath = widget.isActive
  131. ? "images/main/userstatus.png"
  132. : "images/main/userstatusoff.png";
  133. return Align(
  134. alignment: Alignment.topCenter,
  135. child: Padding(
  136. padding: const EdgeInsets.fromLTRB(10.0, 0, 10.0, 10.0),
  137. child:
  138. Row(
  139. children: [
  140. const SizedBox(
  141. width: 80,
  142. height: 20,
  143. ),
  144. Column(
  145. crossAxisAlignment: CrossAxisAlignment.start, // 这将使子组件从左边开始对齐
  146. children: [
  147. Row(
  148. children: [
  149. Text(widget.username),
  150. const SizedBox(width: 10,),
  151. Image(
  152. image: AssetImage(imagePath),
  153. width: 80,
  154. height: 30,
  155. ),
  156. const SizedBox(width: 10,),
  157. IconButton(
  158. icon: widget.isLoading ? const CircularProgressIndicator() : Image.asset("images/main/refresh.png"),
  159. onPressed: () {
  160. // 刷新操作
  161. if(!widget.isLoading){
  162. widget.onRefresh();
  163. }
  164. },
  165. )
  166. ],
  167. ),
  168. Text(
  169. widget.expiryDate,
  170. style: const TextStyle(
  171. fontSize: 8.0, // 设置字体大小为20像素
  172. ),
  173. ),
  174. Text(
  175. widget.userTraffic,
  176. style: const TextStyle(
  177. fontSize: 8.0, // 设置字体大小为20像素
  178. ),
  179. )
  180. ],
  181. ),
  182. ],
  183. ),
  184. // const SizedBox(height: 10,), // 可调整间隔
  185. ),
  186. );
  187. }
  188. }