part of search;

class SearchPage extends StatefulWidget {
  const SearchPage({Key? key}) : super(key: key);

  @override
  State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<SearchController>(
      init: SearchController(),
      builder: (controller) => Scaffold(
        appBar: CustomAppBar(
          automaticallyImplyLeading: false,
          titleSpacing: 0,
          title: Padding(
            padding: EdgeInsets.symmetric(horizontal: AppTheme.margin),
            child: CustomInputSearch(
              controller: controller.searchController,
              readOnly: false,
              hintText: '请输入书籍名称',
              onTap: () {
                // context.pushNamed(Routes.msgs);
              },
              onEditingComplete: () {
                controller.showHistory = false;
                controller.search();
                // FocusScope.of(context).unfocus();
                FocusNode blankNode = FocusNode();
                FocusScope.of(context).requestFocus(blankNode); //指定为空白焦点
              },
            ),
          ),
          actions: [
            GestureDetector(
              onTap: () {
                context.pop();
              },
              child: Container(
                padding: EdgeInsets.only(left: 10.w, top: 10.w, bottom: 10.w),
                child: Text(
                  '取消',
                  style:
                      TextStyle(fontSize: 14.w, height: 1.5, color: Colours.c3),
                ),
              ),
            )
          ],
        ),
        body:
        buildListView(controller),

      ),
    );
  }

  Widget buildListView(SearchController controller) {
    if (controller.showHistory) {
      // 如果条件为true，显示历史搜索记录
      return BuildHistory(controller: controller);
    } else {
      // 如果条件为false，显示搜索到的书籍
      if(controller.books.isEmpty){
        return
          SingleChildScrollView(
              child:Container(
                padding: EdgeInsets.only(top: 110.w),
                alignment: Alignment.center,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    SizedBox(height: 152.w, width: 195.w,
                      child: Image.asset( 'assets/images/none.png',),),
                    SizedBox(height: 13.w,),
                    Text('暂无图书，请检查输入内容',style: TextStyle(fontSize: 14.w,color: Colours.c9),),
                  ],
                ),
              ),
          );
      }else{
        return ListView.builder(
          // shrinkWrap: true,
          // physics: NeverScrollableScrollPhysics(),
            scrollDirection: Axis.vertical,
            itemCount: controller.books.length,
            itemBuilder: (BuildContext context, int index) {
              CourseModel model = controller.books[index];
              return GestureDetector(
                onTap: () {
                  context.pushNamed(Routes.bookDetail,
                      queryParameters: {'book_id': model.bookId.toString()});
                },
                child: LibraryCell(
                  model: model,
                  onTap: () {
                    controller.love(
                        bookId: model.bookId.toString(),
                        isCollection: model.isCollection!);
                  },
                ),
              );
            });
      }

    }
  }

  hideSoftKeyboard(BuildContext context) {
    FocusNode blankNode = FocusNode(); //空白焦点,不赋值给任何输入框的focusNode
    FocusScope.of(context).requestFocus(blankNode); //指定为空白焦点
  }
}
