Skip to content

Commit

Permalink
没有任何软用的搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
Muska-Ami committed Sep 14, 2024
1 parent 4f51556 commit b6c1c9e
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 23 deletions.
14 changes: 14 additions & 0 deletions lib/models/search_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class SearchData {
SearchData({
required this.matchTitle,
required this.matchContent,
this.title,
this.contentMatched,
});

final bool matchTitle;
final bool matchContent;

final String? title;
final List<String>? contentMatched;
}
13 changes: 3 additions & 10 deletions lib/ui/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:miofeed/controllers/navigator_controller.dart';
import 'package:miofeed/models/universal_feed.dart';
import 'package:miofeed/models/universal_item.dart';
import 'package:miofeed/tasks/subscribe_update.dart';
import 'package:miofeed/ui/models/search_dialog.dart';
import 'package:miofeed/ui/paragraph.dart';
import 'package:miofeed/utils/after_layout.dart';
import 'package:miofeed/utils/paragraph_utils.dart';
Expand Down Expand Up @@ -38,15 +39,7 @@ class HomeUI extends StatelessWidget {
),
IconButton(
onPressed: () async {
Get.dialog(
const SimpleDialog(
children: [
Center(
child: Text('Comming soon'),
)
],
),
);
Get.dialog(searchDialog(context));
},
icon: const Icon(Icons.search),
)
Expand Down Expand Up @@ -161,7 +154,7 @@ class HomeUI extends StatelessWidget {
),
),
),
bottomNavigationBar: NavigationBarX().build(),
bottomNavigationBar: navigationBar(),
);
}

Expand Down
12 changes: 5 additions & 7 deletions lib/ui/models/navigation_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import 'package:get/get.dart';

import '../../controllers/navigator_controller.dart';

