home_view.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import 'dart:ffi';
  2. import 'dart:io';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:get/get.dart';
  6. import 'package:naiyouwl/app/common/constants.dart';
  7. import 'package:naiyouwl/app/component/button_select.dart';
  8. import 'package:naiyouwl/app/component/connection_status.dart';
  9. import 'package:naiyouwl/app/component/connection_widget.dart';
  10. import 'package:naiyouwl/app/component/sys_app_bar.dart';
  11. import 'package:naiyouwl/app/controller/controllers.dart';
  12. import 'package:naiyouwl/app/utils/system_proxy.dart';
  13. import 'package:url_launcher/url_launcher.dart';
  14. import '../controllers/home_controller.dart';
  15. class HomeView extends GetView<HomeController> {
  16. const HomeView({Key? key}) : super(key: key);
  17. @override
  18. Widget build(BuildContext context) {
  19. return Container(
  20. decoration: const BoxDecoration(
  21. image: DecorationImage(
  22. image: AssetImage("assets/images/main/main.png"),
  23. fit: BoxFit.fill,
  24. ),
  25. ),
  26. child: Scaffold(
  27. backgroundColor: Colors.transparent,
  28. appBar: SysAppBar(title: const Text(""), actions: [
  29. Row(
  30. children: [
  31. IconButton(
  32. icon: const Icon(Icons.refresh),
  33. tooltip: '刷新节点',
  34. onPressed: () {
  35. controller.fetchNode();
  36. },
  37. ),
  38. IconButton(onPressed: (){
  39. controller.outlogin();
  40. } ,tooltip: '切换账号', icon: const Icon(Icons.exit_to_app))
  41. ],
  42. ),
  43. ],),
  44. body: Obx(() {
  45. if(controller.errAdminMsg.value){
  46. controller.errAdminMsg.value = false;
  47. Future.delayed(Duration.zero, () async {
  48. bool? result = await controllers.dialog.showNormalDialog(
  49. title: "提示",
  50. content: controller.errAdmin.value,
  51. cancelText: "取消",
  52. enterText: "确定",
  53. );
  54. if (result != null && result) {
  55. if(Platform.isWindows){
  56. await controllers.global.onRunAdmin();
  57. }
  58. } else {
  59. // User pressed "取消" or dismissed the dialog
  60. }
  61. });
  62. }
  63. if (controllers.global.errorMsg.isNotEmpty) {
  64. // WidgetsBinding.instance.addPostFrameCallback((_) {
  65. // ScaffoldMessenger.of(context).showSnackBar(
  66. // SnackBar(content: Text(controller.errorMsg.value))
  67. // );
  68. // });
  69. String message = controllers.global.errorMsg.value;
  70. controllers.global.errorMsg.value = ''; // Clear the error message immediately
  71. // Show the dialog and wait for the result
  72. Future.delayed(Duration.zero, () async {
  73. bool? result = await controllers.dialog.showNormalDialog(
  74. title: "提示",
  75. content: message,
  76. cancelText: "取消",
  77. enterText: "确认",
  78. );
  79. if (result != null && result) {
  80. // User pressed "确认"
  81. if(controllers.global.statusCode.value == 445){
  82. await launchUrl(Uri.parse(controller.UsersysConfig.value.userBuy ?? ""));
  83. }
  84. } else {
  85. // User pressed "取消" or dismissed the dialog
  86. }
  87. });
  88. }
  89. final disabled = !controllers.service.isRunning;
  90. return controller.isLoading.value ? const Center(
  91. child: CircularProgressIndicator()) : Column(
  92. children: [
  93. // 错误消息展示
  94. UserStatusWidget(
  95. isActive: controller.GetEnable(),
  96. isLoading: controller.isLoading.value,
  97. username: controller.GetUserName(),
  98. expiryDate: controller.GetExpiredAt(),
  99. userTraffic: controller.GetTraffic(),
  100. onRefresh: () async {
  101. // 这里插入刷新操作代码
  102. //await Future.delayed(Duration(seconds: 2));
  103. if (controller.isLoading.value != true) {
  104. controller.fetchUserinfo();
  105. }
  106. },),
  107. Padding(
  108. padding: const EdgeInsets.fromLTRB(20, 20, 0, 0),
  109. child: Row(
  110. children: controller.imageMap.entries.expand((entry) {
  111. return [
  112. GestureDetector(
  113. onTap: () => controller.onImageTap(entry.key),
  114. child: Image(
  115. image: AssetImage(entry.value),
  116. width: 60,
  117. height: 60,
  118. ),
  119. ),
  120. //Spacer(), // 平均分配间隔
  121. const SizedBox(width: 30),
  122. ];
  123. }).toList()
  124. ..removeLast(), // 删除最后一个Spacer
  125. )
  126. ),
  127. const SizedBox(height: 35,),
  128. Padding(
  129. padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
  130. child: Row(
  131. children: [
  132. Text(controller.getHttp().toString()),
  133. const Spacer(),
  134. Text(controller.getSocket().toString()),
  135. ],
  136. ),
  137. ),
  138. ConnectionWidget(
  139. status: controller.connectStatus.value, onTap: () {
  140. if(controller.isConnect.value){
  141. controller.stopVpn();
  142. } else
  143. {
  144. controller.fetchAuthUser();
  145. }
  146. },),
  147. Align(
  148. alignment: Alignment.center,
  149. child: Text(controllers.global.msgStatus.value),
  150. ),
  151. const SizedBox(height: 20,),
  152. Padding(
  153. padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
  154. child: SizedBox(
  155. width: 200,
  156. height: 40,
  157. child: ElevatedButton(
  158. onPressed: () {
  159. controller.RouteNode();
  160. },
  161. style: ElevatedButton.styleFrom(
  162. primary: Colors.white, // 设置背景颜色为白色
  163. onPrimary: Colors.black, // 设置文字颜色为黑色,确保在白色背景上可见
  164. shape: RoundedRectangleBorder(
  165. borderRadius: BorderRadius.circular(20), // 设置圆角
  166. ),
  167. ),
  168. child: Text(controller.GetNode()),
  169. ),
  170. ),
  171. ),
  172. const SizedBox(height: 20,),
  173. Center(
  174. child: Row(
  175. mainAxisAlignment: MainAxisAlignment.center,
  176. children: [
  177. ButtonSelect(
  178. labels: ['setting_mode_rules'.tr,'setting_mode_global'.tr,],
  179. value: controllers.global.modes.indexOf(controllers.global.modesSelect.value),
  180. onSelect: (idx) => {
  181. controllers.global.updateMode(controllers.global.modes[idx])
  182. },
  183. ),
  184. // const SizedBox(width: 10,),
  185. // ButtonSelect(
  186. // labels: ['route_sys_tile'.tr,'route_tun_title'.tr,],
  187. // value: controllers.global.routeModes.indexOf(controllers.global.routeModesSelect.value),
  188. // onSelect: (idx) => {
  189. // controllers.global.updateRoute(controllers.global.routeModes[idx])
  190. // }
  191. // ),
  192. ],
  193. ),
  194. ),
  195. controllers.service.serviceMode.value == true ? Padding(
  196. padding: const EdgeInsets.fromLTRB(60, 10, 50, 0),
  197. child: Row(
  198. mainAxisAlignment: MainAxisAlignment.center,
  199. children: [
  200. ElevatedButton(onPressed: () async {
  201. await controllers.service.serviceModeSwitch(false);
  202. }, child: const Text("卸载服务")),
  203. ],
  204. ),
  205. ) : Container(),
  206. controller.isServiceInstall.value == true ? Padding(
  207. padding: const EdgeInsets.fromLTRB(60, 10, 50, 0),
  208. child: Row(
  209. mainAxisAlignment: MainAxisAlignment.center,
  210. children: [
  211. ElevatedButton(onPressed: () async {
  212. await controllers.service.serviceModeSwitch(true);
  213. controller.isServiceInstall.value = false;
  214. }, child: const Text("安装服务")),
  215. ],
  216. ),
  217. ) : Container(),
  218. // const SizedBox(height: 20,),
  219. // Align(
  220. // alignment: Alignment.center,
  221. // child:,
  222. // )
  223. Text("版本号:$kVersion"),
  224. const SizedBox(height: 10,),
  225. Text("服务端口${ controller.getSerivce() }"),
  226. controllers.global.sysInfo.value.isEmpty ? Container() : Text("当前:${controllers.global.sysInfo.value}"),
  227. ],
  228. );
  229. })
  230. ),
  231. );
  232. }
  233. }
  234. class UserStatusWidget extends StatefulWidget {
  235. final Future<void> Function() onRefresh; // 新增的回调
  236. final bool isActive;
  237. final bool isLoading; // 新增的变量
  238. final String username;
  239. final String userTraffic;
  240. final String expiryDate;
  241. const UserStatusWidget(
  242. {Key? key, required this.isActive, required this.isLoading, required this.username, required this.userTraffic, required this.expiryDate, required this.onRefresh})
  243. : super(key: key);
  244. @override
  245. _UserStatusWidgetState createState() => _UserStatusWidgetState();
  246. }
  247. class _UserStatusWidgetState extends State<UserStatusWidget> {
  248. @override
  249. Widget build(BuildContext context) {
  250. String imagePath = widget.isActive
  251. ? "assets/images/main/userstatus.png"
  252. : "assets/images/main/userstatusoff.png";
  253. return Align(
  254. alignment: Alignment.topCenter,
  255. child: Padding(
  256. padding: const EdgeInsets.fromLTRB(5.0, 0, 5.0, 0.0),
  257. child:
  258. Row(
  259. children: [
  260. const SizedBox(
  261. width: 80,
  262. height: 20,
  263. ),
  264. Column(
  265. crossAxisAlignment: CrossAxisAlignment.start, // 这将使子组件从左边开始对齐
  266. children: [
  267. Row(
  268. children: [
  269. Container(
  270. width: 150,
  271. child: Text(
  272. widget.username,
  273. overflow: TextOverflow.ellipsis, // 使用省略号
  274. softWrap: false, // 关闭文本自动换行
  275. maxLines: 1, // 设置文本最大行数为 1
  276. ),
  277. ),
  278. const SizedBox(width: 2,),
  279. Image(
  280. image: AssetImage(imagePath),
  281. width: 80,
  282. height: 30,
  283. ),
  284. const SizedBox(width: 10,),
  285. IconButton(
  286. tooltip: '刷新账号状态',
  287. icon: widget.isLoading
  288. ? const CircularProgressIndicator()
  289. : Image(image: AssetImage("assets/images/main/refresh.png"),width: 20,height: 20,),
  290. onPressed: () {
  291. // 刷新操作
  292. if (!widget.isLoading) {
  293. widget.onRefresh();
  294. }
  295. },
  296. )
  297. ],
  298. ),
  299. Text(
  300. widget.expiryDate,
  301. style: const TextStyle(
  302. fontSize: 12.0, // 设置字体大小为20像素
  303. ),
  304. ),
  305. Text(
  306. widget.userTraffic,
  307. style: const TextStyle(
  308. fontSize: 12.0, // 设置字体大小为20像素
  309. ),
  310. )
  311. ],
  312. ),
  313. ],
  314. ),
  315. // const SizedBox(height: 10,), // 可调整间隔
  316. ),
  317. );
  318. }
  319. }