home_view.dart 12 KB

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