Files
blog_app/lib/widget/search.dart
2025-11-23 20:28:45 +08:00

100 lines
2.8 KiB
Dart

import 'package:blog_app/models/blog.dart';
import 'package:blog_app/widget/blog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common/widget/common_widget.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) {
final colors = Theme.of(context).colorScheme;
return CommonCard(
child: ListTile(
title: Text(
blog.title,
style: TextStyle(fontWeight: FontWeight.bold, color: colors.primary),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 4),
Text(
blog.content,
style: TextStyle(color: colors.secondary, fontSize: 14),
),
],
),
onTap: () => navigatorToBlogDetail(context, blog.id),
),
);
}
Widget buildSearchResultList(
BuildContext context,
List<BlogSearch> searchResultList,
) {
return ListView.separated(
itemCount: searchResultList.length,
itemBuilder: (context, index) {
return buildSearchResultItem(context, searchResultList[index]);
},
separatorBuilder: (BuildContext context, int index) {
return SizedBox(height: 8);
},
);
}
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])),
],
),
);
}