优化搜索界面

This commit is contained in:
DelLevin-Home
2026-08-09 22:07:55 +08:00
parent b55523c3b9
commit 4ffc167967
15 changed files with 206 additions and 101 deletions

View File

@@ -7,15 +7,34 @@ import '../../utils/toast_util.dart';
import 'movie_detail_page.dart';
import 'book_detail_page.dart';
/// 在线搜索影视/书籍
class OnlineSearchPage extends StatefulWidget {
/// 在线搜索影视/书籍(带 Scaffold + AppBar
class OnlineSearchPage extends StatelessWidget {
const OnlineSearchPage({super.key});
@override
State<OnlineSearchPage> createState() => _OnlineSearchPageState();
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return Scaffold(
backgroundColor: colors.surface,
appBar: AppBar(
title: const Text('在线搜索'),
elevation: 0,
scrolledUnderElevation: 0,
),
body: const OnlineSearchPageBody(),
);
}
}
class _OnlineSearchPageState extends State<OnlineSearchPage> {
/// 在线搜索 body —— 可嵌入到统一搜索页 SearchHubPage
class OnlineSearchPageBody extends StatefulWidget {
const OnlineSearchPageBody({super.key});
@override
State<OnlineSearchPageBody> createState() => _OnlineSearchPageBodyState();
}
class _OnlineSearchPageBodyState extends State<OnlineSearchPageBody> {
final _searchController = TextEditingController();
final _focusNode = FocusNode();
final _movieScrollController = ScrollController();
@@ -252,39 +271,45 @@ class _OnlineSearchPageState extends State<OnlineSearchPage> {
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return Scaffold(
backgroundColor: colors.surface,
appBar: AppBar(
titleSpacing: 8,
title: _buildSearchBar(colors),
actions: [
TextButton(
onPressed: _doSearch,
child: Text('搜索',
style: TextStyle(fontSize: 14, color: colors.primary)),
return Material(
color: colors.surface,
child: Column(
children: [
// 搜索栏 + 搜索按钮
Padding(
padding: const EdgeInsets.fromLTRB(12, 8, 8, 8),
child: Row(
children: [
Expanded(child: _buildSearchBar(colors)),
TextButton(
onPressed: _doSearch,
child: Text('搜索',
style: TextStyle(fontSize: 14, color: colors.primary)),
),
],
),
),
// 影视/书籍 tab 栏(搜索后显示)
if (_hasSearched)
Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: colors.outlineVariant, width: 0.5))),
child: Row(children: [
_buildTabButton(colors, '影视', 0, _movieTotal),
_buildTabButton(colors, '书籍', 1, _bookTotal),
]),
),
Expanded(
child: _hasSearched
? (_currentTab == 0
? _buildMovieResults(colors)
: _buildBookResults(colors))
: _buildHistoryPanel(colors),
),
],
bottom: _hasSearched
? PreferredSize(
preferredSize: const Size.fromHeight(40),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: colors.outlineVariant, width: 0.5))),
child: Row(children: [
_buildTabButton(colors, '影视', 0, _movieTotal),
_buildTabButton(colors, '书籍', 1, _bookTotal),
]),
),
)
: null,
),
body: _hasSearched
? (_currentTab == 0
? _buildMovieResults(colors)
: _buildBookResults(colors))
: _buildHistoryPanel(colors),
);
}

View File

