|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_markdown/flutter_markdown.dart'; |
| 3 | +import 'package:get/get.dart'; |
| 4 | +import 'package:miofeed/models/search_data.dart'; |
| 5 | +import 'package:miofeed/models/universal_item.dart'; |
| 6 | +import 'package:miofeed/utils/rss/rss_utils.dart'; |
| 7 | + |
| 8 | +searchDialog(BuildContext context) { |
| 9 | + final controller = Get.put(_SearchController()); |
| 10 | + final dataParagraph = RssUtils.allRssParagraph; |
| 11 | + controller.searchData.clear(); |
| 12 | + return SimpleDialog( |
| 13 | + children: [ |
| 14 | + Container( |
| 15 | + margin: const EdgeInsets.symmetric(horizontal: 10), |
| 16 | + child: Column( |
| 17 | + children: [ |
| 18 | + SearchBar( |
| 19 | + onChanged: (value) async { |
| 20 | + controller.hasSearch.value = true; |
| 21 | + controller.searchData.clear(); |
| 22 | + if (value != '') { |
| 23 | + for (var paraData in dataParagraph) { |
| 24 | + final UniversalItem item = paraData['item']; |
| 25 | + final String title = item.title; |
| 26 | + final String description = item.content |
| 27 | + .replaceAll(RegExp(r'<[^>]*>|&[^;]+;'), ' ') |
| 28 | + .replaceAll(RegExp(r'\n{2,}'), ' ') |
| 29 | + .replaceAll(RegExp(r'\r{2,}'), ' ') |
| 30 | + .replaceAll(RegExp(r' {2,}'), ' ') |
| 31 | + .trim(); |
| 32 | + bool matchTitle = false; |
| 33 | + String? outputTitle; |
| 34 | + bool matchContent = false; |
| 35 | + List<String>? outputContent; |
| 36 | + if (title.contains(value)) { |
| 37 | + matchTitle = true; |
| 38 | + outputTitle = title.replaceAll(value, " **$value** "); |
| 39 | + print(outputTitle); |
| 40 | + } |
| 41 | + |
| 42 | + if (description.contains(value)) { |
| 43 | + matchContent = true; |
| 44 | + // 使用正则表达式匹配 value |
| 45 | + RegExp regExp = RegExp( |
| 46 | + '(.{0,17}$value.{0,17})', |
| 47 | + unicode: true, // 确保处理中文字符 |
| 48 | + ); |
| 49 | + description.replaceAll(value, " **$value** "); |
| 50 | + Iterable<RegExpMatch> matches = |
| 51 | + regExp.allMatches(description); |
| 52 | + outputContent = []; |
| 53 | + for (var match in matches) { |
| 54 | + if (match.group(0) != null) { |
| 55 | + outputContent.add(match.group(0)!); |
| 56 | + } |
| 57 | + } |
| 58 | + print(outputContent); |
| 59 | + controller.searchData.add( |
| 60 | + SearchData( |
| 61 | + matchTitle: matchTitle, |
| 62 | + matchContent: matchContent, |
| 63 | + title: outputTitle, |
| 64 | + contentMatched: outputContent, |
| 65 | + ), |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + print(controller.searchData.length); |
| 70 | + } else { |
| 71 | + controller.hasSearch.value = false; |
| 72 | + controller.searchData.clear(); |
| 73 | + } |
| 74 | + }, |
| 75 | + ), |
| 76 | + SizedBox( |
| 77 | + width: MediaQuery.of(context).size.width - 20, |
| 78 | + height: MediaQuery.of(context).size.height - 20, |
| 79 | + child: Obx( |
| 80 | + () => controller.hasSearch.value |
| 81 | + ? ListView.builder( |
| 82 | + itemCount: controller.searchData.length, |
| 83 | + itemBuilder: (BuildContext context, int i) { |
| 84 | + final item = controller.searchData[i]; |
| 85 | + List<Widget> contents = []; |
| 86 | + |
| 87 | + if (item.matchContent) { |
| 88 | + for (var c in item.contentMatched!) { |
| 89 | + contents.add(MarkdownBody(data: c)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return controller.searchData.isNotEmpty |
| 94 | + ? item.matchTitle |
| 95 | + ? ListTile( |
| 96 | + title: MarkdownBody(data: item.title!), |
| 97 | + subtitle: contents.isNotEmpty |
| 98 | + ? Column( |
| 99 | + crossAxisAlignment: |
| 100 | + CrossAxisAlignment.start, |
| 101 | + children: contents, |
| 102 | + ) |
| 103 | + : null, |
| 104 | + ) |
| 105 | + : ListTile( |
| 106 | + title: contents.isNotEmpty |
| 107 | + ? Column( |
| 108 | + crossAxisAlignment: |
| 109 | + CrossAxisAlignment.start, |
| 110 | + children: contents, |
| 111 | + ) |
| 112 | + : null, |
| 113 | + ) |
| 114 | + : Center( |
| 115 | + child: Container( |
| 116 | + margin: const EdgeInsets.only(top: 40), |
| 117 | + child: const Text('什么也没搜到...'), |
| 118 | + ), |
| 119 | + ); |
| 120 | + }) |
| 121 | + : Center( |
| 122 | + child: Container( |
| 123 | + margin: const EdgeInsets.only(top: 40), |
| 124 | + child: const Text('来搜点什么吧...'), |
| 125 | + ), |
| 126 | + ), |
| 127 | + // child: ListView( |
| 128 | + // children: [ |
| 129 | + // Center( |
| 130 | + // child: Container( |
| 131 | + // margin: const EdgeInsets.only(top: 40), |
| 132 | + // child: const Text('来搜点什么吧...'), |
| 133 | + // ), |
| 134 | + // ) |
| 135 | + // ], |
| 136 | + // ), |
| 137 | + ), |
| 138 | + ), |
| 139 | + ], |
| 140 | + ), |
| 141 | + ), |
| 142 | + ], |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +class _SearchController extends GetxController { |
| 147 | + var hasSearch = false.obs; |
| 148 | + var searchData = <SearchData>[].obs; |
| 149 | +} |
0 commit comments