home_view.dart 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/button_select.dart';
  6. import 'package:naiyouwl/app/component/connection_widget.dart';
  7. import 'package:naiyouwl/app/component/sys_app_bar.dart';
  8. import 'package:naiyouwl/app/controller/controllers.dart';
  9. import 'package:naiyouwl/app/utils/system_proxy.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. IconButton(onPressed: (){
  34. controller.outlogin();
  35. }, icon: const Icon(Icons.exit_to_app))
  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. final disabled = !controllers.service.isRunning;
  48. return controller.isLoading.value ? const Center(
  49. child: CircularProgressIndicator()) : Column(
  50. children: [
  51. // 错误消息展示
  52. UserStatusWidget(
  53. isActive: controller.GetEnable(),
  54. isLoading: controller.isLoading.value,
  55. username: controller.GetUserName(),
  56. expiryDate: controller.GetExpiredAt(),
  57. userTraffic: controller.GetTraffic(),
  58. onRefresh: () async {
  59. // 这里插入刷新操作代码
  60. //await Future.delayed(Duration(seconds: 2));
  61. if (controller.isLoading.value != true) {
  62. controller.fetchUserinfo();
  63. }
  64. },),
  65. Padding(
  66. padding: const EdgeInsets.fromLTRB(20, 20, 0, 0),
  67. child: Row(
  68. children: controller.imageMap.entries.expand((entry) {
  69. return [
  70. GestureDetector(
  71. onTap: () => controller.onImageTap(entry.key),
  72. child: Image(
  73. image: AssetImage(entry.value),
  74. width: 60,
  75. height: 60,
  76. ),
  77. ),
  78. //Spacer(), // 平均分配间隔
  79. const SizedBox(width: 30),
  80. ];
  81. }).toList()
  82. ..removeLast(), // 删除最后一个Spacer
  83. )
  84. ),
  85. const SizedBox(height: 30,),
  86. Padding(
  87. padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
  88. child: Row(
  89. children: [
  90. Text(controller.getHttp().toString()),
  91. const Spacer(),
  92. Text(controller.getSocket().toString()),
  93. ],
  94. ),
  95. ),
  96. Obx(() {
  97. return ConnectionWidget(
  98. status: controller.connectStatus.value, onTap: () {
  99. controller.fetchAuthUser();
  100. },);
  101. }),
  102. Padding(
  103. padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
  104. child: SizedBox(
  105. width: 200,
  106. height: 40,
  107. child: ElevatedButton(
  108. onPressed: () {
  109. controller.RouteNode();
  110. },
  111. style: ElevatedButton.styleFrom(
  112. primary: Colors.white, // 设置背景颜色为白色
  113. onPrimary: Colors.black, // 设置文字颜色为黑色,确保在白色背景上可见
  114. shape: RoundedRectangleBorder(
  115. borderRadius: BorderRadius.circular(20), // 设置圆角
  116. ),
  117. ),
  118. child: Text(controller.GetNode()),
  119. ),
  120. ),
  121. ),
  122. const SizedBox(height: 40,),
  123. Align(
  124. alignment: Alignment.center,
  125. child: Padding(
  126. padding: const EdgeInsets.fromLTRB(60, 0, 50, 0),
  127. child: Row(
  128. children: [
  129. ButtonSelect(
  130. labels: ['setting_mode_rules'.tr,'setting_mode_global'.tr,],
  131. value: controllers.global.modes.indexOf(controllers.global.modesSelect.value),
  132. onSelect: (idx) => {
  133. controllers.global.updateMode(controllers.global.modes[idx])
  134. },
  135. ),
  136. const SizedBox(width: 10,),
  137. ButtonSelect(
  138. labels: ['route_sys_tile'.tr,'route_tun_title'.tr,],
  139. value: controllers.global.routeModes.indexOf(controllers.global.routeModesSelect.value),
  140. onSelect: (idx) => {
  141. controllers.global.updateRoute(controllers.global.routeModes[idx])
  142. }
  143. ),
  144. ],
  145. ),
  146. ),
  147. ),
  148. // const SizedBox(height: 20,),
  149. // Align(
  150. // alignment: Alignment.center,
  151. // child:,
  152. // )
  153. ],
  154. );
  155. })
  156. ),
  157. );
  158. }
  159. }
  160. class UserStatusWidget extends StatefulWidget {
  161. final Future<void> Function() onRefresh; // 新增的回调
  162. final bool isActive;
  163. final bool isLoading; // 新增的变量
  164. final String username;
  165. final String userTraffic;
  166. final String expiryDate;
  167. const UserStatusWidget(
  168. {Key? key, required this.isActive, required this.isLoading, required this.username, required this.userTraffic, required this.expiryDate, required this.onRefresh})
  169. : super(key: key);
  170. @override
  171. _UserStatusWidgetState createState() => _UserStatusWidgetState();
  172. }
  173. class _UserStatusWidgetState extends State<UserStatusWidget> {
  174. @override
  175. Widget build(BuildContext context) {
  176. String imagePath = widget.isActive
  177. ? "assets/images/main/userstatus.png"
  178. : "assets/images/main/userstatusoff.png";
  179. return Align(
  180. alignment: Alignment.topCenter,
  181. child: Padding(
  182. padding: const EdgeInsets.fromLTRB(5.0, 0, 5.0, 10.0),
  183. child:
  184. Row(
  185. children: [
  186. const SizedBox(
  187. width: 80,
  188. height: 20,
  189. ),
  190. Column(
  191. crossAxisAlignment: CrossAxisAlignment.start, // 这将使子组件从左边开始对齐
  192. children: [
  193. Row(
  194. children: [
  195. Text(widget.username),
  196. const SizedBox(width: 10,),
  197. Image(
  198. image: AssetImage(imagePath),
  199. width: 80,
  200. height: 30,
  201. ),
  202. const SizedBox(width: 10,),
  203. IconButton(
  204. icon: widget.isLoading
  205. ? const CircularProgressIndicator()
  206. : Image.asset("assets/images/main/refresh.png"),
  207. onPressed: () {
  208. // 刷新操作
  209. if (!widget.isLoading) {
  210. widget.onRefresh();
  211. }
  212. },
  213. )
  214. ],
  215. ),
  216. Text(
  217. widget.expiryDate,
  218. style: const TextStyle(
  219. fontSize: 8.0, // 设置字体大小为20像素
  220. ),
  221. ),
  222. Text(
  223. widget.userTraffic,
  224. style: const TextStyle(
  225. fontSize: 8.0, // 设置字体大小为20像素
  226. ),
  227. )
  228. ],
  229. ),
  230. ],
  231. ),
  232. // const SizedBox(height: 10,), // 可调整间隔
  233. ),
  234. );
  235. }
  236. }