class NavigationBarX {
final NavigatorController nctr = Get.put(NavigatorController());
final NavigatorController _nCtr = Get.put(NavigatorController());

build() => NavigationBar(
selectedIndex: nctr.currentPage.value,
navigationBar() => NavigationBar(
selectedIndex: _nCtr.currentPage.value,
onDestinationSelected: (index) async {
nctr.currentPage.value = index;
nctr.subCurrentPage.value = 0;
_nCtr.currentPage.value = index;
_nCtr.subCurrentPage.value = 0;
switch (index) {
case 0:
// 首页
Expand All @@ -26,4 +25,3 @@ class NavigationBarX {
NavigationDestination(icon: Icon(Icons.settings), label: "设置"),
],
);
}
149 changes: 149 additions & 0 deletions lib/ui/models/search_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:get/get.dart';
import 'package:miofeed/models/search_data.dart';
import 'package:miofeed/models/universal_item.dart';
import 'package:miofeed/utils/rss/rss_utils.dart';

searchDialog(BuildContext context) {
final controller = Get.put(_SearchController());
final dataParagraph = RssUtils.allRssParagraph;
controller.searchData.clear();
return SimpleDialog(
children: [
Container(
margin: const EdgeInsets.symmetric(horizontal: 10),
child: Column(
children: [
SearchBar(
onChanged: (value) async {
controller.hasSearch.value = true;
controller.searchData.clear();
if (value != '') {
for (var paraData in dataParagraph) {
final UniversalItem item = paraData['item'];
final String title = item.title;
final String description = item.content
.replaceAll(RegExp(r'<[^>]*>|&[^;]+;'), ' ')
.replaceAll(RegExp(r'\n{2,}'), ' ')
.replaceAll(RegExp(r'\r{2,}'), ' ')
.replaceAll(RegExp(r' {2,}'), ' ')
.trim();
bool matchTitle = false;
String? outputTitle;
bool matchContent = false;
List<String>? outputContent;
if (title.contains(value)) {
matchTitle = true;
outputTitle = title.replaceAll(value, " **$value** ");
print(outputTitle);
}

if (description.contains(value)) {
matchContent = true;
// 使用正则表达式匹配 value
RegExp regExp = RegExp(
'(.{0,17}$value.{0,17})',
unicode: true, // 确保处理中文字符
);
description.replaceAll(value, " **$value** ");
Iterable<RegExpMatch> matches =
regExp.allMatches(description);
outputContent = [];
for (var match in matches) {
if (match.group(0) != null) {
outputContent.add(match.group(0)!);
}
}
print(outputContent);
controller.searchData.add(
SearchData(
matchTitle: matchTitle,
matchContent: matchContent,
title: outputTitle,
contentMatched: outputContent,
),
);
}
}
print(controller.searchData.length);
} else {
controller.hasSearch.value = false;
controller.searchData.clear();
}
},
),
SizedBox(
width: MediaQuery.of(context).size.width - 20,
height: MediaQuery.of(context).size.height - 20,
child: Obx(
() => controller.hasSearch.value
? ListView.builder(
itemCount: controller.searchData.length,
itemBuilder: (BuildContext context, int i) {
final item = controller.searchData[i];
List<Widget> contents = [];

if (item.matchContent) {
for (var c in item.contentMatched!) {
contents.add(MarkdownBody(data: c));
}
}

return controller.searchData.isNotEmpty
? item.matchTitle
? ListTile(
title: MarkdownBody(data: item.title!),
subtitle: contents.isNotEmpty
? Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: contents,
)
: null,
)
: ListTile(
title: contents.isNotEmpty
? Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: contents,
)
: null,
)
: Center(
child: Container(
margin: const EdgeInsets.only(top: 40),
child: const Text('什么也没搜到...'),
),
);
})
: Center(
child: Container(
margin: const EdgeInsets.only(top: 40),
child: const Text('来搜点什么吧...'),
),
),
// child: ListView(
// children: [
// Center(
// child: Container(
// margin: const EdgeInsets.only(top: 40),
// child: const Text('来搜点什么吧...'),
// ),
// )
// ],
// ),
),
),
],
),
),
],
);
}

class _SearchController extends GetxController {
var hasSearch = false.obs;
var searchData = <SearchData>[].obs;
}
2 changes: 1 addition & 1 deletion lib/ui/paragraph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class ParagraphUI extends StatelessWidget {
),
],
),
bottomNavigationBar: NavigationBarX().build(),
bottomNavigationBar: navigationBar(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ui/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SettingsUI extends StatelessWidget {
),
],
),
bottomNavigationBar: NavigationBarX().build(),
bottomNavigationBar: navigationBar(),
);
}
}
2 changes: 1 addition & 1 deletion lib/ui/settings/about.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class AboutUI extends StatelessWidget {
),
],
),
bottomNavigationBar: NavigationBarX().build(),
bottomNavigationBar: navigationBar(),
);
}
}
2 changes: 1 addition & 1 deletion lib/ui/settings/render.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RenderSettingUI extends StatelessWidget {
body: const Center(
child: Text('Coming soon'),
),
bottomNavigationBar: NavigationBarX().build(),
bottomNavigationBar: navigationBar(),
);
}
}
2 changes: 1 addition & 1 deletion lib/ui/settings/rss_subscribe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class _RssSubSettingState extends State<RssSubSettingUI> {
),
],
),
bottomNavigationBar: NavigationBarX().build(),
bottomNavigationBar: navigationBar(),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/settings/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class ThemeSettingUI extends StatelessWidget {
],
),
),
bottomNavigationBar: NavigationBarX().build(),
bottomNavigationBar: navigationBar(),
);
}

Expand Down
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.0"
flutter_markdown:
dependency: "direct main"
description:
name: flutter_markdown
sha256: a23c41ee57573e62fc2190a1f36a0480c4d90bde3a8a8d7126e5d5992fb53fb7
url: "https://pub.dev"
source: hosted
version: "0.7.3+1"
flutter_svg:
dependency: transitive
description:
Expand Down Expand Up @@ -456,6 +464,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.0"
markdown:
dependency: transitive
description:
name: markdown
sha256: ef2a1298144e3f985cc736b22e0ccdaf188b5b3970648f2d9dc13efd1d9df051
url: "https://pub.dev"
source: hosted
version: "7.2.2"
matcher:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies:
dynamic_color: ^1.7.0
background_fetch: ^1.3.5
package_info_plus: ^8.0.2
flutter_markdown: ^0.7.3+1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit b6c1c9e

Please sign in to comment.