Files
blog_app/lib/widget/search.dart
2025-11-16 21:56:26 +08:00

100 lines
2.8 KiB
Dart

import 'package:blog_app/models/blog.dart';
import 'package:blog_app/pages/blog_detail_page.dart';
import 'package:blog_app/widget/blog.dart';
import 'package:blog_app/widget/common.dart';
import 'package:flutter/material.dart';
Widget buildSearchField({
required TextEditingController controller,
required FocusNode focusNode,
required Function(String) onChanged,
}) {
return TextField(
controller: controller,
focusNode: focusNode,
autofocus: true,
style: TextStyle(color: Colors.white, fontSize: 18),
cursorColor: Colors.white,
decoration: InputDecoration(
hintText: '搜索文章内容...',
hintStyle: TextStyle(color: Colors.white70),
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
),
onChanged: onChanged,
textInputAction: TextInputAction.search,
);
}
Widget buildSearchResultItem(BuildContext context, BlogSearch blog) {
return buildCard(
context: context,
child: ListTile(
title: Text(
blog.title,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.primary,
),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 4),
Text(
blog.content,
style: TextStyle(color: Colors.grey[600], fontSize: 14),
),
],
),
onTap: () => navigatorToBlogDetail(context, blog.id),
),
);
}
Widget buildSearchResultList(
BuildContext context,
List<BlogSearch> searchResultList,
) {
return ListView.builder(
itemCount: searchResultList.length,
itemBuilder: (context, index) {
return buildSearchResultItem(context, searchResultList[index]);
},
);
}
Widget buildSearchQueryEmpty() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.search, size: 64, color: Colors.grey[400]),
SizedBox(height: 16),
Text('搜索文章内容', style: TextStyle(color: Colors.grey[600], fontSize: 16)),
SizedBox(height: 8),
Text('输入标题或内容进行搜索', style: TextStyle(color: Colors.grey[500])),
],
),
);
}
Widget buildSearchResultEmpty(String query) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.search_off, size: 64, color: Colors.grey[400]),
SizedBox(height: 16),
Text(
'没有找到"$query"相关文章',
style: TextStyle(color: Colors.grey[600], fontSize: 16),
),
SizedBox(height: 8),
Text('请尝试其他关键词', style: TextStyle(color: Colors.grey[500])),
],
),
);
}