deviceauth_view.dart 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:get/get.dart';
  5. import 'package:nomo/app/base/base_view.dart';
  6. import 'package:nomo/app/widgets/click_opacity.dart';
  7. import 'package:nomo/app/widgets/ix_app_bar.dart';
  8. import 'package:nomo/config/theme/theme_extensions/theme_extension.dart';
  9. import 'package:pinput/pinput.dart';
  10. import '../controllers/deviceauth_controller.dart';
  11. class DeviceauthView extends BaseView<DeviceauthController> {
  12. const DeviceauthView({super.key});
  13. @override
  14. PreferredSizeWidget? get appBar => IXAppBar(title: 'Device Authorization');
  15. @override
  16. Widget buildContent(BuildContext context) {
  17. return Obx(() {
  18. final isPremium = controller.isPremium.value;
  19. return SingleChildScrollView(
  20. padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 20.h),
  21. child: Column(
  22. children: [
  23. if (isPremium) ...[
  24. // VIP用户界面
  25. _buildVIPUserInterface(),
  26. ] else ...[
  27. // 免费用户界面
  28. _buildFreeUserInterface(),
  29. ],
  30. ],
  31. ),
  32. );
  33. });
  34. }
  35. /// 免费用户界面 - 显示授权码和等待授权
  36. Widget _buildFreeUserInterface() {
  37. return Column(
  38. children: [
  39. // 授权码显示区域
  40. _buildAuthCodeDisplay(),
  41. 30.verticalSpaceFromWidth,
  42. // 说明卡片
  43. _buildInstructionCards(),
  44. ],
  45. );
  46. }
  47. /// VIP用户界面 - 输入授权码和管理设备
  48. Widget _buildVIPUserInterface() {
  49. return Column(
  50. children: [
  51. // 6个输入框在顶部
  52. _buildTopCodeInput(),
  53. 20.verticalSpaceFromWidth,
  54. // 设备管理区域
  55. _buildDeviceManagementSection(),
  56. 20.verticalSpaceFromWidth,
  57. // 授权步骤说明
  58. _buildAuthorizationSteps(),
  59. ],
  60. );
  61. }
  62. /// 授权码显示区域
  63. Widget _buildAuthCodeDisplay() {
  64. return Container(
  65. padding: EdgeInsets.all(24.w),
  66. decoration: BoxDecoration(
  67. color: Get.reactiveTheme.highlightColor,
  68. borderRadius: BorderRadius.circular(16.r),
  69. ),
  70. child: Column(
  71. children: [
  72. // 6位授权码
  73. Obx(
  74. () => Text(
  75. controller.authCode.value,
  76. style: TextStyle(
  77. fontSize: 48.sp,
  78. fontWeight: FontWeight.bold,
  79. color: Colors.white,
  80. letterSpacing: 4.w,
  81. ),
  82. ),
  83. ),
  84. 20.verticalSpaceFromWidth,
  85. // 倒计时和复制按钮
  86. Row(
  87. mainAxisAlignment: MainAxisAlignment.center,
  88. children: [
  89. Icon(
  90. Icons.access_time,
  91. size: 16.w,
  92. color: Get.reactiveTheme.hintColor,
  93. ),
  94. SizedBox(width: 8.w),
  95. Obx(
  96. () => Text(
  97. controller.countdownText,
  98. style: TextStyle(
  99. fontSize: 16.sp,
  100. color: Get.reactiveTheme.hintColor,
  101. ),
  102. ),
  103. ),
  104. SizedBox(width: 16.w),
  105. Container(
  106. width: 1,
  107. height: 20.h,
  108. color: Get.reactiveTheme.hintColor.withOpacity(0.3),
  109. ),
  110. SizedBox(width: 16.w),
  111. ClickOpacity(
  112. onTap: () {
  113. Clipboard.setData(
  114. ClipboardData(text: controller.authCode.value),
  115. );
  116. controller.copyAuthCode();
  117. },
  118. child: Row(
  119. mainAxisSize: MainAxisSize.min,
  120. children: [
  121. Text(
  122. 'Copy',
  123. style: TextStyle(
  124. fontSize: 16.sp,
  125. color: Get.reactiveTheme.hintColor,
  126. ),
  127. ),
  128. SizedBox(width: 4.w),
  129. Icon(
  130. Icons.copy,
  131. size: 16.w,
  132. color: Get.reactiveTheme.hintColor,
  133. ),
  134. ],
  135. ),
  136. ),
  137. ],
  138. ),
  139. 20.verticalSpaceFromWidth,
  140. // 提示文字
  141. Text(
  142. 'Please keep this page open.',
  143. style: TextStyle(
  144. fontSize: 16.sp,
  145. color: const Color(0xFFFFB800),
  146. fontWeight: FontWeight.w500,
  147. ),
  148. ),
  149. ],
  150. ),
  151. );
  152. }
  153. /// 说明卡片
  154. Widget _buildInstructionCards() {
  155. return Container(
  156. padding: EdgeInsets.all(20.w),
  157. decoration: BoxDecoration(
  158. color: Get.reactiveTheme.hintColor.withOpacity(0.1),
  159. borderRadius: BorderRadius.circular(16.r),
  160. border: Border.all(
  161. color: const Color(0xFF00A8E8).withOpacity(0.3),
  162. width: 1,
  163. ),
  164. ),
  165. child: Column(
  166. children: [
  167. _buildInstructionItem(
  168. icon: Icons.vpn_key,
  169. title: 'Authorization Code',
  170. description:
  171. 'This 6-digit code allows a VIP user to link your device. It refreshes every 15 minutes.',
  172. ),
  173. 20.verticalSpaceFromWidth,
  174. _buildInstructionItem(
  175. icon: Icons.workspace_premium,
  176. title: 'Share with Pre User',
  177. description:
  178. 'Tell the VIP user this code so they can enter it on their device to authorize you.',
  179. ),
  180. 20.verticalSpaceFromWidth,
  181. _buildInstructionItem(
  182. icon: Icons.access_time,
  183. title: 'Waiting for Authorization',
  184. description:
  185. 'Please keep this page open.\nOnce approved, your account will automatically upgrade and reconnect.',
  186. highlightText: 'Please keep this page open.',
  187. ),
  188. ],
  189. ),
  190. );
  191. }
  192. /// 说明条目
  193. Widget _buildInstructionItem({
  194. required IconData icon,
  195. required String title,
  196. required String description,
  197. String? highlightText,
  198. }) {
  199. return Row(
  200. crossAxisAlignment: CrossAxisAlignment.start,
  201. children: [
  202. Container(
  203. width: 40.w,
  204. height: 40.w,
  205. decoration: BoxDecoration(
  206. color: const Color(0xFF00A8E8).withOpacity(0.2),
  207. borderRadius: BorderRadius.circular(8.r),
  208. ),
  209. child: Icon(icon, size: 20.w, color: const Color(0xFF00A8E8)),
  210. ),
  211. SizedBox(width: 16.w),
  212. Expanded(
  213. child: Column(
  214. crossAxisAlignment: CrossAxisAlignment.start,
  215. children: [
  216. Text(
  217. title,
  218. style: TextStyle(
  219. fontSize: 16.sp,
  220. fontWeight: FontWeight.bold,
  221. color: Colors.white,
  222. ),
  223. ),
  224. SizedBox(height: 8.h),
  225. highlightText != null
  226. ? RichText(
  227. text: TextSpan(
  228. children: [
  229. TextSpan(
  230. text: highlightText,
  231. style: TextStyle(
  232. fontSize: 14.sp,
  233. color: const Color(0xFF00A8E8),
  234. ),
  235. ),
  236. TextSpan(
  237. text:
  238. '\n${description.substring(highlightText.length + 1)}',
  239. style: TextStyle(
  240. fontSize: 14.sp,
  241. color: Get.reactiveTheme.hintColor,
  242. ),
  243. ),
  244. ],
  245. ),
  246. )
  247. : Text(
  248. description,
  249. style: TextStyle(
  250. fontSize: 14.sp,
  251. color: Get.reactiveTheme.hintColor,
  252. height: 1.4,
  253. ),
  254. ),
  255. ],
  256. ),
  257. ),
  258. ],
  259. );
  260. }
  261. /// 顶部6个输入框
  262. Widget _buildTopCodeInput() {
  263. return Container(
  264. padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.h),
  265. decoration: BoxDecoration(
  266. color: Get.reactiveTheme.highlightColor,
  267. borderRadius: BorderRadius.circular(16.r),
  268. ),
  269. child: Obx(
  270. () => Pinput(
  271. length: 6,
  272. defaultPinTheme: PinTheme(
  273. width: 40.w,
  274. height: 50.h,
  275. textStyle: TextStyle(
  276. fontSize: 24.sp,
  277. fontWeight: FontWeight.bold,
  278. color: Colors.white,
  279. ),
  280. decoration: BoxDecoration(
  281. color: Get.reactiveTheme.hintColor.withOpacity(0.1),
  282. borderRadius: BorderRadius.circular(8.r),
  283. border: Border.all(color: Colors.transparent, width: 2),
  284. ),
  285. ),
  286. focusedPinTheme: PinTheme(
  287. width: 40.w,
  288. height: 50.h,
  289. textStyle: TextStyle(
  290. fontSize: 24.sp,
  291. fontWeight: FontWeight.bold,
  292. color: Colors.white,
  293. ),
  294. decoration: BoxDecoration(
  295. color: const Color(0xFF00A8E8).withOpacity(0.2),
  296. borderRadius: BorderRadius.circular(8.r),
  297. border: Border.all(color: const Color(0xFF00A8E8), width: 2),
  298. ),
  299. ),
  300. submittedPinTheme: PinTheme(
  301. width: 40.w,
  302. height: 50.h,
  303. textStyle: TextStyle(
  304. fontSize: 24.sp,
  305. fontWeight: FontWeight.bold,
  306. color: Colors.white,
  307. ),
  308. decoration: BoxDecoration(
  309. color: const Color(0xFF00A8E8).withOpacity(0.2),
  310. borderRadius: BorderRadius.circular(8.r),
  311. border: Border.all(color: Colors.transparent, width: 2),
  312. ),
  313. ),
  314. onChanged: (value) {
  315. controller.setInputCode(value);
  316. },
  317. onCompleted: (pin) {
  318. controller.submitAuthCode();
  319. },
  320. inputFormatters: [FilteringTextInputFormatter.digitsOnly],
  321. keyboardType: TextInputType.number,
  322. showCursor: true,
  323. cursor: Container(
  324. width: 2,
  325. height: 20.h,
  326. color: const Color(0xFF00A8E8),
  327. ),
  328. ),
  329. ),
  330. );
  331. }
  332. /// 设备管理区域
  333. Widget _buildDeviceManagementSection() {
  334. return Container(
  335. padding: EdgeInsets.all(20.w),
  336. decoration: BoxDecoration(
  337. color: Get.reactiveTheme.highlightColor,
  338. borderRadius: BorderRadius.circular(16.r),
  339. ),
  340. child: Column(
  341. crossAxisAlignment: CrossAxisAlignment.start,
  342. children: [
  343. // 标题
  344. Row(
  345. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  346. children: [
  347. Text(
  348. 'Manage Devices',
  349. style: TextStyle(
  350. fontSize: 18.sp,
  351. fontWeight: FontWeight.bold,
  352. color: Colors.white,
  353. ),
  354. ),
  355. Obx(
  356. () => Text(
  357. controller.deviceCountText,
  358. style: TextStyle(
  359. fontSize: 16.sp,
  360. color: Get.reactiveTheme.hintColor,
  361. ),
  362. ),
  363. ),
  364. ],
  365. ),
  366. 20.verticalSpaceFromWidth,
  367. // 设备列表
  368. Obx(
  369. () => Column(
  370. children: [
  371. ...controller.currentDevices.map((device) {
  372. return _buildDeviceCard(device);
  373. }).toList(),
  374. // 等待激活的设备
  375. if (controller.inputCode.value.isEmpty)
  376. _buildAwaitingDeviceCard(),
  377. ],
  378. ),
  379. ),
  380. 20.verticalSpaceFromWidth,
  381. // 授权限制说明
  382. Text(
  383. 'Authorize up to 4 devices as Premium (${controller.deviceCountText})',
  384. style: TextStyle(
  385. fontSize: 14.sp,
  386. color: Get.reactiveTheme.hintColor,
  387. ),
  388. ),
  389. ],
  390. ),
  391. );
  392. }
  393. /// 等待激活的设备卡片
  394. Widget _buildAwaitingDeviceCard() {
  395. return Container(
  396. margin: EdgeInsets.only(bottom: 12.h),
  397. padding: EdgeInsets.all(16.w),
  398. decoration: BoxDecoration(
  399. color: Get.reactiveTheme.hintColor.withOpacity(0.1),
  400. borderRadius: BorderRadius.circular(12.r),
  401. ),
  402. child: Row(
  403. children: [
  404. // 设备图标
  405. Container(
  406. width: 40.w,
  407. height: 40.w,
  408. decoration: BoxDecoration(
  409. color: Get.reactiveTheme.hintColor.withOpacity(0.2),
  410. borderRadius: BorderRadius.circular(8.r),
  411. ),
  412. child: Icon(
  413. Icons.phone_iphone,
  414. size: 20.w,
  415. color: Get.reactiveTheme.hintColor,
  416. ),
  417. ),
  418. SizedBox(width: 12.w),
  419. // 设备信息
  420. Expanded(
  421. child: Text(
  422. 'Awaiting Activation',
  423. style: TextStyle(
  424. fontSize: 16.sp,
  425. fontWeight: FontWeight.w500,
  426. color: Colors.white,
  427. ),
  428. ),
  429. ),
  430. ],
  431. ),
  432. );
  433. }
  434. /// 设备卡片
  435. Widget _buildDeviceCard(DeviceInfo device) {
  436. return Container(
  437. margin: EdgeInsets.only(bottom: 12.h),
  438. padding: EdgeInsets.all(16.w),
  439. decoration: BoxDecoration(
  440. color: Get.reactiveTheme.hintColor.withOpacity(0.1),
  441. borderRadius: BorderRadius.circular(12.r),
  442. ),
  443. child: Row(
  444. children: [
  445. // 设备图标
  446. Container(
  447. width: 40.w,
  448. height: 40.w,
  449. decoration: BoxDecoration(
  450. color: Get.reactiveTheme.hintColor.withOpacity(0.2),
  451. borderRadius: BorderRadius.circular(8.r),
  452. ),
  453. child: Icon(
  454. _getDeviceIcon(device.type),
  455. size: 20.w,
  456. color: Get.reactiveTheme.hintColor,
  457. ),
  458. ),
  459. SizedBox(width: 12.w),
  460. // 设备信息
  461. Expanded(
  462. child: Column(
  463. crossAxisAlignment: CrossAxisAlignment.start,
  464. children: [
  465. Text(
  466. device.name,
  467. style: TextStyle(
  468. fontSize: 16.sp,
  469. fontWeight: FontWeight.w500,
  470. color: Colors.white,
  471. ),
  472. ),
  473. if (device.uid != null) ...[
  474. SizedBox(height: 4.h),
  475. Text(
  476. device.uid!,
  477. style: TextStyle(
  478. fontSize: 14.sp,
  479. color: Get.reactiveTheme.hintColor,
  480. ),
  481. ),
  482. ],
  483. if (device.date != null) ...[
  484. SizedBox(height: 4.h),
  485. Text(
  486. device.date!,
  487. style: TextStyle(
  488. fontSize: 12.sp,
  489. color: Get.reactiveTheme.hintColor,
  490. ),
  491. ),
  492. ],
  493. ],
  494. ),
  495. ),
  496. // 操作按钮
  497. if (device.isCurrent)
  498. ClickOpacity(
  499. onTap: () => controller.removeDevice(device.id),
  500. child: Container(
  501. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 6.h),
  502. decoration: BoxDecoration(
  503. color: Get.reactiveTheme.hintColor.withOpacity(0.2),
  504. borderRadius: BorderRadius.circular(6.r),
  505. ),
  506. child: Row(
  507. mainAxisSize: MainAxisSize.min,
  508. children: [
  509. Icon(
  510. Icons.delete,
  511. size: 16.w,
  512. color: Get.reactiveTheme.hintColor,
  513. ),
  514. SizedBox(width: 4.w),
  515. Text(
  516. 'Relieve',
  517. style: TextStyle(
  518. fontSize: 12.sp,
  519. color: Get.reactiveTheme.hintColor,
  520. ),
  521. ),
  522. ],
  523. ),
  524. ),
  525. )
  526. else
  527. ClickOpacity(
  528. onTap: () => controller.removeDevice(device.id),
  529. child: Container(
  530. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 6.h),
  531. decoration: BoxDecoration(
  532. color: const Color(0xFF00A8E8),
  533. borderRadius: BorderRadius.circular(6.r),
  534. ),
  535. child: Text(
  536. 'Relieve',
  537. style: TextStyle(
  538. fontSize: 12.sp,
  539. color: Colors.white,
  540. fontWeight: FontWeight.w500,
  541. ),
  542. ),
  543. ),
  544. ),
  545. ],
  546. ),
  547. );
  548. }
  549. /// 获取设备图标
  550. IconData _getDeviceIcon(DeviceTypeEnum type) {
  551. switch (type) {
  552. case DeviceTypeEnum.ios:
  553. return Icons.phone_iphone;
  554. case DeviceTypeEnum.android:
  555. return Icons.android;
  556. case DeviceTypeEnum.windows:
  557. return Icons.laptop_windows;
  558. case DeviceTypeEnum.mac:
  559. return Icons.laptop_mac;
  560. }
  561. }
  562. /// 授权步骤说明
  563. Widget _buildAuthorizationSteps() {
  564. return Container(
  565. padding: EdgeInsets.all(20.w),
  566. decoration: BoxDecoration(
  567. color: Get.reactiveTheme.highlightColor,
  568. borderRadius: BorderRadius.circular(16.r),
  569. ),
  570. child: Column(
  571. crossAxisAlignment: CrossAxisAlignment.start,
  572. children: [
  573. Text(
  574. 'Authorization Steps',
  575. style: TextStyle(
  576. fontSize: 18.sp,
  577. fontWeight: FontWeight.bold,
  578. color: Colors.white,
  579. ),
  580. ),
  581. 20.verticalSpaceFromWidth,
  582. _buildStepItem(
  583. stepNumber: 1,
  584. title: 'Enter Code',
  585. description:
  586. 'Input the 6-digit code shown on the other device (free user). This code refreshes every 15 minutes.',
  587. icon: Icons.input,
  588. isCompleted: controller.inputCode.value.isNotEmpty,
  589. ),
  590. 16.verticalSpaceFromWidth,
  591. _buildStepItem(
  592. stepNumber: 2,
  593. title: 'Verify Device',
  594. description:
  595. 'We\'ll check if the entered code matches an active device waiting for authorization.',
  596. icon: Icons.verified_user,
  597. isCompleted: controller.isCodeComplete.value,
  598. ),
  599. 16.verticalSpaceFromWidth,
  600. _buildStepItem(
  601. stepNumber: 3,
  602. title: 'Authorization Successful',
  603. description:
  604. 'Once confirmed, the device will automatically upgrade and link to your account.',
  605. icon: Icons.check_circle,
  606. isCompleted:
  607. controller.authorizationStatus.value ==
  608. AuthorizationStatus.success,
  609. ),
  610. ],
  611. ),
  612. );
  613. }
  614. /// 步骤条目
  615. Widget _buildStepItem({
  616. required int stepNumber,
  617. required String title,
  618. required String description,
  619. required IconData icon,
  620. required bool isCompleted,
  621. }) {
  622. return Row(
  623. crossAxisAlignment: CrossAxisAlignment.start,
  624. children: [
  625. Container(
  626. width: 32.w,
  627. height: 32.w,
  628. decoration: BoxDecoration(
  629. color: isCompleted
  630. ? const Color(0xFF00D9A3)
  631. : Get.reactiveTheme.hintColor.withOpacity(0.2),
  632. borderRadius: BorderRadius.circular(8.r),
  633. ),
  634. child: Center(
  635. child: isCompleted
  636. ? Icon(Icons.check, size: 16.w, color: Colors.white)
  637. : Icon(icon, size: 16.w, color: Get.reactiveTheme.hintColor),
  638. ),
  639. ),
  640. SizedBox(width: 12.w),
  641. Expanded(
  642. child: Column(
  643. crossAxisAlignment: CrossAxisAlignment.start,
  644. children: [
  645. Text(
  646. title,
  647. style: TextStyle(
  648. fontSize: 16.sp,
  649. fontWeight: FontWeight.w500,
  650. color: Colors.white,
  651. ),
  652. ),
  653. SizedBox(height: 4.h),
  654. Text(
  655. description,
  656. style: TextStyle(
  657. fontSize: 14.sp,
  658. color: Get.reactiveTheme.hintColor,
  659. height: 1.4,
  660. ),
  661. ),
  662. ],
  663. ),
  664. ),
  665. ],
  666. );
  667. }
  668. }