@@ -0,0 +1,92 @@
import 'package:flutter/material.dart';
import '../../utils/user_prefs.dart';
import 'search_page.dart';
import 'online_search_page.dart';
/// 统一搜索页 —— 本地搜索 / 增强搜索(在线)切换
class SearchHubPage extends StatefulWidget {
const SearchHubPage({super.key});
@override
State<SearchHubPage> createState() => _SearchHubPageState();
}
class _SearchHubPageState extends State<SearchHubPage> {
late bool _isOnline;
@override
void initState() {
super.initState();
_isOnline = UserPrefs().lastSearchOnline;
}
void _switchTo(bool online) {
if (!mounted || _isOnline == online) return;
setState(() => _isOnline = online);
UserPrefs().setLastSearchOnline(online);
}
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
final showToggle = UserPrefs().enhancedSearchEnabled;
return Scaffold(
appBar: AppBar(
title: const Text('搜索'),
elevation: 0,
scrolledUnderElevation: 0,
actions: [
if (showToggle) _buildToggle(colors),
const SizedBox(width: 4),
],
),
body: IndexedStack(
index: _isOnline ? 1 : 0,
children: const [
SearchPageBody(),
OnlineSearchPageBody(),
],
),
);
}
Widget _buildToggle(ColorScheme colors) {
return Container(
margin: const EdgeInsets.only(right: 8),
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
color: colors.surfaceContainerHighest.withValues(alpha: 0.6),
borderRadius: BorderRadius.circular(8),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
_toggleBtn('本地', !_isOnline, () => _switchTo(false), colors),
_toggleBtn('增强', _isOnline, () => _switchTo(true), colors),
],
),
);
}
Widget _toggleBtn(String label, bool selected, VoidCallback onTap, ColorScheme colors) {
return Material(
color: selected ? colors.primary : Colors.transparent,
borderRadius: BorderRadius.circular(6),
child: InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(6),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
child: Text(
label,
style: TextStyle(
fontSize: 12,
fontWeight: selected ? FontWeight.w600 : FontWeight.w400,
color: selected ? colors.onPrimary : colors.onSurface.withValues(alpha: 0.55),
),
),
),
),
);
}
}

View File

@@ -9,15 +9,34 @@ import '../note/note_detail_page.dart';
import '../game/game_detail_page.dart';
import '../../widgets/fade_in_local_image.dart';
/// 搜索页面
class SearchPage extends StatefulWidget {
/// 搜索页面(带 Scaffold + AppBar
class SearchPage extends StatelessWidget {
const SearchPage({super.key});
@override
State<SearchPage> createState() => _SearchPageState();
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return Scaffold(
backgroundColor: colors.surface,
appBar: AppBar(
title: const Text('搜索'),
elevation: 0,
scrolledUnderElevation: 0,
),
body: const SearchPageBody(),
);
}
}
class _SearchPageState extends State<SearchPage> {
/// 搜索页 body —— 可嵌入到统一搜索页 SearchHubPage
class SearchPageBody extends StatefulWidget {
const SearchPageBody({super.key});
@override
State<SearchPageBody> createState() => _SearchPageBodyState();
}
class _SearchPageBodyState extends State<SearchPageBody> {
final _searchController = TextEditingController();
final _focusNode = FocusNode();
@@ -33,12 +52,6 @@ class _SearchPageState extends State<SearchPage> {
Timer? _debounce;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _focusNode.requestFocus());
}
@override
void dispose() {
_searchController.dispose();
@@ -167,14 +180,9 @@ class _SearchPageState extends State<SearchPage> {
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return Scaffold(
backgroundColor: colors.surface,
appBar: AppBar(
title: const Text('搜索'),
elevation: 0,
scrolledUnderElevation: 0,
),
body: Column(children: [
return Material(
color: colors.surface,
child: Column(children: [
_buildSearchBar(),
_buildFilterRow(),
Expanded(
@@ -239,7 +247,7 @@ class _SearchPageState extends State<SearchPage> {
const SizedBox(width: 8),
Expanded(child: _filterChip('书籍', Icons.menu_book_outlined, _showBooks, bookCount, () { setState(() { _showBooks = !_showBooks; _performSearch(); }); })),
const SizedBox(width: 8),
Expanded(child: _filterChip('笔记', Icons.note_outlined, _showNotes, noteCount, () { setState(() { _showNotes = !_showNotes; _performSearch(); }); })),
Expanded(child: _filterChip('笔记', Icons.sticky_note_2_outlined, _showNotes, noteCount, () { setState(() { _showNotes = !_showNotes; _performSearch(); }); })),
const SizedBox(width: 8),
Expanded(child: _filterChip('游戏', Icons.sports_esports_outlined, _showGames, gameCount, () { setState(() { _showGames = !_showGames; _performSearch(); }); })),
]),