提交 ebcbb949 authored 作者: yueweilu's avatar yueweilu

搜索界面接口调试与界面修正

上级 3b9e556c
......@@ -85,5 +85,51 @@ abstract class CommonAPI {
return false;
}
/// 6、清除搜索记录
///
static Future <bool> clear() async {
final result = await HttpService.to.post(
'/v1/book/Information/serachDel',
params: {},
);
if (result.data is Map && result.data['is_success'] == 1){
return true;
}
return false;
}
/// 7、搜索历史记录
///
static Future <List<String>> searchRecords() async {
final result = await HttpService.to.post(
'/v1/book/Information/serachRecord',
params: {},
);
if (result.data is! Map && result.data['list'] is! List) return [];
return List.generate(result.data['list'].length, (index){
return result.data['list'][index]['name'];
});
}
/// 8、搜索书籍
static Future <List<CourseModel>> searchBooks({
int page = 1,
int limit = 20,
required String keyWord
}) async {
final result = await HttpService.to.post(
'/v1/book/Information/searchBook',
params: {
'currentPage': page,
'pageSize': limit,
'keyword':keyWord
},
);
if (result.data is! Map && result.data['list'] is! List) return [];
return List.generate(result.data['list'].length, (index){
return CourseModel.fromJson(result.data['list'][index]);
});
}
}
\ No newline at end of file
......@@ -4,15 +4,47 @@ class SearchController extends GetxController {
late TextEditingController searchController = TextEditingController();
final int _maxStorageNum = 10;
// 搜索记录数组
List<String> records = [];
// 书籍数组
List<CourseModel> books = [];
late List<String> searchList;
@override
void onInit() {
searchList = _getHistory();
super.onInit();
void onReady() {
_getSearchRecords();
super.onReady();
}
@override
void onClose() {
searchController.dispose();
super.onClose();
}
/// 获取搜索记录
void _getSearchRecords() async {
records = await CommonAPI.searchRecords();
update();
}
/// 清除搜索记录
void clear() async {
bool result = await CommonAPI.clear();
if (result) {
_getSearchRecords();
}
}
/// 搜索书籍
void search() async {
books = await CommonAPI.searchBooks(keyWord: searchController.text);
_getSearchRecords();
update();
}
/// 本地搜索历史
/*
///获取搜索关键字数据
List<String> _getHistory(){
List <String>historys = [];
......@@ -61,12 +93,9 @@ class SearchController extends GetxController {
update();
}
*/
@override
void onClose() {
searchController.dispose();
super.onClose();
}
}
\ No newline at end of file
......@@ -7,6 +7,8 @@ import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:go_router/go_router.dart';
import '../../apis/index.dart';
import '../../models/index.dart';
import '../../services/index.dart';
import '../../theme.dart';
import '../../utils/index.dart';
......
......@@ -17,7 +17,7 @@ class _SearchPageState extends State<SearchPage> {
automaticallyImplyLeading:false,
titleSpacing: 0,
title: Padding(
padding: EdgeInsets.symmetric(horizontal: AppTheme.margin),
padding: const EdgeInsets.symmetric(horizontal: AppTheme.margin),
child: CustomInputSearch(
controller: controller.searchController,
readOnly:false,
......@@ -26,7 +26,7 @@ class _SearchPageState extends State<SearchPage> {
// context.pushNamed(Routes.msgs);
},
onEditingComplete: (){
controller.saveHistory();
controller.search();
},
),
),
......@@ -36,14 +36,14 @@ class _SearchPageState extends State<SearchPage> {
context.pop();
},
child: Container(
padding: EdgeInsets.only(left: 10,top: 10,bottom: 10),
child: Text('取消',style: TextStyle(fontSize: 14,height: 1.5,color: Colours.c3),),
padding: const EdgeInsets.only(left: 10,top: 10,bottom: 10),
child: const Text('取消',style: TextStyle(fontSize: 14,height: 1.5,color: Colours.c3),),
// color: Colors.red,
),
)
],
),
body: BuildHistory(),
body: BuildHistory(controller: controller),
),
);
}
......
part of search;
class BuildHistory extends StatefulWidget {
const BuildHistory({super.key});
final SearchController controller;
const BuildHistory({
Key? key,
required this.controller,
}) : super(key: key);
@override
State<BuildHistory> createState() => _BuildHistoryState();
......@@ -9,13 +14,9 @@ class BuildHistory extends StatefulWidget {
class _BuildHistoryState extends State<BuildHistory> {
// List<String> _searchList = [];
@override
Widget build(BuildContext context) {
return GetBuilder<SearchController>(
init: SearchController(),
builder:(controller)=> Container(
return Container(
margin: const EdgeInsets.only(left: 13,top: 18),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
......@@ -26,10 +27,10 @@ class _BuildHistoryState extends State<BuildHistory> {
const Text('搜索历史',style: TextStyle(fontSize: 14,height: 1.5,color: Colours.c3,fontWeight: Fonts.medium),),
GestureDetector(
onTap: (){
controller.clearHistory();
widget.controller.clear();
},
child: Container(
padding: EdgeInsets.all(10),
padding: const EdgeInsets.all(10),
// width: 10,
// height: 10,
// color: Colors.cyan,
......@@ -45,19 +46,17 @@ class _BuildHistoryState extends State<BuildHistory> {
runSpacing: 10,
alignment: WrapAlignment.start,
runAlignment: WrapAlignment.start,
children: _buildItem(controller.searchList),
children: _buildItem(widget.controller.records),
),
],
),
),
);
);
}
List <Widget> _buildItem(List<String> searchList){
print(searchList);
if (searchList == null || searchList.isEmpty){
List<Widget> list = [
Container(
const SizedBox(
width: 0,
height: 0,
)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论