search.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:vnote_device_plugin/devices/searcher.dart';
  5. import 'package:vnote_device_plugin/models/device.dart';
  6. class SearchDialog extends StatefulWidget {
  7. const SearchDialog({
  8. Key? key,
  9. required this.type,
  10. }) : super(key: key);
  11. final String type;
  12. static Future<DeviceInfo?> dialog(String type) async {
  13. return Get.dialog(
  14. AlertDialog(
  15. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
  16. title: const Text("搜索设备"),
  17. content: SizedBox(
  18. height: 280,
  19. width: 400,
  20. child: SearchDialog(type: type),
  21. ),
  22. actionsAlignment: MainAxisAlignment.center,
  23. actions: [
  24. SizedBox(
  25. width: 360,
  26. child: TextButton(
  27. onPressed: () {
  28. Get.back();
  29. },
  30. child: const Text(
  31. "取消",
  32. style: TextStyle(color: Colors.blue, fontSize: 20),
  33. ),
  34. ),
  35. ),
  36. ],
  37. ),
  38. barrierDismissible: false,
  39. );
  40. }
  41. @override
  42. State<StatefulWidget> createState() => _SearchDialogState();
  43. }
  44. class _SearchDialogState extends State<SearchDialog> {
  45. String? value;
  46. DeviceSearcher? searcher;
  47. @override
  48. void initState() {
  49. super.initState();
  50. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  51. if (mounted) {
  52. searcher = DeviceSearcher(widget.type);
  53. searcher!.init();
  54. searcher!.successEvent.addListener(onSuccess);
  55. searcher!.start();
  56. }
  57. });
  58. }
  59. @override
  60. void dispose() {
  61. if (searcher != null) {
  62. searcher!.successEvent.removeListener(onSuccess);
  63. searcher!.stop().then((value) {
  64. searcher!.dispose();
  65. searcher = null;
  66. });
  67. }
  68. super.dispose();
  69. }
  70. void onSuccess(_, DeviceInfo e) {
  71. if (value != null) return;
  72. print(jsonEncode(value.toString()));
  73. setState(() {
  74. value = e.mac;
  75. Future.delayed(const Duration(seconds: 2), () {
  76. Get.back(result: e);
  77. });
  78. });
  79. }
  80. @override
  81. Widget build(BuildContext context) {
  82. Widget child;
  83. if (value == null) {
  84. child = _buildWorking();
  85. } else {
  86. child = _buildDone();
  87. }
  88. return Container(
  89. alignment: Alignment.center,
  90. // decoration: BoxDecoration(borderRadius: BorderRadius.circular(24)),
  91. child: child,
  92. );
  93. }
  94. Widget _buildWorking() {
  95. return Column(
  96. mainAxisSize: MainAxisSize.min,
  97. mainAxisAlignment: MainAxisAlignment.center,
  98. children: const [
  99. SizedBox(height: 36),
  100. SizedBox(
  101. height: 24,
  102. width: 24,
  103. child: CircularProgressIndicator(),
  104. ),
  105. SizedBox(height: 36),
  106. Text(
  107. "正在搜索...",
  108. style: TextStyle(fontSize: 20),
  109. ),
  110. ],
  111. );
  112. }
  113. Widget _buildDone() {
  114. return Row(
  115. mainAxisSize: MainAxisSize.min,
  116. crossAxisAlignment: CrossAxisAlignment.center,
  117. children: [
  118. const Icon(Icons.check_circle, color: Colors.green, size: 32),
  119. const SizedBox(width: 12),
  120. Column(
  121. mainAxisSize: MainAxisSize.min,
  122. mainAxisAlignment: MainAxisAlignment.center,
  123. children: [
  124. const Text(
  125. "MAC地址:",
  126. style: TextStyle(fontSize: 24),
  127. ),
  128. const SizedBox(height: 8),
  129. Text(
  130. value!,
  131. style: const TextStyle(fontSize: 24),
  132. ),
  133. ],
  134. ),
  135. ],
  136. );
  137. }
  138. }